简体   繁体   中英

MySQL INSERT error (does nothing)

Hi I'm pretty new to mysql and im trying to insert some data into a data base using INSERT the data is received from a HTML form

Inserting data

$q = "INSERT INTO 'customers'('firstname', 'lastname', 'email') VALUES ('$first, '$last', '$email')";
mysqli_query($dbc, $q);

Thats the INSERT function

Selecting data

$q = 'SELECT * FROM customers';
$r = mysqli_query($dbc, $q)
OR die
(mysqli_error());

echo 'customers';

while($row = mysqli_fetch_array ($r, MYSQL_ASSOC))
{
echo '<br />' .$row['firstname']. $row['lastname']. $row['email'];
}

Thats the SELECT function and how I'm displaying the database on a php page

No matter what tutorial or forum i look in i cant seem to work out why i can't add to or show the results!?

Don't use quotes to escape column or table names, use backticks instead. Quotes are string delimiters.

INSERT INTO `customers`(`firstname`, `lastname`, `email`) 
VALUES ('$first', '$last', '$email')

And you should really consider using Prepared Statements to prevend SQL injections. But you absolutely must escape your user input. Otherwise your input can produce syntax errors too.

And as pointed out in the other answer - you did miss a quote too in your first parameter.

Try with this...you have problem with the $first you have missed quote

 $q = "INSERT INTO customers(firstname, lastname, email) VALUES ('$first', '$last', '$email')";
 mysqli_query($q);

EDITED

By enclosing the column and table names quotes you are specifying them in string. Use the code below

$q = "INSERT INTO customers(firstname, lastname, email) 
VALUES ('$first', '$last', '$email')";
mysqli_query($dbc, $q);

Hope this helps you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM