i have to two tables
users table:
CREATE TABLE `users` (
`id` int(11) NOT NULL,
`fname` varchar(255) NOT NULL,
`lname` varchar(255) NOT NULL,
`username` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL,
`password` varchar(255) NOT NULL,
`phone` int(11) NOT NULL,
`role` enum('admin','user') NOT NULL DEFAULT 'user',
PRIMARY KEY (`id`),
UNIQUE KEY `email` (`email`)
|id |fname | lname| username | email | password | phone |
----- ------ ------ ---------- ------------------ ---------- ----------
| 123| john | bale | john22 | john@email.com | hashed | 0123456 |
| 20 | mike |Taylor| mike123 | mike@email.com | hashed | 456789 |
cars table:
CREATE TABLE `cars` (
`vid` int(11) unsigned NOT NULL,
`user_id` int(11) DEFAULT NULL,
`model` varchar(255) NOT NULL,
`reg_number` int(20) unsigned NOT NULL,
`year` int(10) unsigned NOT NULL,
`color` varchar(128) NOT NULL,
PRIMARY KEY (`vid`),
UNIQUE KEY `reg_number` (`reg_number`),
KEY `user_car_id` (`user_id`),
CONSTRAINT `user_car_id` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)
| vid | user_id | model | reg_number | year | color |
------ --------- -------- ------------- ------ -------
| 555 | NULL | toyota | 258 | 2000 | white |
| 099 | NULL | mazda | 852 | 2015 | gray |
what i want is, when the user 'for example' john his id=123 submit the car form his id appear in the cars table in the column user_id.
here is the car form in html:
<form action="" method="post" autocomplete="off" >
<!--
<div class="row pt-4">
<div class="col-3">
<label for="vehicle" class="form-label">Vehicle-ID:</label>
<input type="number" class="form-control" name="vid" id="vehicle" >
</div>
<div class="col-3">
<label for="model" class="form-label">Vehicle Model:</label>
<input type="text" class="form-control" name="model" id="model" placeholder="E.g. Toyota" >
</div>
<div class="col-3">
<label for="registration" class="form-label">Registration Number:</label>
<input type="number" class="form-control" name="reg_number" id="registration" >
</div>
</div>
<!--
<div class="row">
<div class="col-3">
<label for="year" class="form-label">Year of Production:</label>
<input type="number" class="form-control" name="year" id="year" >
</div>
<div class="col-3">
<label for="color" class="form-label">Vehicle Color:</label>
<input type="text" class="form-control" name="color" id="" >
</div>
</div>
<div class="row">
<div class="col-12 text-center">
<button type="submit" class="btn btn-light my-3">
Confirm
</button>
</div>
</div>
</div>
</form>
and here's the php insertion data code:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$vid = mysqli_real_escape_string($mysql, $_POST['vid']);
$model = mysqli_real_escape_string($mysql, $_POST['model']);
$reg_number = mysqli_real_escape_string($mysql, $_POST['reg_number']);
$year = mysqli_real_escape_string($mysql, $_POST['year']);
$color = mysqli_real_escape_string($mysql, $_POST['color']);
if (empty($vid)) {
array_push($errors, "vehicle id is required!");
}
if (empty($model)) {
array_push($errors, "vheicle model is required!");
}
if (empty($reg_number)) {
array_push($errors, "registeration number is required!");
}
if (empty($year)) {
array_push($errors, "year of production is required!");
}
if (empty($color)) {
array_push($errors, "vheicle color is required!");
}
if (!count($errors)) {
$carExists = $mysql->query("select vid, reg_number from cars where vid ='$vid' limit 1 ");
if ($carExists->num_rows) {
array_push($errors, "vheicle id already registed!");
}
}
if (!count($errors)) {
$carExists = $mysql->query("select vid, reg_number from cars where reg_number ='$reg_number' limit 1 ");
if ($carExists->num_rows) {
array_push($errors, "registeration number already registed!");
}
$carq = "insert into cars (vid, model, reg_number, year, color) values ('$vid','$model','$reg_number','$year', '$color')";
$mysql->query($carq);
$_SESSION['logged_in'] = true;
$_SESSION['car_id'] = $mysql->insert_id;
$_SESSION['success_message'] = "car submitted successfully";
print_r($_SESSION['car_id']);
header('location: carformm.php');
die();
}
What I have tried:
i tried this query but didn't work:
$carq = "insert into cars (vid, model, reg_number, year, color, user_id) values ('$vid','$model','$reg_number','$year', '$color','$_POST[user_id]')";
$mysql->query($carq);
this is the error:
Warning**: Undefined array key "user_id" in **C:\\xampp\\htdocs\\flex-tut\\Carformm.php** on line **53**
>
> **Fatal error**: Uncaught mysqli_sql_exception: Cannot add or update a child row: a foreign key constraint fails (\`carcare\`.\`cars\`, CONSTRAINT \`user_car_id\` FOREIGN KEY (\`user_id\`) REFERENCES \`users\` (\`id\`)) in C:\\xampp\\htdocs\\flex-tut\\Carformm.php:54 Stack trace: #0 C:\\xampp\\htdocs\\flex-tut\\Carformm.php(54): mysqli-\>query('insert into car...') #1 {main} thrown in **C:\\xampp\\htdocs\\flex-tut\\Carformm.php** on line **54**
what can i do to make it work?