Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2

Thread: Add new record

  1. #1
    New Coder
    Join Date
    Sep 2011
    Posts
    78
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Add new record

    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:
    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

  • #2
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,313
    Thanks
    4
    Thanked 206 Times in 203 Posts
    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.

    PHP Code:
    $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
    Dave .... HostMonster for all of your hosting needs


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •