Page 1 of 1

PHP code isn´t right?

Posted: Mon Aug 29, 2011 6:44 pm
by eduardchile
I want to see the latest results if I fill in my html form values for serial no, description, price and quantity, but it doesn´t work! Why?


<html>
<body>

<?php
$con = mysql_connect("localhost","eduardli_user","-z.x,c");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

$serial_no = $_POST["serial_no"];
$description = $_POST["description"];
$price = $_POST["price"];
$quantity = $_POST["quantity"];

mysql_select_db("eduardli_company", $con) or die(mysql_error());


mysql_query ("INSERT INTO Products (serial_no, description, price, quantity)
VALUES ('$serial_no', '$description', '$price', '$quantity')") or die(mysql_error());

$latest="select * from Products order by serial_no desc limit 0,1";
$result = mysql_query($latest,$con) or die(mysql_error());
$row = mysql_fetch_array($result);
{
echo $row['serial_no'] . " " . $row['description'] . " " . $row['price'] . " " . $row['quantity'];
echo "<br />";
}

mysql_close($con);
?>

</body>
</html>

Re: PHP code isn´t right?

Posted: Mon Aug 29, 2011 6:53 pm
by Celauran
You're missing a while loop.

Code: Select all

$latest="select * from Products order by serial_no desc limit 0,1";
$result = mysql_query($latest,$con) or die(mysql_error());
while ($row = mysql_fetch_array($result))
{
    echo $row['serial_no'] . " " . $row['description'] . " " . $row['price'] . " " . $row['quantity'];
}

Re: PHP code isn´t right?

Posted: Mon Aug 29, 2011 6:54 pm
by nowaydown1
Welcome to the fourm. The problem has to do with how you fetch your results. Currently you have this:

Code: Select all

$row = mysql_fetch_array($result);
{
     echo $row['serial_no'] . " " . $row['description'] . " " . $row['price'] . " " . $row['quantity'];
     echo "
     ";
}
You have curly brackets here ({ }), but no looping structure, so you only get the first row. Try this instead:

Code: Select all

while($row = mysql_fetch_array($result))
{
     echo $row['serial_no'] . " " . $row['description'] . " " . $row['price'] . " " . $row['quantity'];
     echo "
     ";
}
Hope that helps.