sfia Posted April 29, 2020 Share Posted April 29, 2020 $username = $_POST['uid']; $email = $_POST['mail']; $password = $_POST['pwd']; $passwordRepeat = $_POST['pwd-repeat']; $date = $_POST['date2']; $stream = $_POST['relationship']; $sql1 = "INSERT INTO users (uidUsers, emailUsers, pwdUsers, relationship) VALUES (?, ?, ?, ?);"; $sql2 = "INSERT INTO Family1 (username, application_filed, relationship) VALUES (?, ?, ?);"; $sql3 = "INSERT INTO Family2 (username, application_filed, relationship) VALUES (?, ?, ?);"; mysqli_query($sql1, $conn); mysqli_query($sql2, $conn); mysqli_query($sql3, $conn); $stmt = mysqli_stmt_init($conn); if (!mysqli_stmt_prepare($stmt, $sql2)) { header("Location: ../signup.php?error=sqlerror"); exit(); } else { mysqli_stmt_bind_param($stmt, "sss", $username, $date, $stream); $result = mysqli_stmt_get_result($stmt); if ($row = mysqli_fetch_assoc($result)) ($username==$_SESSION['uid'] and $stream =='nursing'); mysqli_stmt_execute($stmt); } if (!mysqli_stmt_prepare($stmt, $sql3)) { header("Location: ../signup.php?error=sqlerror"); exit(); } else { mysqli_stmt_bind_param($stmt, "sss", $username, $date, $stream); $result = mysqli_stmt_get_result($stmt); if ($row = mysqli_fetch_assoc($result)) ($username==$_SESSION['uid'] and $stream =='doctoral'); mysqli_stmt_execute($stmt); } if (!mysqli_stmt_prepare($stmt, $sql1)) { header("Location: ../signup.php?error=sqlerror"); exit(); } if (!mysqli_stmt_prepare($stmt, $sql1)) { header("Location: ../signup.php?error=sqlerror"); exit(); } else { $hashedPwd = password_hash($password, PASSWORD_DEFAULT); mysqli_stmt_bind_param($stmt, "ssss", $username, $email, $hashedPwd, $stream); mysqli_stmt_execute($stmt); header("Location: ../signup.php?signup=success"); exit(); I was wondering if someone could point me in the right direction. I have this code. They idea I had behind it is to insert values into different tables depending on variables being passed. So when user fills out a form and selects $stream="nursing" I want results to go to table 'users' and 'Family1', but not 'Family2' table. and if user selects $stream='doctoral' results should go to table 'users' and 'Family2', and not go to 'Family1' But with my query I get results go to both table and also users table. And there is no restriction to what users selects, variable $stream being passed no matter what it is. Is this the wrong way to go here? Did I completely mess up the logic? Quote Link to comment https://forums.phpfreaks.com/topic/310664-inserting-values-to-different-tables-based-on-variables/ Share on other sites More sharing options...
gw1500se Posted April 29, 2020 Share Posted April 29, 2020 This code is confusing (please select PHP for your code next time). It appears you are running the query before you bind parameters. I don't seen how this can ever work. Quote Link to comment https://forums.phpfreaks.com/topic/310664-inserting-values-to-different-tables-based-on-variables/#findComment-1577340 Share on other sites More sharing options...
sfia Posted April 29, 2020 Author Share Posted April 29, 2020 <?php $username = $_POST['uid']; $email = $_POST['mail']; $password = $_POST['pwd']; $passwordRepeat = $_POST['pwd-repeat']; $date = $_POST['date2']; $stream = $_POST['relationship']; $sql1 = "INSERT INTO users (uidUsers, emailUsers, pwdUsers, relationship) VALUES (?, ?, ?, ?);"; $sql2 = "INSERT INTO Family1 (username, application_filed, relationship) VALUES (?, ?, ?);"; $sql3 = "INSERT INTO Family2 (username, application_filed, relationship) VALUES (?, ?, ?);"; mysqli_query($sql1, $conn); mysqli_query($sql2, $conn); mysqli_query($sql3, $conn); $stmt = mysqli_stmt_init($conn); if (!mysqli_stmt_prepare($stmt, $sql2)) { header("Location: ../signup.php?error=sqlerror"); exit(); } else { mysqli_stmt_bind_param($stmt, "sss", $username, $date, $stream); $result = mysqli_stmt_get_result($stmt); if ($row = mysqli_fetch_assoc($result)) ($username==$_SESSION['uid'] and $stream =='nursing'); mysqli_stmt_execute($stmt); } if (!mysqli_stmt_prepare($stmt, $sql3)) { header("Location: ../signup.php?error=sqlerror"); exit(); } else { mysqli_stmt_bind_param($stmt, "sss", $username, $date, $stream); $result = mysqli_stmt_get_result($stmt); if ($row = mysqli_fetch_assoc($result)) ($username==$_SESSION['uid'] and $stream =='doctoral'); mysqli_stmt_execute($stmt); } if (!mysqli_stmt_prepare($stmt, $sql1)) { header("Location: ../signup.php?error=sqlerror"); exit(); } if (!mysqli_stmt_prepare($stmt, $sql1)) { header("Location: ../signup.php?error=sqlerror"); exit(); } else { $hashedPwd = password_hash($password, PASSWORD_DEFAULT); mysqli_stmt_bind_param($stmt, "ssss", $username, $email, $hashedPwd, $stream); mysqli_stmt_execute($stmt); header("Location: ../signup.php?signup=success"); exit(); ?> sorry, i added php tags to make it better readable. My queries work with parameters that I set up. <?php mysqli_stmt_bind_param($stmt, "sss", $username, $date, $stream); ?> But i dont know how to make it work with that condition before executing query. perhaps I'm not using right logic here? <?php ($username==$_SESSION['uid'] and $stream =='nursing'); mysqli_stmt_execute($stmt); ?> Quote Link to comment https://forums.phpfreaks.com/topic/310664-inserting-values-to-different-tables-based-on-variables/#findComment-1577341 Share on other sites More sharing options...
gw1500se Posted April 29, 2020 Share Posted April 29, 2020 I still don't understand this mysqli_query($sql1, $conn); mysqli_query($sql2, $conn); mysqli_query($sql3, $conn); when you haven't yet bound the variables. Quote Link to comment https://forums.phpfreaks.com/topic/310664-inserting-values-to-different-tables-based-on-variables/#findComment-1577342 Share on other sites More sharing options...
sfia Posted April 29, 2020 Author Share Posted April 29, 2020 ok, I get that part. I'm not sure why I was doing it I think i just thought I was preparing a statement. I actually removed all three mysqli_query and my query still works. But don't I bind my variables by doing this? ---> mysqli_stmt_bind_param($stmt, "sss", $username, $date, $stream); I thought my sql queries just prepared statements with placeholders(?, ?, ?) Quote Link to comment https://forums.phpfreaks.com/topic/310664-inserting-values-to-different-tables-based-on-variables/#findComment-1577346 Share on other sites More sharing options...
gw1500se Posted April 29, 2020 Share Posted April 29, 2020 read this to see how to prepare and execute a query. Quote Link to comment https://forums.phpfreaks.com/topic/310664-inserting-values-to-different-tables-based-on-variables/#findComment-1577347 Share on other sites More sharing options...
sfia Posted April 30, 2020 Author Share Posted April 30, 2020 ok with some help i got it working and doing what i want. <?php $stmt = mysqli_stmt_init($conn); if (!mysqli_stmt_prepare($stmt, $sql2)) { header("Location: ../signup.php?error=sqlerror"); exit(); } else if ($stream =='nursing') { mysqli_stmt_bind_param($stmt, "sss", $username, $date, $stream); mysqli_stmt_execute($stmt); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/310664-inserting-values-to-different-tables-based-on-variables/#findComment-1577351 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.