Jump to content

mysqli_query not working (insertion into database)


_Akash

Recommended Posts

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 conncetion
successful 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!

 

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.