average american Posted September 12, 2007 Share Posted September 12, 2007 I need help this may be easy i don't know... I need to link my two table together. The first table(listings) has a row ID (primary and auto inc) my second table (images) has a row LISTING_ID. I cannot get the last id from the listings table to get inserted on to the images table. I have the part of my code below: mysql_query("LOCK TABLES listings, images WRITE"); mysql_query("INSERT INTO images (name, size, type, content ) ". "VALUES ('$fileName', '$fileSize', '$fileType', '$content')"); $result = mysql_query("SELECT MAX(ID) AS LAST_ID FROM listings"); $id = mysql_fetch_array($result); mysql_query("UNLOCK TABLES"); echo "Last ID#" . $id[LAST_ID]; echo "<br>File $fileName uploaded<br>"; the images load and i am able to see the ID via the echo "Last ID#" .$id[LAST_ID'] part of the code. But, nomater what I have tried i cannot get that id to be posted in the listing_id of the images table. Quote Link to comment https://forums.phpfreaks.com/topic/68950-inserting-an-id-from-one-table-to-another/ Share on other sites More sharing options...
kireol Posted September 12, 2007 Share Posted September 12, 2007 where are you inserting into your listings table? Quote Link to comment https://forums.phpfreaks.com/topic/68950-inserting-an-id-from-one-table-to-another/#findComment-346596 Share on other sites More sharing options...
average american Posted September 12, 2007 Author Share Posted September 12, 2007 from another page.... i would like to have this all on the same page but it was way to confusing. this is the code for the listings <?php if (isset($_REQUEST['Submit'])) { # THIS CODE TELL MYSQL TO INSERT THE DATA FROM THE FORM INTO YOUR MYSQL TABLE $address=mysql_real_escape_string($_POST['address']); $city=mysql_real_escape_string($_POST['city']); $state=mysql_real_escape_string($_POST['state']); $bed=mysql_real_escape_string($_POST['bed']); $bath=mysql_real_escape_string($_POST['bath']); $garage=mysql_real_escape_string($_POST['garage']); $rent=mysql_real_escape_string($_POST['rent']); $description=mysql_real_escape_string($_POST['description']); $zip=mysql_real_escape_string($_POST['zip']); $sql = "INSERT INTO listings(address, city, state, bed, bath, garage, rent, description, zip) values ('$address', '$city', '$state', '$bed', '$bath', '$garage', '$rent', '$description', '$zip')"; if($result = mysql_query($sql , $db)) { echo "Thank you, Your information has been entered into our database"; echo "<a href= '$config_basedir./database entry/addingimages.php'>add images</a>"; } else { echo "ERROR: ".mysql_error(); } } else { ?> <form method="post" action=""> <p>Address: <input name="address" type="text" size="50" maxlength="100"> </p> <p><br> City: <input name="city" type="text" size="30" maxlength="30"> State: <input name="state" type="text" size="2" maxlength="2"> Zip: <input name="zip" type="text" size="6" maxlength="5"> </p> <p><br> Bed: <input name="bed" type="text" size="4" maxlength="1"> Bath: <input name="bath" type="text" size="4" maxlength="3"> Garage: <input name="garage" type="text" size="4" maxlength="1"> Rent: $ <input name="rent" type="text" size="6" maxlength="4"> </p> <p> <br> Description: <br> <textarea name="description" cols="100" rows="5"></textarea> <br> <br> <input type="submit" name="Submit" value="Submit"> </p> </form> <p> Quote Link to comment https://forums.phpfreaks.com/topic/68950-inserting-an-id-from-one-table-to-another/#findComment-346600 Share on other sites More sharing options...
kireol Posted September 12, 2007 Share Posted September 12, 2007 well, you arent explicitly insertng the ID field. do you have phpmyadmin or something to verify that that field has data like you expect? Quote Link to comment https://forums.phpfreaks.com/topic/68950-inserting-an-id-from-one-table-to-another/#findComment-346602 Share on other sites More sharing options...
average american Posted September 12, 2007 Author Share Posted September 12, 2007 i do have phpmyadmin and that is true it is not get inserted. (that is the listing_id) the listings table is auto id so it is automaticly inserted into the table. i just can not figure out how to get that auto inserted id into the images table and into the listing_id. Quote Link to comment https://forums.phpfreaks.com/topic/68950-inserting-an-id-from-one-table-to-another/#findComment-346609 Share on other sites More sharing options...
redarrow Posted September 12, 2007 Share Posted September 12, 2007 you need to use $_GET in the url off the link page.php?id=$id or use a session Quote Link to comment https://forums.phpfreaks.com/topic/68950-inserting-an-id-from-one-table-to-another/#findComment-346615 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.