Why is the serial no always 0 while I put other numbers?


Php file:

<html>
<body>

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

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

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


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

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

mysql_close($con);
?>

</body>
</html>


form.html:

<html>
<head>
<style type="text/css">

#desc
{
width:10%;
height:5%;
}
</style>
<title>POST</title>
</head>

<body>


<form action='insert.php' method="post">

Serialno.: <input type="text" name="serialno" id="serialno"><br />
Description: <input type="text" name="description" id="description"><br />
Price: <input type="text" name="price" id="price" /><br />
Quantity: <input type="text" name="quantity" id="quantity" />
<input type='submit' value='submit' />


</form>

</body>
</html>

wrap your code in [code=language]codesample [/code] makes it easier to read, turns on code highlighting, sometimes the highlights can solve the problem, they show up mismatched braces miss-spelled codewords etc

your code wrapped in [code=php]

<?php
$con = mysql_connect("localhost","eduardli_user","-z.x,c");
if (!$con) { die('Could not connect: ' . mysql_error()); }
$description = $_POST["description"];
$price = $_POST["price"];
$quantity = $_POST["quantity"];
mysql_select_db("eduardli_company", $con) or die(mysql_error());
mysql_query ("INSERT INTO Products (description, price, quantity) VALUES ('$description', '$price', '$quantity')")
 or die(mysql_error());
$latest="select * from Products order by serialno desc limit 0,1";
$result = mysql_query($latest,$con) or die(mysql_error());
$row = mysql_fetch_array($result);
{ echo $row['serialno.'] . " " . $row['description'] . " " . $row['price'] . " " . $row['quantity'];
echo "<br />";
}
mysql_close($con);
?>

assuming serialno is the autoincrement field
try

$latest="select * from Products order by serialno desc limit 1";

select the first 1 result,
the original code chooses the first result starting at record(0), which is always equal to record(0)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.