can someone help me with this, Im using a simple form to add user into a database, my goal is to add the user into one table, but at the same time add the (userid) into a different table, the user id is made automatically by the database, it is possible to do this ?
Thank you
<?php
require("coneccion.php");
if(!empty($_POST))
{
$query = "INSERT INTO students (fname, lname, studentcode) values (:fname, :lname, :studentcode)";
$query_params = array(':fname' => $_POST['fname'], ':lname' => $_POST['lname'], ':studentcode' => $_POST['studentcode']);
try
{
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch(PDOException $ex)
{
die("Error" . $ex->getMessage());
}
$name = $_SESSION['user']['username'];
$id = $_SESSION['courseid'];
$query = "INSERT INTO idtables(courseid, studentid) values ('$id', (SELECT id FROM users WHERE username = '$name'))";
try
{
$stmt = $db->prepare($query);
$stmt->execute();
}
catch(PDOExecute $ex)
{
die("Error" . $ex->getMessage());
}
$id = $_SESSION['courseid'];
header("Location: index.php?id=$id");
die("Rendirecting to index.php?id=$id");
}
?>
<?php
$id = $_SESSION['courseid'];
echo $id;
?>
<form action="add.php" method="post">
First Name: <input type="text" name="fname" value=""><br>
Last Name: <input type="text" name="lname" value=""><br>
Stude Code: <input type="text" name="studentcode" value=""><br>
<input type="submit" value="Add Student">
</form>