PNewCode Posted April 17, 2023 Share Posted April 17, 2023 Hello I have a working form that sends info to another page and enters it in the database. What I am trying to do is add $sql2 ="UPDATE bandstage SET jkredeem ='used' WHERE mybandid = $user_id"; to the bit below I'm not including the form or the rest of the database connection because all of that is working with no issues and I think it will just clutter up the question. So the object is to keep what is working below, and also update the column "jkredeem" from the table "bandstage" to "used" (which "bandstage" is a different table in the same database) If there's any question why I wrote it the way I did below, the answer is this is how I know to do it while I'm still learning. Thats the only reason I can offer :) $sql = "INSERT into redeemjk (mybandid,audio,title,bandname,contactname,email) VALUES('$mybandid','$song','$title','$bandname','$contactname','$email')"; if(mysqli_query($conn,$sql)){ echo " "; } else{ echo "Error"; } Quote Link to comment https://forums.phpfreaks.com/topic/316147-how-to-updat-2-tables-in-one-database-within-one-form/ Share on other sites More sharing options...
Solution ginerjm Posted April 17, 2023 Solution Share Posted April 17, 2023 (edited) You will have to write a separate query to update the other table. Be sure to check the first query is successful before doing the second one. And read up on how to do a prepared query since you are inserting user-generated data into your table which could corrupt your database. And - BTW - you would be better off if you learned to use the PDO interface instead of MySqlI. If you have access to it. Edited April 17, 2023 by ginerjm 1 Quote Link to comment https://forums.phpfreaks.com/topic/316147-how-to-updat-2-tables-in-one-database-within-one-form/#findComment-1607405 Share on other sites More sharing options...
PNewCode Posted April 18, 2023 Author Share Posted April 18, 2023 @ginerjm Thank you. That helped a lot. I'm at number 60 in fails at adding a 2nd query and making it work, but now I know that it's what I have to do to make that work. Quote Link to comment https://forums.phpfreaks.com/topic/316147-how-to-updat-2-tables-in-one-database-within-one-form/#findComment-1607406 Share on other sites More sharing options...
ginerjm Posted April 18, 2023 Share Posted April 18, 2023 And why haven't you posted any of these efforts? Quote Link to comment https://forums.phpfreaks.com/topic/316147-how-to-updat-2-tables-in-one-database-within-one-form/#findComment-1607408 Share on other sites More sharing options...
PNewCode Posted April 18, 2023 Author Share Posted April 18, 2023 Because I want to try more before I ask for more help. I was just letting you know that what you replied sent me on the path to knowing what to do so thank you. I'll update if I get a solution or hit a brick wall trying Quote Link to comment https://forums.phpfreaks.com/topic/316147-how-to-updat-2-tables-in-one-database-within-one-form/#findComment-1607410 Share on other sites More sharing options...
PNewCode Posted April 18, 2023 Author Share Posted April 18, 2023 @ginerjm I got it. So from what you posted... I started to build up a second query but it was failing. Thats because I was just putting that in with the other one. I see that I had to have a complete new query (forgive my jargon, I haven't learned the right termonology let). So I replicated what I had, and renamed the second one, and put in my UPDATE command instead of the INSERT in the 2nd one. I did get some help on this outside of here to understand to rename it to have a difference. Thank you much for the help I appreciate it. Solution Quote Link to comment https://forums.phpfreaks.com/topic/316147-how-to-updat-2-tables-in-one-database-within-one-form/#findComment-1607413 Share on other sites More sharing options...
ginerjm Posted April 18, 2023 Share Posted April 18, 2023 Use the <> symbol to create a block for inserting code into a post. Makes for nice reading an it is clearer that's what it is. Why not sql1 and sql2 ? Quote Link to comment https://forums.phpfreaks.com/topic/316147-how-to-updat-2-tables-in-one-database-within-one-form/#findComment-1607414 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.