2

I have developed a software but suddenly got that the data insertion using INSERT INTO is not working. I dont know why it happened. Here is the code piece given below:

<?php

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "booking";

$conn = new mysqli($servername, $username, $password, $dbname);
if($conn->connect_error){die("Connection failed: ". $conn->connect_error);}

$sql = "INSERT INTO data (CNIC) VALUES ('37105458742-5')";


if(mysqli_query($conn, $sql) {echo("Success");}


?>

The database connection is successful. On the line 14 $sql = "INSERT..... gives error:

Parse error: syntax error, unexpected '{' in C:\xampp\htdocs\booking\index.php on line 14
Zeshan Sajid
  • 559
  • 3
  • 14
  • it not a INSERT error. it is syntax error. Please read the error message carefully and then resolve it. Modify your last statement as : if(mysqli_query($conn, $sql) ) { echo("Success"); } – Faiyaz Alam Feb 22 '16 at 06:01
  • Also don't mix object oriented approach with procedural approach. It will not give error but not a good practice – Alive to die - Anant Feb 22 '16 at 06:33

4 Answers4

2
if(mysqli_query($conn, $sql) {echo("Success");}

Should be

if(mysqli_query($conn, $sql)) {echo("Success");}

Missing a closing bracket.

Matt
  • 2,851
  • 1
  • 13
  • 27
0

You are missing the ending bracket of If condition:

This:

if(mysqli_query($conn, $sql) {echo("Success");}

Should be:

if(mysqli_query($conn, $sql)) {
    echo("Success");
}
devpro
  • 16,184
  • 3
  • 27
  • 38
0

Write your code as below:-

// You have Missed ')' bracket in below line
if(mysqli_query($conn, $sql)) {
  echo("Success");
}else{
  // Always check for errors
  printf("Error: %s\n", mysqli_error($conn));
}

Hope it will help you :)

Ravi Hirani
  • 6,511
  • 1
  • 27
  • 42
0

Just use below code

if ($conn->query($sql) === TRUE) {
  echo "success";
} else {
  echo "Error: " . $sql . "<br>" . $conn->error;
}

and remove below line from your code

if(mysqli_query($conn, $sql) {echo("Success");}

Dhaval Patel
  • 1,076
  • 6
  • 19