Php 在撇号进入数据库时反斜杠

Php 在撇号进入数据库时反斜杠,php,mysql,Php,Mysql,解决了问题的前一部分,但仍然需要理解为什么这不起作用:) 返回到单独转义字符串,我有这个 mysql_query("INSERT INTO users (firstname,surname,email,password,birthday,birthmonth,birthyear,houseno,streetname,town,country,postcode,phonenumber,singer,songwriter,producer,composer,band,instrument,i

解决了问题的前一部分,但仍然需要理解为什么这不起作用:)

返回到单独转义字符串,我有这个

    mysql_query("INSERT INTO users (firstname,surname,email,password,birthday,birthmonth,birthyear,houseno,streetname,town,country,postcode,phonenumber,singer,songwriter,producer,composer,band,instrument,instrument2,extra,confirmcode) VALUES ('".$firstname."','".$surname."','".$email."','".$password."',".$birthday.",".$birthmonth.",".$birthyear.",'".$houseno."','".$streetname."','".$town."','".$country."','".$postcode."',".$phonenumber.",".$singer.",".$songwriter.",".$producer.",".$composer.", ".$band.",'".$instrument."','".$instrument2."','".$extra."','".$rand."')",
    mysql_real_escape_string($surname),
    mysql_real_escape_string($firstname),
    mysql_real_escape_string($stown),
    mysql_real_escape_string($houseno),
    mysql_real_escape_string($streetname),
    mysql_real_escape_string($extra),
    mysql_real_escape_string($email)) or die ('pood'); 
    echo $streetname;
我得到了die错误-因此它不回显$streetname;(应该是“Clark’s Way”),并且似乎没有反斜杠撇号,因为它没有被输入数据库

如果我没有按照你的标准进行研究,我深表歉意,但我一直在努力理解为什么这几个小时都不起作用


谢谢:)

错误是由反斜杠引号引起的。删除反斜杠,它看起来应该工作

之前:
值(\'Joshua\'、\'Oakley\'、

之后:
VALUES('Joshua','Oakley',
mysql\u query()
不接受那样的多个参数。您需要转义查询字符串中的每个变量。如下所示:

... VALUES ('".mysql_real_escape_string($firstname)."','".mysql_r...
所以,你最终会得到这样的结果:

$query = "INSERT INTO users (firstname,surname,email,password,birthday,
birthmonth,birthyear,houseno,streetname,town,country,postcode,
phonenumber,singer,songwriter,producer,composer,band,instrument,
instrument2,extra,confirmcode) VALUES (
'". mysql_real_escape_string($firstname) ."',
'". mysql_real_escape_string($surname) ."',
'". mysql_real_escape_string($email) ."',
'". mysql_real_escape_string($password) ."',
'". mysql_real_escape_string($birthday) ."',
'". mysql_real_escape_string($birthmonth) ."',
'". mysql_real_escape_string($birthyear) ."',
'". mysql_real_escape_string($houseno) ."',
'". mysql_real_escape_string($streetname) ."',
'". mysql_real_escape_string($town) ."',
'". mysql_real_escape_string($country) ."',
'". mysql_real_escape_string($postcode) ."',
'". mysql_real_escape_string($phonenumber) ."',
'". mysql_real_escape_string($singer) ."',
'". mysql_real_escape_string($songwriter) ."',
'". mysql_real_escape_string($producer) ."',
'". mysql_real_escape_string($composer) ."',
'". mysql_real_escape_string($band) ."',
'". mysql_real_escape_string($instrument) ."',
'". mysql_real_escape_string($instrument2) ."',
'". mysql_real_escape_string($extra) ."',
'". mysql_real_escape_string($rand) ."')";

mysql_query($query) or die (mysql_error()); 
对不起,如果有错别字,但我想你明白了


下面是一个(未经测试的)参数化查询如何工作的示例,根据以下示例改编:


对每个输入值进行转义,而不是对整个查询进行转义。更好的是,将参数化查询与一起使用,而不必麻烦转义。验证所有int字段是否都由“”包围,并且其他字段是否由“”包围:D我将查看参数化查询。谢谢。我的朋友(谁比我更擅长使用这些东西)将单独的escape_字符串更改为这个大字符串,因为它们工作不正常。我现在知道是什么导致了它,所以我可以尝试解决它。使用参数化查询的第一个原因是为了避免SQL注入。第二个原因是为了避免您现在所经历的痛苦。是的,我找到了这个,有没有办法更改$quer你想停止在开头和结尾的撇号上加反斜杠吗?:)@Joshua'J-Dawg'Oakley我会看看是否能用一个参数化查询(准备好的语句)来制作一个例子,向你展示它的样子。记住这都是未经测试的,但它应该能给你一个大致的想法。:-)谢谢你花了这么多时间来做,我试着自己把它打出来,只转义相关的(不是整数),但我得到了某种语法错误(由于某种原因我的服务器上没有打开),然后我试着复制你写的东西,我得到了同样的问题。我将尝试撤消所有操作并一次添加一个转义字符串time@Joshua“J-Dawg”Oakley我应该在这里呆一会儿,所以如果你想继续讨论这个问题,那你呢,这样我们就不会把这页上的评论弄得乱七八糟了。谢谢你,你这个漂亮的男人!当我只在$streetname上使用mysql\u real\u escape\u字符串时,我会尝试一个接一个地添加更多变量,看看它们是否有效。编辑:对不起,没有看到你的评论,但很有道理^^
$query = "INSERT INTO users (firstname,surname,email,password,birthday,
birthmonth,birthyear,houseno,streetname,town,country,postcode,
phonenumber,singer,songwriter,producer,composer,band,instrument,
instrument2,extra,confirmcode) VALUES (
:firstname, :surname, :email, :password, :birthday, :birthmonth,
:birthyear, :houseno, :streetname, :town, :country, :postcode, :phonenumber,
:singer, :songwriter, :producer, :composer, :band, :instrument, :instrument2,
:extra, :rand)";

// assume $dbh is your PDO database connection
$sth = $dbh->prepare($query);
$sth->bindParam(':firstname', $firstname);
$sth->bindParam(':surname', $surname);
$sth->bindParam(':email', $email);
$sth->bindParam(':password', $password);
$sth->bindParam(':birthday', $birthday);
// and so on...

$sth->execute();