Click here to Skip to main content
15,850,921 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have to two tables

users table:
SQL
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:

SQL
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:

HTML
<form action="" method="post" autocomplete="off" >
       <!-- Second row -->
       <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>

         <!-- Third row -->
         <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:

PHP
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:

PHP
$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?
Posted
Updated 6-Feb-23 0:54am

1 solution

Your error is created because you have never posted the user_id to your form but a reference were made to it.

The easiets way for this is to store your user info in a session which can be called upon anytime during their active session on any page it is required.

Read more about sessions, creating a session, closing a session etc from THIS PHP manual.

Some basic sample code usage will look like this -
<?php
// page1.php

session_start();

echo 'Welcome to page #1';

$_SESSION['favcolor'] = 'green';
$_SESSION['animal']   = 'cat';
$_SESSION['time']     = time();

// Works if session cookie was accepted
echo '<br /><a href="page2.php">page 2</a>';

// Or maybe pass along the session id, if needed
echo '<br /><a href="page2.php?' . SID . '">page 2</a>';
//After viewing page1.php, the second page page2.php will magically contain the session data.
?>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900