2015-11-02 85 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