JamesThePanda Posted March 31, 2012 Share Posted March 31, 2012 Hi I cant understand why this code isnt echoing the results. $ids = $_POST['uid']; $con = mysql_connect("localhost","root",""); mysql_select_db("product", $con); $sqlout = mysql_query("SELECT * FROM product2 WHERE 'ID' IN ('$ids')"); while ($sqlres = mysql_fetch_assoc($sqlout)) { echo $sqlres['filename'] . " " . $sqlres['title']; } mysql_close($con); Anyone got any ideas? Thanks James Link to comment https://forums.phpfreaks.com/topic/260041-cant-understand-why-this-code-isnt-working/ Share on other sites More sharing options...
AyKay47 Posted March 31, 2012 Share Posted March 31, 2012 There is an error in your SQL, if you had proper error handling established you would know this. The only way that double quotes can be used as an identifier quoting character is when the SQL mode include ANSI_QUOTES. Either way you are using single quotes to encase the field `id`. Use either back-ticks or none at all. Also, to use the IN clause here, $ids would need to be a comma delimited list of values, which I doubt it is. $ids = $_POST['uid']; $con = mysql_connect("localhost","root",""); mysql_select_db("product", $con); $ids = intval($ids); //sanitize data $sqlout = mysql_query("SELECT * FROM product2 WHERE ID = '$ids'") or die(mysql_error()); //select only the fields that you are going to use while ($sqlres = mysql_fetch_assoc($sqlout)) { echo $sqlres['filename'] . " " . $sqlres['title']; } mysql_close($con); Link to comment https://forums.phpfreaks.com/topic/260041-cant-understand-why-this-code-isnt-working/#findComment-1332881 Share on other sites More sharing options...
Psycho Posted March 31, 2012 Share Posted March 31, 2012 Easy - there were no results. One reason my be that you put the field name in single strait quotes. So instead of looking for any records were the field ID has a value in the list, the query is trying to see if the STRING 'ID' is in the list of values. You should create your queries as a string variable so you can echo to the page for debugging purposes. And you can add other code to verify if there were results or not. Also, I don't know what you expect the post value to look like, but you may not get the results you expect based on how it is formatted $ids = $_POST['uid']; $con = mysql_connect("localhost","root",""); mysql_select_db("product", $con); $query = "SELECT `filename`, `title` FROM `product2` WHERE `ID` IN ('$ids')" $result = mysql_query($query); if(!mysql_num_rows($result)) { echo "There were no results"; } else { while($row = mysql_fetch_assoc($result)) { echo "{$row['filename']} {$row['title']}<br>\n"; } } mysql_close($con); Link to comment https://forums.phpfreaks.com/topic/260041-cant-understand-why-this-code-isnt-working/#findComment-1332885 Share on other sites More sharing options...
JamesThePanda Posted March 31, 2012 Author Share Posted March 31, 2012 hmmm now its only returning a single result I have a group checklist HTML form that were the array is started. I wanted the script to pull details from the database when I select certain items. now if I sect multiple items it only returns one result. Link to comment https://forums.phpfreaks.com/topic/260041-cant-understand-why-this-code-isnt-working/#findComment-1332893 Share on other sites More sharing options...
AyKay47 Posted March 31, 2012 Share Posted March 31, 2012 make sure the checkbox input names are in array format. Post the relevant code please. Link to comment https://forums.phpfreaks.com/topic/260041-cant-understand-why-this-code-isnt-working/#findComment-1332897 Share on other sites More sharing options...
JamesThePanda Posted March 31, 2012 Author Share Posted March 31, 2012 ok this is what I have but for some reason its stoped working geting a parse error on the last line <?php $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } if (!isset($_POST['id'])){ mysql_select_db("product", $con); $result = mysql_query("SELECT * FROM product2"); echo'<form method="post" action="pro.php">'; while($row = mysql_fetch_array($result)) { echo "<tr><td><input type=\"checkbox\" name=\"uid[]\" value=\"".$row['ID']."\" /></td><td>".$row['filename'] . "</td><td> " . $row['title']. "</tr> "; } echo '<input type=\"submit\" name=\"formSubmit\" value=\"Submit\" />'; echo '</form>'; mysql_close($con); ?> Link to comment https://forums.phpfreaks.com/topic/260041-cant-understand-why-this-code-isnt-working/#findComment-1332908 Share on other sites More sharing options...
AyKay47 Posted March 31, 2012 Share Posted March 31, 2012 double quotes don't need escaped when the string is wrapped in single quotes, the backslashes will be interpolated as literal backslashes. echo '<input type="submit" name="formSubmit" value="Submit" />'; Link to comment https://forums.phpfreaks.com/topic/260041-cant-understand-why-this-code-isnt-working/#findComment-1332910 Share on other sites More sharing options...
JamesThePanda Posted March 31, 2012 Author Share Posted March 31, 2012 ok here is the new "improved" code. Also I'm getting a parse error on line 16 <?php $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } if (!isset($_POST['id'])){ mysql_select_db("product", $con); $result = mysql_query("SELECT * FROM product2"); echo'<form method="post" action="pro.php">'; while($row = mysql_fetch_array($result)) { echo '<tr><td><input type="checkbox" name="uid[]" value=".$row['ID']." /></td><td>".$row['filename']."</td><td>".$row['title']."</tr>'; } echo '<input type="submit" name="formSubmit" value="Submit" /></form>'; ?> Link to comment https://forums.phpfreaks.com/topic/260041-cant-understand-why-this-code-isnt-working/#findComment-1332913 Share on other sites More sharing options...
AyKay47 Posted March 31, 2012 Share Posted March 31, 2012 echo '<tr><td><input type="checkbox" name="uid[]" value="'.$row['ID'].'" /></td><td>"'.$row['filename'].'"</td><td>"'.$row['title'].'"</tr>'; Link to comment https://forums.phpfreaks.com/topic/260041-cant-understand-why-this-code-isnt-working/#findComment-1332917 Share on other sites More sharing options...
JamesThePanda Posted March 31, 2012 Author Share Posted March 31, 2012 Hi thanks for that I replace that li with your code, Now im getting an error on line 20 it makes no sence <?php $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } if (!isset($_POST['id'])){ mysql_select_db("product", $con); $result = mysql_query("SELECT * FROM product2"); echo'<form method="post" action="pro.php">'; while($row = mysql_fetch_array($result)) { echo '<tr><td><input type="checkbox" name="uid[]" value="'.$row['ID'].'" /></td><td>"'.$row['filename'].'"</td><td>"'.$row['title'].'"</tr>'; } echo '<input type="submit" name="formSubmit" value="Submit" /></form>'; ?> Thanks James Link to comment https://forums.phpfreaks.com/topic/260041-cant-understand-why-this-code-isnt-working/#findComment-1332918 Share on other sites More sharing options...
AyKay47 Posted March 31, 2012 Share Posted March 31, 2012 instead of saying "i'm getting an error", please post the error and the line(s) that it refers to. Link to comment https://forums.phpfreaks.com/topic/260041-cant-understand-why-this-code-isnt-working/#findComment-1332922 Share on other sites More sharing options...
homer.favenir Posted March 31, 2012 Share Posted March 31, 2012 you forgot the closing bracket of if (!isset($_POST['id'])){ Link to comment https://forums.phpfreaks.com/topic/260041-cant-understand-why-this-code-isnt-working/#findComment-1332924 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.