I have written up some code to take a upc entry from a text box and upon submit will pass the "upc" to the query code.
My problem is, none of my echos will post to my page. I can get them to post in CLI, but not on my web browser. If I put in test echos before the fetch line of the query they will echo to the page, but anything after the query will not. Here is the code:
<?php
//setup
require_once('Factual.php');
$factual = new Factual("mykeyhere","myotherkeyhere");
$upc = $_GET["upc"];
$query = new FactualQuery;
$query->search($upc);
$res = $factual->fetch("products", $query);
$data = $res->getData();
//print_r($res->getData());
$item = $data[1];
$itemAvgPrice = $item['avg_price'];
$itemBrand = $item['brand'];
$itemCategory = $item['category'];
$itemEan13 = $item['ean13'];
$itemFactualID = $item['factual_id'];
$itemImage = $item['image_urls'][0];
$itemSize = $item['size'][0];
$itemUPC = $item['upc'];
echo "Average Price: " . $itemAvgPrice;
echo "\nBrand: " . $itemBrand;
echo "\nCategory: " . $itemCategory;
echo "\nEan13: " . $itemEan13;
echo "\nFactual ID: " . $itemFactualID;
echo "\nImage URL: " . $itemImage;
echo "\nSize: " . $itemSize;
echo "\nUPC: " . $itemUPC;
?>
Related
I have made a blog in PHP, on Post.php with If isset get I am checking Post ID and than getting slug of same ID from database and accessing the post with that slug.
The issue is its generating Url like: Post.php?Slug=postname-title
I want to make it Post.php?postname-title OR urls.com/postname-title
Please note I am getting post from database matching with slug If any how I do not set php to Get in url it is not getting value from the same slug.
Please guide if it will be done via .htaccess or any other way?
<?php
// add mysql_real_escape_string to slug, to prevent sql injection
$slug = $_GET["slug"];
$sql = "SELECT * FROM posts WHERE slug = '" . mysqli_real_escape_string($db, $_GET["slug"]) . "'";
$result = mysqli_query($db, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while ($row = mysqli_fetch_assoc($result)) {
echo "<h2>" . $row["title"] . "</h2>";
echo "<div id='post-details'>" . "Posted on " . $row["post_date"] . " -- " . "Last Update " . $row["last_edited"] .
"</div>";
if (loggedin()) { echo "<div id='postactions'>This Post:<a href='create_note.php?id=" . $row["id"] . "'>Add Notes</a>";
echo 'Delete Post';
echo 'Edit</div>'; }
$cat_id = $row["cat_id"];
$sql = "SELECT * FROM categories WHERE id = $cat_id";
$resultone = mysqli_query($db, $sql);
if (mysqli_num_rows($resultone) > 0) {
// output data of each row
while ($rowone = mysqli_fetch_assoc($resultone)) {
$cat_name = $rowone["name"];
echo "<div id='post-category'>" . "Category: <a href='all-categories.php?cat_id=$cat_id'>$cat_name</a>" . "</div>";
}
}
echo "<p>" . htmlspecialchars_decode($row["body"]) . "</p>";
$_SESSION["post_id"] = $row["id"];
$postid = $row["id"];
}
} else {
echo "0 results";
}
?>
If you want a simple .htaccess version, you could use the following rule:
RewriteRule ^post/(.+) Post.php?Slug=$1
This will change your URL from a pretty URL like: yourdomain.com/post/postname-title to yourdomain.com/Post.php?Slug=postname-title
I have this code and in "SELECT * FROM Blogi WHERE PostID=".
How to get each line separately so that takes postedBY, title, date, content by PostID
<?php
try{
$Blog = $conn->prepare("SELECT * FROM Blogi WHERE PostID=");
$Blog->execute();
}catch(PDOException $e){
echo $e->getMessage();
}
$fetch = $Blog->fetchAll();
$usercount = $Blog->rowCount();
if($usercount > 0){
foreach($fetch as $f){
$PostID = $f['PostID'];
$PostedBY = $f['PostedBY'];
$Title = $f['Title'];
$Date = $f['Date'];
$Content = $f['Content'];
}
}else{
session_destroy();
header('location: index.php');
}
?>
I have very bad English, so I don't know anyone can understand this.
How to do it further this postID="" to that he would start to work?
You have already done it in foreach loop... so add a line for output like... Also you aren't sending any PostID value to your query, be sure that you are sending a value for it.
foreach($fetch as $f){
$PostID = $f['PostID'];
$PostedBY = $f['PostedBY']
$Title = $f['Title'];
$Date = $f['Date'];
$Content = $f['Content'];
echo $PostID . " " . $Title . " " . $Date . " " . $Content . "<br />";
}
I'm not too familiar with PHP arrays, I have the following code that generates query to output the results needed.
$allstore = $_POST['store'];
function createSelect($allstore)
{
if (empty($allstore))
return "";
$querySelect = "";
$queryJoin = "";
$baseTable = "";
foreach ($allstore as $store => $value) {
if (!$querySelect) {
$baseTable = $store;
$querySelect = "SELECT " . $store . ".item_no, " . $store . ".actual_price, " . $store . ".selling_price, " . $store . ".qty as " . $store;
} else {
$querySelect .= ", " . $store . ".qty as " . $store;
$queryJoin .= "
INNER JOIN " . $store . " ON " . $baseTable . ".item_no = " . $store . ".item_no";
}
}
$querySelect .= " FROM " . $baseTable;
$query = $querySelect . $queryJoin;
return $query;
}
//Stores to be shown
$allstore = ['s_M9' =>0 , 's_M10' =>1];
$query = (createSelect($allstore));
$result = mysql_query($query);
//rest of code...
As you can see above, at the very top there is $allstore = $_POST['store']; Which collects values based from previous form POST method that has checkbox with the name=store[] .
Now According to the function shown, if I create my own keys and values like this
$allstore = ['s_M9' =>0 , 's_M10' =>1];
the output shows exactly what i'm looking for. But the problem goes on how to let $allstore implode those stores s_M9, s_M10 based on what the user has selected on the previous page ( checkbox )? I mean, the user can select either one of the stores or Both stores . How can I implode the checked results between those brackets without inserting them manually?
Thank You
Edit :
<?php
echo "<form action='somewhere.php' method='POST'>";
$query = "SELECT * from stores_list ORDER BY short Asc";
$result = mysql_query($query);
if(mysql_num_rows($result)>0){
$num = mysql_num_rows($result);
for($i=0;$i<$num;$i++){
$row = mysql_fetch_assoc($result);
echo "<input type=checkbox name=store[] value={$row['short']} style='width:20px; height:20px;'>{$row['short']}";
}
}
else{
//No Stores Available
echo "No Stores Found !";
}
echo "</td><input type='submit' value='Search'/></form>";
$allstore = [];
if (!empty($_POST['store'])) {
foreach ($_POST['store'] as $value) {
$allstore[$value] = 1; // or 0, it doesn't matter because your function adds all the keys
}
}
$query = (createSelect($allstore));
$result = mysql_query($query);
And of course you have to take care of your createSelect function to avoid SQL Injections, please read here
I'm trying to query some vars in my MySQL db, but for some reasons it returns 0.
Here's the code:
<html>
<body>
<?php
session_start();
$connection = mysqli_connect("", "", "", "");
if (empty($connection))
$error = "Could not connect to Database, please contact an admin with this code: DBE1";
$tnews = mysqli_query($connection, "SELECT * FROM Test ORDER BY ID DESC");
while ($news = mysqli_fetch_array($tnews))
{
$title = $news['Title'];
$pimg = $news['PrevImg'];
$ptext = $news['PrevText'];
$text = $news['Text'];
$img1 = $news['Img1'];
echo "<h1>" + $title + "</h1><br><br>";
echo "<p>" + $pimg + "</p>";
echo "<br><p>" + $ptext + "</p><br><br>";
echo "<p>" + $text + "</p>";
echo "<br><p>" + $img1 + "</p>";
echo $title;
}
mysqli_close($connection);
?>
</body>
</html>
Oh and the result
00000
I have another site where almost (var names, echo's, ...) everything is the same, it works there! I have really have no idea what's wrong, does anyone have an idea?
P.S: The DB Connection has empty strings because it has the ip, username, password and db of my database! ;D
Thanks for the help!
The + is a concatenate operator in JS, use PHP's equivalent, being a dot .
Now, as stated in comments, you are outputting content before sessions.
You may get a warning similar to this:
Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /path/to/file:3) in /path/to/file.php on line 4
Corrected code, is you still want to use sessions. If not, just remove session_start();
Sidenote: It is important not to have a space before <?php or any other output such as HTML, a cookie or a byte order mark, which will also account as outputting before header.
<?php
session_start();
?>
<html>
<body>
<?php
$connection = mysqli_connect("", "", "", "");
if (empty($connection))
$error = "Could not connect to Database, please contact an admin with this code: DBE1";
$tnews = mysqli_query($connection, "SELECT * FROM Test ORDER BY ID DESC");
while ($news = mysqli_fetch_array($tnews))
{
$title = $news['Title'];
$pimg = $news['PrevImg'];
$ptext = $news['PrevText'];
$text = $news['Text'];
$img1 = $news['Img1'];
echo "<h1>" . $title . "</h1><br><br>";
echo "<p>" . $pimg . "</p>";
echo "<br><p>" . $ptext . "</p><br><br>";
echo "<p>" . $text . "</p>";
echo "<br><p>" . $img1 . "</p>";
echo $title;
}
mysqli_close($connection);
?>
</body>
</html>
Add error reporting to the top of your file(s) which will signal warnings/errors as shown further up in my answer.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of your code
Sidenote: Error reporting should only be done in staging, and never production.
I don't know how that worked out in your other server, but you should be using dots . instead of + to concatenate strings in php.
while ($news = mysqli_fetch_array($tnews))
{
$title = $news['Title'];
$pimg = $news['PrevImg'];
$ptext = $news['PrevText'];
$text = $news['Text'];
$img1 = $news['Img1'];
echo "<h1>" . $title . "</h1><br><br>";
echo "<p>" . $pimg . "</p>";
echo "<br><p>" . $ptext . "</p><br><br>";
echo "<p>" . $text . "</p>";
echo "<br><p>" . $img1 . "</p>";
echo $title;
}
I have a while loop printing multiple checkboxes..I changed them to checkboxes instead of radio buttons.. now all I want to do is pass the names of all those checkboxes to my vote.php file. If I give my checkbox in my loop a simple name and carry that over to my vote.php which handles all my POST data, it only carries over my last selection.. I want all of my selections. I cleaned my code up for you guys a little bit.
Tell me where I am going wrong here.. here is my initial code printing the buttons..
while($row_nominee=mysql_fetch_array($result_nominee)){
$id = $row_nominee[0];
//print "$level";
$prefix = $row_nominee[1];
$fname = $row_nominee[2];
$lname = $row_nominee[3];
$suffix = $row_nominee[4];
$city = $row_nominee[5];
$state = $row_nominee[6];
$zip = $row_nominee[7];
$bio = $row_nominee[8];
$level = $row_nominee[10];
$name = $prefix . " " . $fname . " " . $lname;
$address = $city . " " . $state . " " . $zip;
//print "$voted";
print "<tr>";
print "<td width=\"4\" valign=\"top\"><input type=\"checkbox\" name=\"candidateOne\" id=\"candidate\" value=$id></td>";
print "<td valign=\"top\"><FONT face=Tahoma,Arial,Helv size=-1><b>Name:</b> <font color=\"#ff0000\">$name</font><br><b>Hometown:</b> $address<br><b>Bio:<br /></b> $bio</font></td>";
print "</tr>";
}
?>
//now here is my vote.php file which handles the checkboxes.
//get the contents from the vote ballot Form
$voter_id = safeEscapeString(qsrequest(voter));
$candidate_id = safeEscapeString(qsrequest(candidateOne));
//print "$voter_id and $candidate_id";
include '../../sql/usagym_connection.php';
if(qsrequest(correct))
{
$voter_id1= safeEscapeString(qsrequest(voter1));
$candidate_id1= safeEscapeString(qsrequest(candidate1));
$votes1= safeEscapeString(qsrequest(votes1));
$votes1 += 1;
$sql_voter = "update stateChair_voters set voted='Y' where (usagnum='$voter_id1')";
//print "$sql_voter<br>";
$result_voter = mysql_query($sql_voter, $link) or die("Invalid query2");
$update_candidate = "update stateChair_nominees set votes=$votes1 where (id=$candidate_id1)";
//print "$update_candidate<br>";
$result_update = mysql_query($update_candidate, $link) or die("Invalid query3");
//print "Total votes is $votes1.";
header( "Location: vote_thanks.html");
exit;
}
else
{
//connect the database
$sql_candidate = "select id, prefix, fname, lname, suffix, city, state, zip, bio, votes from stateChair_nominees where id=$candidate_id";
$result_candidate = mysql_query($sql_candidate, $link) or die("Invalid query1". mysql_error());
while($row_candidate=mysql_fetch_array($result_candidate)){
$id = $row_candidate[0];
$prefix = $row_candidate[1];
$fname = $row_candidate[2];
$lname = $row_candidate[3];
$suffix = $row_candidate[4];
$city = $row_candidate[5];
$state = $row_candidate[6];
$zip = $row_candidate[7];
$bio = $row_candidate[8];
$votes = $row_candidate[9];
$name = $prefix . " " . $fname . " " . $lname;
$address = $city . " " . $state . " " . $zip;
}
?>
All I really want to do is submit multiple people to a vote and not just one person. Thoughts? Thanks guys!
Here is my code for my checkboxes..
print "<td width=\"4\" valign=\"top\"><input type=\"checkbox\" name=\"candidateOne\" id=\"candidate\" value=$id></td>";
Now here is the code that handles these checkboxes.. I didn't write this code and I am having to debug it, so any help is appreciated.
$candidate_id = safeEscapeString(qsrequest(candidateOne));
This code right now handles a string, not a variable. What's the process in having a variable represent multiple checkboxes on the other file while recording them on here?
print "<td width=\"4\" valign=\"top\"><input type=\"radio\" name=\"candidateOne\" id=\"candidate\" value=$id></td>";
You must change the 'name' as you have changed the 'value' in the loop by a variable.