-7

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.

  • And what is the error message you get? – Viktor Aug 17 '17 at 12:20
  • 2
    What do you mean by *"but no data inserted in my table."*? Does the `mysqli_query()` succeeds? If not, use [`mysqli_error()`](http://php.net/manual/en/mysqli.error.php) to find out why. – axiac Aug 17 '17 at 12:21
  • 4
    I see 6 fields and 7 values. – u_mulder Aug 17 '17 at 12:21
  • 2
    You should add error handling - and read the actual errors - for example by telling mysqli to throw exceptions. Then you will see that you are trying to insert 7 values in 6 fields. – jeroen Aug 17 '17 at 12:21
  • Your script is at risk of [SQL Injection Attack](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) Even [if you are escaping inputs, its not safe!](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) Use [prepared parameterized statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) – RiggsFolly Aug 17 '17 at 12:23
  • Add `ini_set('display_errors', 1); ini_set('log_errors',1); error_reporting(E_ALL); mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);` to the top of your script. This will force any `mysqli_` errors to generate an Exception that you can see on the browser and other errors will also be visible on your browser. – RiggsFolly Aug 17 '17 at 12:24
  • Thanks i noticed the fields and rectified. I only managed to insert a single row data and trying once more it was not successful – Vincent Omondi Aug 17 '17 at 12:34
  • Thanks alot. Have managed to rectify the error – Vincent Omondi Aug 17 '17 at 13:02

1 Answers1

0

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.

  • Yes, i have rectified it. I could only insert 1 row of data. Any other attempt is a fail. However the connection to the db is successful. – Vincent Omondi Aug 17 '17 at 12:58