“组"列以某种方式导致语法错误 [英] The 'group' column somehow causes a syntax error

查看:88
本文介绍了“组"列以某种方式导致语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不断收到此错误:

您的SQL语法有错误;请查看与您的MySQL服务器版本相对应的手册,以在第1行的"..."附近使用正确的语法.

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 '...' at line 1.

我知道我的组"字段有问题,因为如果删除它,我的表将被更新而没有任何问题.

I know that it is something wrong with my "group" field, because if I delete it my table gets updated without any problems.

这是我的表单代码:

<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代码的更新:

And here is my update to MySQL code:

<?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();
?>

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

As you see I made some echo's to check if I get my data and there is everything okay. It's only something bad going with "group" data. Any ideas?

推荐答案

group是保留关键字,它在查询中使用它.

group is a reserved key word and its used it the query.

对于保留字,您需要使用反斜线作为

You need to use backtics for reserved words as

`group`

在此处查看它们的列表

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

这篇关于“组"列以某种方式导致语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆