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!

 
Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.