2015-11-02 80 views
1

我遇到了更新包含HTML数据的我的MySQL数据的问题,我不断修正错误;但是,一旦一个错误得到解决,就会给出另一个错误。当前的错误如下:PHP包含关键字/保留字的MySQL查询

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 'desc='Live updates to certain games will also be posted on this website througho' at line 1

我一直在清除堆栈溢出了近3天没有任何明确的答案。所以我希望有人能找到这个!

这里是我的PHP表单代码:

if (isset($_POST['submit'])) { 
    $WName = mysql_prep($_POST['wname']); 
    $SName = mysql_prep($_POST['sname']); 
    $Desc = mysql_prep($_POST['desc']); 
    $LogoURL = mysql_prep($_POST['logourl']); 
    $aboutPage = mysql_prep($_POST['aboutpage']); 
    $query = "UPDATE settings SET name='$WName',subName='$SName',desc='$Desc',logoUrl='$LogoURL',about='$aboutPage'"; 
    // $query = mysql_prep($query); 
    mysql_query($query) or die(mysql_error()); 
    header("Location: settings.php?=success"); 
} 

功能

mysql_prep()
可以在互联网,即在这里找到: https://gist.github.com/ZachMoreno/1504031

下面是HTML表单:

<form role="form" action="" method="post"> 
    <!-- text input --> 
    <div class="form-group"> 
     <label>Website Name</label> 
     <input type="text" name="wname" class="form-control" placeholder=" 
      <?php echo $row['name']; ?>" value=" 
      <?php echo $row['name']; ?>" /> 
     </div> 
     <div class="form-group"> 
      <label>Sub Name</label> 
      <input type="text" name="sname" class="form-control" placeholder=" 
       <?php echo $row['subName']; ?>" value=" 
       <?php echo $row['subName']; ?>" /> 
      </div> 
      <div class="form-group"> 
       <label>Description</label> 
       <textarea name="desc" class="form-control" rows="3" placeholder=" 
        <?php echo $row['desc']; ?>" > 
        <?php echo $row['desc']; ?> 
       </textarea> 
      </div> 
      <div class="form-group"> 
       <label>Logo URL</label> 
       <input type="text" name="logourl" class="form-control" placeholder=" 
        <?php echo $row['logoUrl']; ?>" value=" 
        <?php echo $row['logoUrl']; ?>" /> 
       </div> 
       <div class="form-group"> 
        <label>About Page</label> 
        <textarea class="form-control" name="aboutpage" rows="6" placeholder=" 
         <?php echo $row['about']; ?>"> 
         <?php echo $row['about']; ?> 
        </textarea> 
       </div> 
       <div class="box-footer"> 
        <input type="submit" name="submit" class="btn btn-primary" value="Submit" style="margin-left:-10px;" /> 
       </div> 
      </form> 

谢谢非常希望你能提供任何帮助,我希望能够解决这个问题,并且我的目标是用来帮助未来的访问者相同/相似的问题。

+0

应该使用mysqli_函数和mysqli_real_escape_string()函数,而不是旧的,不推荐使用的mysql_函数。 – SyntaxLAMP

+0

你可以var_dump($ query)'并发布结果 – MaggsWeb

+0

如何使用'htmlspecialchars()'和'mysqli_real_escape_string()'? – Thamilan

回答

1

不敢相信我以前没有看到过;我在MySQL中遇到的问题是数据库的列名是'desc',我原来的想法是'description',但实际上它与关键字'descending'冲突。这给了语法错误。

这是我在MySQL文档中找到的; 9.3 Keywords and Reserved Words

Keywords are words that have significance in SQL. Certain keywords, such as SELECT, DELETE, or BIGINT, are reserved and require special treatment for use as identifiers such as table and column names. This may also be true for the names of built-in functions.

在该网站链接上面你可以看到的不应该使用或应当包括反斜杠(我不会去)关键字/保留字列表。

我的解决方案?不要使用保留字作为标识符!

您可以做的最简单的解决方案就是避免使用这些单词。我通过将标识符更改为'description'来防止使用保留字'desc'。

感谢您的帮助!希望这能在未来帮助人们。

0

从mysql_prep()函数返回的字符串已经转义了单引号。 所以.. ..你不能在你的查询字符串中使用这些作为分隔符。将它们更改为双引号。

$query = "UPDATE settings SET name = \"$WName\", 
           subName = \"$SName\", 
           desc = \"$Desc\", 
           logoUrl = \"$LogoURL\", 
           about = \"$aboutPage\" "; 

你可以尝试$testQuery只有文字..

$testQuery = "UPDATE settings SET name = \"ABC\", 
            subName = \"DEF\", 
            desc = \"GHI\", 
            logoUrl = \"JKL\", 
            about = \"MNO\" "; 

而且,你缺少一个WHERE条款,或者是有唯一的1行?

+0

解析错误:语法错误,意外'$ WName '(T_VARIABLE)in ...“在$查询行中。 – Lachie

+0

好的,请使用转义双引号 – MaggsWeb

+0

“您的SQL语法错误; .......'desc =附近'某些游戏的实时更新也会在此网站上发布第3行”另一个SQL语法错误(我将错误减少了一点,但与原始文章中的原始错误相同) – Lachie