skip to Main Content

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);

3

Answers


  1. Chosen as BEST ANSWER

    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.


  2. 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();
    
    Login or Signup to reply.
  3. 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;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search