在添加另一个要添加到数据库的值后,出现语法错误。我不知道问题是什么,我对 php 和 mysql 有点陌生。 我做错了什么?
ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''index',name,score) VALUES ('2','guyaa','300')' at line 1
这是插入的 php:
<?php
require_once 'app_config.php';
connect();
$index = $_GET['index'];
$name = $_GET['name'];
$score = $_GET['score'];
$query = "INSERT INTO scores (index,name,score) VALUES ('" . $index ."','" . $name ."','" . $score . "')";
$result = mysql_query($query) or die('ERROR: '. mysql_error());
这是检索:
<?php
require_once 'app_config.php';
connect();
$query = "SELECT * From scores";
$result = mysql_query($query);
while($item = mysql_fetch_array($result)){
echo $item['index'];
echo ' - ';
echo $item['name'];
echo ' - ';
echo $item['score'];
echo '<br>';
}
最佳答案
索引是 MySQL reserved word 。您需要使用反引号
$query = "INSERT INTO scores (`index`,name,score) VALUES ('" . $index
."','" . $name ."','" . $score . "')";
关于php - MySql php 获取和插入语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31790273/