简体   繁体   中英

PHP INSERT query syntax

I am working on a site to make DnD character sheets for my friends and I. The first part is done and data is POST-ing correctly but will not upload to server. I have worked on this problem for along time and still cannot figure it out. All of the stuff is spelled the same as server and the server is connected. The error I get with mysqli_error() is:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server for the right syntax to use near'character(id, username, characterName, class, level, race, alignment, deity, si' at line 1

I put echo before the problem to make sure everything is working before uploaded and everything is working fine but will not upload. The $bd is connection and is working because i use it with other pages.

This is my code:

echo ($id . "<br />");
echo ($username . "<br />");
echo ($characterName . "<br />");
echo ($class . "<br />");
echo ($level . "<br />");
echo ($race . "<br />");
echo ($alignment . "<br />");
echo ($deity . "<br />");
echo ($size . "<br />");
echo ($age . "<br />");
echo ($gender . "<br />");
echo ($height . "<br />");
echo ($weight . "<br />");
echo ($eyes . "<br />");
echo ($hair . "<br />");
echo ($skin . "<br />");
echo ($mysql_hostname . "<br />");
echo ($mysql_user . "<br />");
echo ($mysql_password . "<br />");
echo ($mysql_database . "<br />");

if(isset($_POST['nc1']))

{
$query    =    "INSERT INTO characters (id, username, characterName, class, level, race,  alignment, deity, size, age, gender, height, weight, eyes, hair, skin) VALUES ('$id', '$username', '$characterName', '$class', '$level', '$race', '$alignment', '$deity', '$size',     '$age', '$gender', '$height', '$weight', '$eyes', '$hair', '$skin')";
mysqli_query($bd,$query) or die('Error: ' . mysqli_error($bd));
//header('location:success.php');//Redirect To Success Page
echo ("success");
}

character is a reserved word in mysql. If you use this as a tablename, you must quote that:

$query = "INSERT INTO `character` ( `id`, `username`, ....

As a good practice and in order to prevent such faults, always quote table or column names.

这里说您的表名character是保留字,因此您需要更改它,否则您的代码将无法工作。

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