Jump to content

how to updat 2 tables in one database within one form?


PNewCode
Go to solution Solved by ginerjm,

Recommended Posts

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";
    }

 

Link to comment
Share on other sites

  • Solution

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 by ginerjm
  • Like 1
Link to comment
Share on other sites

@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


image.png.85b0ad9910551743c30d31d1a99e7d3d.png

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.