I want to automatically place the ID from the Parent table into a child table using PHP prepared statements? - Hack The Tech - Latest News related to Computer and Technology

Hack The Tech - Latest News related to Computer and Technology

Get Daily Latest News related to Computer and Technology and hack the world.

Thursday, July 13, 2023

I want to automatically place the ID from the Parent table into a child table using PHP prepared statements?

I have two separate tables each will input the form data into the phpmyadmin Db respectively.

Users and Parents respectively

I am using procedural PHP and a prepared statement for form data. I have had success with each form inserting data into the Db just fine, however when it comes to the usersId from the users table being inserted into the Parents table user_id (fk) I only receive NULL in the database. I then can manually update that columnn with the appropriate data from a drop down list in PHPmyadmin.

The fact that I can get the information into the tables is great but I want the usersID automatically inserted into the parents table in the user_Id column.

Here is the prepared statement that I am using.

<?php

if (isset($_POST["submit"])) {
$mFname = $_POST["mFname"];
$mLname = $_POST["mLname"];
$mEmail = $_POST["mEmail"];
$mPhone = $_POST["mPhone"];

$fFname = $_POST["fFname"];
$fLname = $_POST["fLname"];
$fEmail = $_POST["fEmail"];
$fPhone = $_POST["fPhone"];

$addressL1 = $_POST["addressL1"];
$addressL2 = $_POST["addressL2"];
$city = $_POST["city"];
$stateAbbr = $_POST["stateAbbr"];
$zip = $_POST["zip"];
$id = $_POST['user_id'];


include_once 'dbh.inc.php';
include_once 'functions.inc.php';
 
$sql = "INSERT INTO parent (mFname, mLname, mEmail, mPhone, fFname, fLname, fEmail, fPhone, addressL1, addressL2, city, stateAbbr, zip, user_id) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?);";

$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)) {
    echo "SQL error";
} else {
    mysqli_stmt_bind_param($stmt, "ssssssssssssis", $mFname, $mLname, $mEmail, $mPhone, $fFname, $fLname, $fEmail, $fPhone, $addressL1, $addressL2, $city, $stateAbbr, $zip, $user_id);
    mysqli_stmt_execute($stmt);

}

$sql1 = "UPDATE parent INNER JOIN users ON parent.user_id = users.usersId SET parent.user_id =     users.usersId";

    if (mysqli_query($conn, $sql1)) {
    echo "Record updated successfully";
    } else {
    echo "Error updating record: " . mysqli_error($conn);
    }

    header("Location: ../mf2.php?parentinfo=success");
}`

enter image description here

The image above shows that the data makes it into the table however as the image shows the user_Id column will display NULL. I need the information from the users table, specifically the usersID column to show in the parents table under the user_Id column automatically on submit.

Please help and talk to me as though I am just learning this because I am. The more detailed you are the better. Thank you all in advance for your help with this.

I tried to use an inner join and found that it says that updating the record was successful at one point but when I checked the Db I found that wasn't the case.

I have tried using a the usersId session variable in the prepared statement that was unsuccessful.

I am not sure what I am doing wrong here but any help would be greatly appreciated.



source https://stackoverflow.com/questions/76673625/i-want-to-automatically-place-the-id-from-the-parent-table-into-a-child-table-us

No comments:

Post a Comment