1

This is the error currently show. Below The first code I save as details.php and the second on as details.html

Warning: Undefined array key "first_name" in C:\xampp\htdocs\children_math\detail2.php on line 12

Warning: Undefined array key "last_name" in C:\xampp\htdocs\children_math\detail2.php on line 13

Warning: Undefined array key "email" in C:\xampp\htdocs\children_math\detail2.php on line 14

Warning: Undefined array key "age" in C:\xampp\htdocs\children_math\detail2.php on line 15 ERROR: Could not able to execute INSERT INTO details (first_name, last_name, email, age) VALUES ('', '', '', ''). Cannot add or update a child row: a foreign key constraint fails (record.details, CONSTRAINT details_ibfk_1 FOREIGN KEY (id) REFERENCES users (id))

/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("localhost", "root", "", "record");
 
// Check connection
if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}
 
// Escape user inputs for security
$first_name = mysqli_real_escape_string($link, $_REQUEST['first_name']);
$last_name = mysqli_real_escape_string($link, $_REQUEST['last_name']);
$email = mysqli_real_escape_string($link, $_REQUEST['email']);
$age = mysqli_real_escape_string($link, $_REQUEST['age']);
 
// Attempt insert query execution
$sql = "INSERT INTO details (first_name, last_name, email, age) VALUES ('$first_name', '$last_name', '$email', '$age')";
if(mysqli_query($link, $sql)){
    echo "Records added successfully.";
} else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
 
// Close connection
mysqli_close($link);
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Add Record Form</title>
</head>
<body>
<form action="detail2.php" method="post">
    <p>
        <label for="firstName">First Name:</label>
        <input type="text" name="first_name" id="firstName">
    </p>
    <p>
        <label for="lastName">Last Name:</label>
        <input type="text" name="last_name" id="lastName">
    </p>
    <p>
        <label for="emailAddress">Email Address:</label>
        <input type="text" name="email" id="emailAddress">
    </p>
    <p>
        <label for="ages">Email Address:</label>
        <input type="text" name="age" id="ages">
    </p>
    <input type="submit" value="Submit">
</form>
</body>
</html>```
peterh
  • 11,875
  • 18
  • 85
  • 108
Kimi Zul
  • 31
  • 1
  • 1
  • 3

2 Answers2

4

i had a similar problem; i solved it by checking the file referenced in the error and the line. In your case its details2.php.In my case that error was triggered because the variable names $variable_name = $_POST['key_value']; were initialized outside my if(isset($_POST['Register'])) block. All i did was include it immediately after my if(condition)block like so;

if (isset($_POST['Register'])) {
    
// receive all input values from the form
$name = mysqli_real_escape_string($conn, $_POST['name']);
$Username = mysqli_real_escape_string($conn, $_POST['username']);
$phone_number = mysqli_real_escape_string($conn, $_POST['phone_number']);
$email =  mysqli_real_escape_string($conn, $_POST['email']);
$Church =  mysqli_real_escape_string($conn, $_POST['church']);
$Password1 = mysqli_real_escape_string($conn, $_POST['password']); //encrypt password
$Password2 = mysqli_real_escape_string($conn, $_POST['passwordConf']);
$token = bin2hex(random_bytes(50)); // generate unique token

// form validation: ensure that the form is correctly filled ... // by adding (array_push()) corresponding error unto $errors array

    if (empty($_POST['name'])) {
        $errors['name'] = 'Name required';
    }
    ....
Sesugh01
  • 49
  • 4
0

Ensure that your form has the method attribute set to post

<form action="your_page.php" method="post">
  <label for="email">Email :</label>
  <input type="text" id="email" name="emailInput"><br><br>
</form>

At your_page.php, you can get the value by $email = $_POST['emailInput'];

The string(emailInput) inside the $_POST['emailInput']; is the name of your input tag.

Tan Yi Jing
  • 295
  • 1
  • 8
  • CREATE TABLE users( id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, username VARCHAR(50) NOT NULL UNIQUE, password VARCHAR(255) NOT NULL, created_at DATETIME DEFAULT CURRENT_TIMESTAMP ); CREATE TABLE details( id INT NOT NULL PRIMARY KEY, first_name VARCHAR(30) NOT NULL, last_name VARCHAR(30) NOT NULL, email VARCHAR(70) NOT NULL UNIQUE, age VARCHAR(10) NOT NULL ); my table is like this Mr Tan, i want to link with users table. – Kimi Zul Feb 08 '21 at 08:12
  • I can't find any issue with the coding above hence the only problem might be the linkage between your web application and the DB (PhpMyAdmin) – Tan Yi Jing Feb 08 '21 at 08:14