_Akash Posted March 22, 2013 Share Posted March 22, 2013 hello, I have just started working with php and encountered a problem in handling the MySQL database through my php script. the code is as follows: _______________________________________ <?php echo "successful!!"; $first=$_POST['first']; $last=$_POST['last']; $email=$_POST['email']; $db= mysqli_connect('localhost','root', 'akash123','akash') or die('Error in connecting to the database'); echo "</br> successful conncetion"; $query= "insert into info (first, last, email) values ($first,$last,$email)"; echo "</br> successful query"; $result= mysql_query($db,$query) or die("Error in insertion"); echo "</br> successful insertion"; mysqli_close($db); echo "the email ID of ".$first." ".$last." is ".$email; ?> _______________________________________ And the output is as follows: successful!!successful conncetionsuccessful queryError in query I understand that all the steps prior to mysqli_query function are functioning properly. The connection to the database is established, but it cant write into it.Please help! Quote Link to comment https://forums.phpfreaks.com/topic/276034-mysqli_query-not-working-insertion-into-database/ Share on other sites More sharing options...
_Akash Posted March 22, 2013 Author Share Posted March 22, 2013 I have created a table "info" into my "akash" database. I have manually tried to populate the database, which works. But the same is not being done by the script. Quote Link to comment https://forums.phpfreaks.com/topic/276034-mysqli_query-not-working-insertion-into-database/#findComment-1420384 Share on other sites More sharing options...
moltm4785 Posted March 22, 2013 Share Posted March 22, 2013 Your issue is either here $result= mysql_query($db,$query) or die("Error in insertion"); normally you should have the below: $mysqli = new mysqli("localhost", "USERNAME", "PASSWORD", "DATABASE"); $query = $mysqli -> query("insert into info (first, last, email) values ('$first','$last','$email')"); OR $query= "insert into info (first, last, email) values ($first,$last,$email)"; you are stating "($first,$last,$email)" this should be: ('$first', '$last', '$email') Quote Link to comment https://forums.phpfreaks.com/topic/276034-mysqli_query-not-working-insertion-into-database/#findComment-1420393 Share on other sites More sharing options...
haku Posted March 23, 2013 Share Posted March 23, 2013 Please use code tags around your code. It's the <> button in the editor. Quote Link to comment https://forums.phpfreaks.com/topic/276034-mysqli_query-not-working-insertion-into-database/#findComment-1420458 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.