0

I'm trying to insert data into a database but couldn't. Below is my code

<?php

$servername = "localhost";
$username = "root";
$password = "";
$database =  "routine maker";

$conn = mysqli_connect($servername, $username, $password, $database);

if(!$conn){
    die("Sorry we failed to connect: ". mysqli_connect_error());
}else{
    echo '<script>alert("Connection was successful");</script>';
}

$sql = `INSERT INTO check (teacherName, subject , class) VALUES ("skg", "Doe", 4)`;
if ($conn->query($sql) === TRUE) {
  echo "New record created successfully";
} else {
  echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>

i was expecting the data to be inserted.

ADyson
  • 57,178
  • 14
  • 51
  • 63
  • 1
    `line 61` The PHP you have posted does not have 61 lines, can you highlight what line 61 is? – Keith Jan 03 '23 at 15:06
  • On this line, `$sql = \`INSERT INTO check (teacherName, subject , class) VALUES ("skg", "Doe", 4)\`;` the backticks need to be single-quotes. It's a string, not a shell command. Please always double-check the basics of your work carefully. – ADyson Jan 03 '23 at 15:07
  • try something like this 'INSERT INTO `check` (`teacherName`,`subject` , `class`) VALUES ("skg", "Doe", 4)' – Plamen Penchev Jan 03 '23 at 15:11
  • In PHP strings should be inside single- or double- quotes. Backtick (`) is not a valid syntax for strings. – Ilia Yatsenko Jan 03 '23 at 15:43

0 Answers0