Vincent Omondi Vincent Omondi - 5 months ago 17
HTML Question

Error when inserting data in mysql using php insert.php

I am having a challenge inserting data. Everything checks out...i.e connection to database. The data cant just insert. am using local host xampp.
My insert.php

<?php
$servername = "localhost";
$username = "root";
$password = "Maweutah2";
$dbname = "digital_content";
$link = mysqli_connect($servername, $username, $password, $dbname );
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}

// Escape user inputs for security
$ref = mysqli_real_escape_string($link, $_REQUEST['ref']);
$date = mysqli_real_escape_string($link, $_REQUEST['date']);
$name = mysqli_real_escape_string($link, $_REQUEST['name']);
$class = mysqli_real_escape_string($link, $_REQUEST['class']);
$serial = mysqli_real_escape_string($link, $_REQUEST['serial']);
$details = mysqli_real_escape_string($link, $_REQUEST['details']);
$remarks = mysqli_real_escape_string($link, $_REQUEST['remarks']);

// attempt insert query execution
$sql = "INSERT INTO laptop_repair (ref, name, class, serial, details, remark) VALUES ('$ref', '$date', '$name', '$class', '$serial', '$details', '$remarks')";
if(mysqli_query($link, $sql)){
echo "<h2>Records added successfully<a href='laptop_repairs.php'>ENTER AGAIN</h2></a>.";
} else{
echo "<h2>ERROR: TRY AGAIN <a href='db.php'>ENTER AGAIN</h2></a>";
}

// close connection
mysqli_close($link);
?>


My form

<!DOCTYPE html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<div class="center">
<h1>ENTER DETAILS</h1>
<form action="insert.php" method="POST">
<h3>REF NUMBER</h3><input type="text" name="ref" /><br>
<h3>DATE</h3><input type="text" name="date" /><br>
<h3>NAME</h3><input type="text" name="name" /><br>
<h3>CLASS</h3><input type="text" name="class" /><br>
<h3>SERIAL</h3><input type="text" name="serial" /><br>
<h3>DETAILS</h3><input type="text" class="inputmingi" name="details" /><br>
<h3>REMARKS</h3><input type="text" class="inputmingi" name="remarks" /><br>
<input type="submit" />

</form>

</div>
<div class="center">
<h1>MENU</h1>
<form action="view.php" method="POST">
<button type="submit">View records</button>
</form>
</div>
</html>


When I submit, the connection is successful but no data inserted in my table.

Answer Source

update this

$sql = "INSERT INTO laptop_repair (ref, date, name, class, serial, details, remark) VALUES ('$ref', '$date', '$name', '$class', '$serial', '$details', '$remarks')";

you are forget to insert date value. you haven't mentioned in the query.