PHP code isn´t right?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
eduardchile
Forum Newbie
Posts: 3
Joined: Mon Aug 29, 2011 6:31 pm

PHP code isn´t right?

Post 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>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: PHP code isn´t right?

Post 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'];
}
nowaydown1
Forum Contributor
Posts: 169
Joined: Sun Apr 27, 2008 1:22 am

Re: PHP code isn´t right?

Post 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.
Post Reply