-3

I need help with this query. If I use this query in MySQL that works fine but is not being executed when used in PHP.

$sql = "INSERT INTO dtable (name, mobile, email) VALUES ('".$MP_Name."', '".$MP_Mobil."','".$MP_Email."');";

$conn->query($sql);

Anil
  • 61
  • 1
  • 1
  • 7

3 Answers3

1

Use prepared statements to avoid sql injection.

$conn = new mysqli($servername, $username, $password, $dbname);

$stmt = $conn->prepare("INSERT INTO dtable (name, mobile, email) VALUES (?, ?, ?)");
$stmt->bind_param("sss", $MP_Name, $MP_Mobil, $MP_Email);
$stmt->execute();
$stmt->close();
Ivan86
  • 5,695
  • 2
  • 14
  • 30
0

Try to echo the query string before executing it and copy/paste that echoed query in phpmyadmin and check for errors in the query

 $result= $sql->query(sprintf("INSERT INTO dtable(name,mobile,email) VALUES ('%s','%s','%s')", ($_POST['MP_Name']), ($_POST['MP_Mobil']), ($_POST['MP_Email'])));
 echo $sql;
0

With more study, I came to know that I was actually getting an error; commands out of sync; you can't run this command now.

I added $conn->next_result() before running the query which solved the issue.

Anil
  • 61
  • 1
  • 1
  • 7