Jump to content

Can't Understand Why This Code is'nt Working


JamesThePanda

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

ok this is what I have but for some reason its stoped working geting a parse error on the last line  :wtf:

 

 

<?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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.