Simple Insert Variable PHP into MySQL -


i have simple bit of code can't working.

<?php $mysqli_connection = new mysqli('localhost', 'root', 'secret', 'edgeserver'); if ($mysqli_connection->connect_error) {    echo "not connected, error: " . $mysqli_connection->connect_error;     $username = 'eddie';    $username = mysql_real_escape_string($username);    $email = 'eddie_the_eagle@hotmail.com';    $email = mysql_real_escape_string($email);     $sql = "insert  `users` (`username`, `email`)             values ( '".$username."', '".$email."')";    $res = $mysqli_connection->query($sql); }  ?> 

when run code no error appears users table remains empty.

there 2 problems:-

  1. you mixing mysql_* mysqli_*

  2. no error checking done.

try this:-

<?php      $mysqli_connection = new mysqli('localhost', 'root', 'secret', 'edgeserver');     if ($mysqli_connection->connect_error)     {         echo "not connected, error: " . $mysqli_connection->connect_error;     }//change     $username = 'eddie';     $username = mysqli_real_escape_string($mysqli_connection,$username);//connection link must provided first parameter     $email = 'eddie_the_eagle@hotmail.com';     $email = mysqli_real_escape_string($mysqli_connection,$email); //same here      $sql = "insert  users (username, email) values ( '".$username."', '".$email."')";      $res = $mysqli_connection->query($sql); ?> 

note:-please habitat yourselves use error reporting when going stuff. thanks.


Comments