小编典典

“组”列以某种方式导致语法错误

sql

我不断收到此错误:

您的SQL语法有误;检查与您的MySQL服务器版本相对应的手册,以找到在第1行的“ …”附近使用的正确语法。

我知道我的“组”字段有问题,因为如果删除它,我的表将被更新而不会出现任何问题。

这是我的表单代码:

<form enctype="multipart/form-data" action="products_import_confirmation.php" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="99999999" />
<center><table><br><br>
<td>Prek臈s pavadinimas:     
<td><input type="text" name="name_p" value="<?php echo $name_p;?>">
<span class="error">* <?php echo $name_pErr;?></span></td>
<tr>
<td>Gamintojas:<td><input type="text" name="brand" value="<?php echo $brand;?>">
<span class="error">* <?php echo $brandErr;?></span></td>
<tr>
<td>Specifikacijos:<td><textarea style="resize:none" name="specs" rows="5" cols="60"><?php echo $specs;?></textarea>
<span class="error">* <?php echo $specsErr;?></span></td>
<tr>
<td>Apra拧ymas:<td><textarea style="resize:none" name="about" rows="5" cols="60"><?php echo $about;?></textarea>
<tr>
<td>Kategorija:<td><select name="group" value="<?php echo $group;?>">
<option value="">Pasirinkite kategorij膮</option>
<option disabled>------------------</option>
<option value="Telefonai">Telefonai</option>
<option value="Plan拧et臈s">Plan拧et臈s</option>
<option value="Kompiuteriai">Kompiuteriai</option>
</select>
<span class="error">* <?php echo $groupErr;?></span></td>
<td><div><input name="image" accept="image/jpeg" type="file" /></div></td>
<tr>
</center></table>                               
<td><div><input type="submit" value="Submit" /></div></td>
</form>

这是我对MySQL代码的更新:

<?php
require("/includes/connection_to_db.php");

mysql_query("SET NAMES 'utf8'");

session_start();
echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>";

if(isset($_GET['logout']) && $_GET['logout'] == "true"){
unset($_SESSION['user_id']);
header("Location: index.php");
}
if (isset($_FILES['image']) && $_FILES['image']['size'] > 0) 
    {
        // Temporary file name_p stored on the server
        $tmpname_p = $_FILES['image']['tmp_name'];

        // Read the file
        $fp = fopen($tmpname_p, 'r');
        $image = fread($fp, filesize($tmpname_p));
        $image = addslashes($image);
        fclose($fp);

        date_default_timezone_set('Europe/Vilnius');
        $V_ID = mysql_real_escape_string($_SESSION['user_id']);
        $name_p = mysql_real_escape_string($_POST['name_p']);
        $group = mysql_real_escape_string($_POST['group']);
        $brand = mysql_real_escape_string($_POST['brand']);
        $specs = mysql_real_escape_string($_POST['specs']);
        $about = mysql_real_escape_string($_POST['about']);
        $date = date('Y-m-d H:i:s');

        $query = "INSERT INTO prek臈s (V_ID, name, group, brand, specs, about, image, date)
                VALUES ($V_ID, '$name_p', $group, '$brand', '$specs', '$about', '$image', '$date')";

        if(mysql_query($query))
        {

            echo "Prek臈 ".$name." s臈kmingai 寞kelta <br>";
            echo "<a href=\"../main.php\">Gr寞啪ti</a>";
        }
        else
        {

            echo "漠vyko klaida: ".mysql_error()."<br>";
            echo "<a href=\"../products_import.php\">Gr寞啪ti</a>";
            echo $date;
            echo $name_p;
            echo $brand;
            echo $specs;
            echo $about;
            echo $group;
        }
    }
    else 
    {
        echo "Neu啪pildyti visi reikalaujami duomenys.";
    }

mysql_close();
?>

如您所见,我做了一些回显,以检查是否获取了我的数据,并且一切正常。“分组”数据只会使事情变糟。有任何想法吗?


阅读 202

收藏
2021-04-14

共1个答案

小编典典

group 是保留关键字,它用于查询。

您需要对保留字使用backtics作为

`group`

在这里检查他们的名单

https://dev.mysql.com/doc/refman/5.5/zh-CN/reserved-
words.html

2021-04-14