mysql - I keep getting "Error Querying Database" in PHP code -


looks i'm connecting server fine. problem seems happen when runs query. keeps saying

error querying database

here code:

 <?php  $dbc = mysqli_connect('localhost', 'elvis_store')       or die('error connecting mysql server.');   $first_name = $_post['firstname'];  $last_name = $_post['lastname'];  $email = $_post['email'];   $query = "insert email_list (first_name, last_name, email)" .       "values ('$first_name', '$last_name', '$email')";  mysqli_query($dbc, $query)       or die('error querying database.');   echo 'customer added.';   mysqli_close($dbc);  ?> 

you getting error because in mysqli connection give location , username. not give database name used. if have no password, need write connection this:

$dbc = mysqli_connect('localhost', 'elvis_store', null, 'dbname) 

or

$dbc = mysqli_connect('localhost', 'dbusername', null, 'elvis_store') 

if "elvis_store" database name , not username. remember, mysqli connection is: mysqli_connect(dblocation, dbusername, dbpassword, dbname).

also, ed has pointed out in answer, there syntax error in mysql statement. here snippet ed's answer:

$query = "insert email_list (first_name, last_name, email) " . "values ('$first_name', '$last_name', '$email')"; 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

dataset - MPAndroidchart returning no chart Data available -

java - No use of nillable="0" in SOAP Webservice -