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>
PHP code isn´t right?
Moderator: General Moderators
Re: PHP code isn´t right?
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?
Welcome to the fourm. The problem has to do with how you fetch your results. Currently you have this:
You have curly brackets here ({ }), but no looping structure, so you only get the first row. Try this instead:
Hope that helps.
Code: Select all
$row = mysql_fetch_array($result);
{
echo $row['serial_no'] . " " . $row['description'] . " " . $row['price'] . " " . $row['quantity'];
echo "
";
}
Code: Select all
while($row = mysql_fetch_array($result))
{
echo $row['serial_no'] . " " . $row['description'] . " " . $row['price'] . " " . $row['quantity'];
echo "
";
}