0

I am trying to update one of two tables within my DB, based upon the results of a variable. Essentially, the variable is either "a" or "b", based upon the form selection. I define this variable as $t. I have defined the other variables as well. Here is the code:

$t = $_POST['t']; //(defined in javascript from form selection)
$a = "a";
$b = "b";
//Create own rows in table based upon $t type
if ($t = $a){
    $sql = "INSERT INTO user_a (id, email, user_type, name, create_time) 
            VALUES ('$db_id','$e','$a','$n', now())";
    $query = mysqli_query($db_conx, $sql);
} else {
    $sql = "INSERT INTO user_b (id, email, user_type, name, create_time) 
            VALUES ('$db_id','$e','$b','$n', now())";   
    $query = mysqli_query($db_conx, $sql);
exit();
}

I have some coding before this, however I don't think that is the issue, as I can update both tables. I can only update table "a", and not table "b". Appreciate the help.

1 Answer 1

1

You are assigning a value to $t instead of comparing the variable values, so your if statement will allways evaluate to true.

Change

if ($t = $a)

to

if ($t == $a)

As a obligatory sidenote, your query is wide wide open to sql injection, you should use prepared statements instead.

Sign up to request clarification or add additional context in comments.

4 Comments

I just tried that and it still is not working. This is driving me insane. I can't see how this doesn't work. I have checked syntax and spelling 1000x and that's not it. I guess I am more frustrated that it will update "a", but not "b". I tried switching the SQL commands around ("b" before "a") as well as 'if ($t == $v)', but it doesn't work.
And sorry, I am learning and just am frustrated that I can't figure this out. Thanks for the help and I appreciate your assistance.
can you please echo out $t before the if. How sure are you that it isn't a?
GOT IT! I did have some code that I think may have caused it, but also, I was using an apostrophe in the name for "b" and it wasn't uploading to the database. THANK YOU MICHAEL!!!! By the way, why is it I cannot upload an apostrophe to the database? I have VARCHAR selected. Is there another format to use for capturing text that covers more characters?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.