...

View Full Version : Add new record



VickP07
10-04-2011, 07:35 AM
Hey guys,

So i have a page that allows the user to enter in new info into the database. Right now the user is able to enter in a recipe name, description, prep time, and total time.

After the user enters in the new data he/she clicks on a submit button to add the data to the database. This is all working PERFECTLY!!! Also, i am using mysql_insert_id to assign a auto 'id' for the new recipe data.

Right now i am trying to echo out the new id after the user clicks the submit button, I AM ONLY DNG THIS for testing/debug purposes i will remove later.

But, for some reason everytime after i add a new record into teh database i echo out the id and it is always = 0 instead of showing the actual 'id' value which i KNOW it should have

here is my code:


<?
$db = mysql_connect( "localhost","root", "temp1234");
mysql_select_db( "Peters_restaurantDB");

$sql = "
SELECT id, name, preptime, totaltime, rating
FROM recipes;
";

$result = mysql_query( $sql )
or die( "Bad query ($sql): " . mysql_error() );

if( $_POST )
{
$id = mysql_insert_id();
$rname = $_POST['rname'];
$desc = $_POST['desc'];
$prep = $_POST['ptime'];
$total = $_POST['ttime'];

$sql =
"
INSERT INTO recipes (id, name, description, preptime, totaltime)
VALUES ($id, '$rname', '$desc', $prep, $total);
";

echo "New Recipe: (" , $rname, ") has been added to the database! "; //pass new recipe
echo $id;

$result = mysql_query( $sql )
or die( "Insert failed ($sql): " . mysql_error() ); ?>

<p><form method="get" ACTION="addsteps.php?id=<?=$id?>">
<input type="submit" value="Add Steps"> Click here to view add steps form.
</form> </p>

<?
}

?>

<html>

<head></head>

<body>

<h1>Add Recipe</h1>

<hr/>

<form action="addnewrec.php?id=<?echo $id?>" method="POST">
<table>
<tr><td align="right">Recipe Name:</td>
<td><input type="textbox" name="rname"></td></tr>
<tr><td align="right">Description:</td>
<td><textarea name="desc" cols="40" rows="4"></textarea></td></tr>
<tr><td align="right">Prep time:</td>
<td><input type="textbox" name="ptime"></td></tr>
<tr><td align="right">Total Time:</td>
<td><input type="textbox" name="ttime"></td></tr>
<tr><td></td>
<td><input type="submit" value="Add Recipe"></td></tr>
</table>
</form>


<hr />
<!--Now this following line of code will be used to return to previous page-->
<p> </p>
<form method="get" ACTION="PetersRecipeDB.php">
<input type="submit" value="Back"> Click here to view new added recipe AFTER you have inputted new recipe data
</form>


</body>
</html>


Can someone tell me why this is not working properly (THE ECHO $id) after i display this msg: echo "New Recipe: (" , $rname, ") has been added to the database! "; //pass new recipe

FOUND IN THE if ($_POST) statement

djm0219
10-04-2011, 01:13 PM
mysql_insert_id() is set AFTER the insert has taken place. If that is an auto increment column in your table, which it looks like it may be, you do not want to set it to any value when doing the insert. You may retrieve its value after the insert however if you need/want it for some reason.

At the point where you echo the name and ID the information has not been added yet.



$result = mysql_query( $sql )
or die( "Insert failed ($sql): " . mysql_error() );

$id = mysql_insert_id();
echo "New Recipe: (" , $rname, ") has been added to the database! "; //pass new recipe
echo $id;



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum