Jago6060 Posted October 1, 2007 Share Posted October 1, 2007 I'm trying to have my php script insert into 2 tables at once, it's for a picture database. Is it actually possible to insert into 2 tables at once? If so what would the sql statement look like. If not, any suggestions would be a great help. Thanks! Quote Link to comment Share on other sites More sharing options...
trq Posted October 1, 2007 Share Posted October 1, 2007 Is it actually possible to insert into 2 tables at once? Not from within php. You'll need to execute two seperate queries. Quote Link to comment Share on other sites More sharing options...
jaymc Posted October 1, 2007 Share Posted October 1, 2007 Of course $querya = "INSERT QUERY HERE FOR TABLE 1"; $queryb = "INSERT QUERY HERE FOR TABLE 2"; mysql_query($querya); mysql_query($queryb); As for the query.. you will need to write that yourself Quote Link to comment Share on other sites More sharing options...
Jago6060 Posted October 1, 2007 Author Share Posted October 1, 2007 I tried doing two queries but it only executed the first query for some reason. Quote Link to comment Share on other sites More sharing options...
trq Posted October 1, 2007 Share Posted October 1, 2007 Post your code. Quote Link to comment Share on other sites More sharing options...
Jago6060 Posted October 1, 2007 Author Share Posted October 1, 2007 $sql = "INSERT INTO feature_images (img_dir)values('$target_path')"; $result=mysql_query($sql); This is the current code without trying to execute two insert statements. I don't know if it matters but this code is within an upload loop Quote Link to comment Share on other sites More sharing options...
trq Posted October 1, 2007 Share Posted October 1, 2007 Post the relevent code (that isn't working). We can't help if we can't see what your doing! Quote Link to comment Share on other sites More sharing options...
jaymc Posted October 1, 2007 Share Posted October 1, 2007 If its in a loop...feature_images needs to be a variable, it its static, its only ever going to insert to one table Make $tablename dynamic in relation to what ever your loop is pullin out $sql = "INSERT INTO $tablename (img_dir)values('$target_path')"; $result=mysql_query($sql); Quote Link to comment Share on other sites More sharing options...
Jago6060 Posted October 1, 2007 Author Share Posted October 1, 2007 <? include 'connect.php'; // Where the file is going to be placed $target_path = "./uploads/images/"; /* Add the original filename to our target path. Result is "/uploads/images/filename.extension" */ $target_path = $target_path . basename( $_FILES['uploadedfile']['name']); $_FILES['uploadedfile']['tmp_name']; if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded"; @mysql_select_db($database) or die( "Unable to select database"); $sql = "INSERT INTO feature_images (img_dir)values('$target_path')"; $result=mysql_query($sql); mysql_close(); header ('Location: logout.php'); }else{ echo "There was an error uploading the file, please try again!"; echo "<a href=upload_feature_form.php>Back</a>"; } ?> heres the full script Quote Link to comment Share on other sites More sharing options...
jaymc Posted October 1, 2007 Share Posted October 1, 2007 Like I said, turn feature_images into a variable which will contain your table name Using the loop, it needs to switch from feature_images to your other table name If it looks twice thats 2 seperate inserts into 2 seperate tables providing the table names have been set Quote Link to comment Share on other sites More sharing options...
trq Posted October 1, 2007 Share Posted October 1, 2007 I don't see anywhere in that code where you attempt to make to consecutive queries. Also, you never actually check your query succeeds or not. Quote Link to comment Share on other sites More sharing options...
Jago6060 Posted October 1, 2007 Author Share Posted October 1, 2007 $feature = "feature_images"; $images = "crc_images"; $sql = "INSERT INTO $feature (img_dir)values('$target_path'),INSERT INTO $images (img_dir)values('$target_path')"; $result=mysql_query($sql); would something like this work? Quote Link to comment Share on other sites More sharing options...
jaymc Posted October 1, 2007 Share Posted October 1, 2007 No Just stick with what you have, but copy and paste the query code and change the table name Refer to my first post Cant get much simpler Quote Link to comment Share on other sites More sharing options...
Jago6060 Posted October 1, 2007 Author Share Posted October 1, 2007 ok, sorry to be a pain but I just wanted to clarify this. The format I should use is... $feature = "feature_images"; $images = "crc_images"; $sql = "INSERT INTO $feature (img_dir)values('$target_path')"; $sql2 = "INSERT INTO $images (img_dir)values('$target_path')"; $result=mysql_query($sql); $result=mysql_query($sql2); right? Quote Link to comment Share on other sites More sharing options...
jaymc Posted October 1, 2007 Share Posted October 1, 2007 Correct, if your not putting them in a loop, no need to even make them variables I only said that because I thought it was in a loop if your just placing it in the script, you can statically assign the table names to the query No big deal though, what you have above will work fine Quote Link to comment Share on other sites More sharing options...
Jago6060 Posted October 1, 2007 Author Share Posted October 1, 2007 it works, thanks alot Quote Link to comment 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.