php 验证mysql保留字_PHP MySQL查询包含关键字/保留字

bd96500e110b49cbb3cd949968f18be7.png

I have encountered an issue with updated my MySQL data which includes HTML data, I continuously fixed errors; however, once one error is fixed it gives another. The current error is as follows:

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

I have been scavenging on Stack Overflow for nearly 3 days without any definitive answers. So I am hoping someone can find this!

Here is my PHP form code:

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");

}

The functionmysql_prep() can be found on the internet, namely here: https://gist.github.com/ZachMoreno/1504031

Here is the HTML form:

Website Name

Sub Name

Description

Logo URL

About Page

Thanks very much for any assistance that you can provide, I hope this can be figured out and I aim to use this to assist future visitors who encounter the same/similar issues.

解决方案

Can't believe I didn't see this earlier; the issue I had with MySQL was that the database had the column name 'desc' which I originally had the idea that it meant 'description' but in fact it was conflicting with the keyword 'descending'. This gave the syntax error.

Here is what I found on the MySQL documentation; 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.

On that web link above you can see a list of keywords/reserved words that shouldn't be used or should include back slashes (which I won't go into).

My solution? Don't use reserved words as identifiers!

The easiest solution that you can do is to simply avoid using these words. I prevented using the reserved word 'desc' by changing the identifier to 'description'.

Thanks for all your help! Hope this assists people in the future.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值