【问题标题】:niceEDIT and PHP/MySQL special charsniceEDIT 和 PHP/MySQL 特殊字符
【发布时间】:2011-09-08 11:46:13
【问题描述】:

您好,我有一个带有文本区域的表单,其中包含描述性文本,其中包含标点符号,例如逗号等。

在我使用过的 PHP 脚本上

$description    = empty( $_POST['inputDescription'])? 'NULL': "'" .  mysql_real_escape_string($_POST['inputDescription']) . "'";

但在提交包含逗号的文本时,我仍然遇到语法错误,即 this..

您的 SQL 语法有误;检查与您的 MySQL 服务器版本相对应的手册,以了解在 's 附近使用正确的语法

任何想法都会很棒,我正在拔头发!

编辑大量代码(对不起)

<?php
    session_start();
    include "includes/connection.php";

    $contact        = $_POST['inputName'];
    $company    = $_POST['inputCompany'];
    $region             = $_POST['inputRegion'];
    $address1   = $_POST['inputAddress1'];
    $address2   = empty( $_POST['inputAddress2'])? 'NULL'   : "'" . mysql_real_escape_string(  $_POST['inputAddress2']) . "'";
    $city               = $_POST['inputCity'];
    $county             = empty( $_POST['inputCounty'])? 'NULL' : "'" . mysql_real_escape_string(  $_POST['inputCounty']) . "'";
    $postcode   = $_POST['inputPostcode'];
    $email          = empty( $_POST['inputEmail'])? 'NULL'  : "'" . mysql_real_escape_string(  $_POST['inputEmail']) . "'";
    $telephone1 = $_POST['inputPhoneOne'];
    $telephone2 = empty( $_POST['inputPhoneTwo'])? 'NULL'   : "'" . mysql_real_escape_string(  $_POST['inputPhoneTwo']) . "'";
    $website        = empty( $_POST['inputWebsite'])? 'NULL'    : "'" . mysql_real_escape_string(  $_POST['inputWebsite']) . "'";
    $description    = empty( $_POST['inputDescription'])? 'NULL': "'" .  mysql_real_escape_string($_POST['inputDescription']) . "'";
    $userid             = $_POST['inputUserID'];

    if(
    $contact == '' || 
    $company == '' ||  
    $address1 == '' || 
    $address2 == '' || 
    $city == '' || 
    $county == '' || 
    $postcode == '' ||  
    $telephone1 == '' || 
    $telephone2 == '' || 
    $email == '' || 
    $website == '' || 
    $description == '' || 
    $region == '' ||  
    $userid == ''){
    $_SESSION['status'] = 'error';
    } else {
        mysql_query("INSERT INTO RegionalContacts
                (`bID`,`user_id`,`Name`,`Company`,`Address1`,`Address2`,`City`,`County`,`Postcode`,`Telephone1`,`Telephone2`,`eMail`,`Website`,`Description`,`Region`)
VALUES(NULL,'$userid','$contact','$company','$address1',$address2,'$city',$county,'$postcode','$telephone1',$telephone2,$email,$website,$description,'$region')") or die(mysql_error());
        $_SESSION['status'] = 'success';
    }
    header("location: regionalContacts.php");
?>

【问题讨论】:

  • 显示执行 SQL 的代码。 (还有 SQL。)
  • 您可以附上准确的插入语句,而不是显示冗长的 PHP 脚本
  • 我应@nickc 的要求输入了这段代码,以便他有一切可以帮助,但感谢您的建议

标签: php mysql nicedit


【解决方案1】:

您的插入语句看起来像它的值(我假设)是传递给它的字符串,它们周围没有引号,例如地址 2、县、电话 2、电子邮件、网站和描述。这会导致语法错误。

【讨论】:

  • 感谢这些是必填字段,即不能为空,因此不能转义@piddl0r
  • 我认为你错过了我的观点。在您的 INSERT 语句中,您的字符串不包含在引号中,这将导致语法错误,无论该字段是否允许为空
  • 'sepcial' 字符不会导致插入问题,因为您已经将它们转义了。
【解决方案2】:

您的 INSERT 语句中的某些值有引号,而其他值则没有:

'$telephone1',$telephone2

使用准备好的语句来避免此类错误……

【讨论】:

  • 谢谢,任何人都可以回答这个问题,如果我的描述字段有'如何根据上面的代码将其提交给 MySQL 而不会出现错误?
  • 已回答:使用prepared statements!
猜你喜欢
  • 1970-01-01
  • 2013-03-26
  • 1970-01-01
  • 2010-10-12
  • 1970-01-01
  • 2015-05-08
  • 1970-01-01
  • 2015-12-31
  • 2015-05-23
相关资源
最近更新 更多