Php 转义字符问题
我有一个动态添加文本框的代码Php 转义字符问题,php,Php,我有一个动态添加文本框的代码我的问题是,当我使用转义字符,即(')或(“)并在php脚本中发布数据时,这是可以的,但当我将此数据插入mysql时,它会给我错误警告:mysql\u real\u escape\u string()希望参数1是字符串,数组在第123行的/var/www/book/books.php中给出 row_no=0; function addRow(tbl,row){ row_no++; if (row_no<=20){ if (row_no>=10){ var te
我的问题是,当我使用转义字符,即(')或(“)并在php脚本中发布数据时,这是可以的,但当我将此数据插入mysql时,它会给我错误
警告:mysql\u real\u escape\u string()希望参数1是字符串,数组在第123行的/var/www/book/books.php中给出
row_no=0;
function addRow(tbl,row){
row_no++;
if (row_no<=20){
if (row_no>=10){
var textbox = '';}
if (row_no<10){
var textbox = '';}
var textbox2 = '';
var tbl = document.getElementById(tbl);
var rowIndex = document.getElementById(row).value;
var newRow = tbl.insertRow(row_no);
var newCell = newRow.insertCell(0);
newCell.innerHTML = textbox;
var newCell = newRow.insertCell(1);
newCell.innerHTML = textbox2;
var newCell = newRow.insertCell(2);
}
if (row_no>20){
alert ("Too Many Items. Limit of 20.");
}
$amount是一个数组。mysql\u real\u escape\u string需要一个字符串而不是数组,这就是为什么您会收到错误消息。您可能需要遍历$amount数组并为数组的每个成员调用mysql\u real\u escape\u string。我认为错误消息表明,mysql\u escape\u string需要的第一个参数是字符串而不是arr嗯
Warning: mysql_real_escape_string() expects parameter 1 to be string, array given in /var/www/book/books.php on line 123
如果要转义整个数组,可以使用,应执行以下操作:
$ingredient = $_POST['ingredient'];
$amount = $_POST['amount'];
$integer = 0;
$ingredient = array_map('mysql_real_escape_string', $ingredient);
$amount = array_map('mysql_real_escape_string', $amount);
while (count($ingredient)>$integer) {
if (($ingredient[$integer] <> "") && ($amount[$integer] <> "")){
$sql = "INSERT INTO cafe.ingredients (ingredient_name, ammount, rec_id)
VALUES ('".$ingredient[$integer]."', '".$amount[$integer]."', $rec_id)" ;
echo "the echo value is".$ingredient[$integer]."and the error is below
";
mysql_query($sql) or die(mysql_error());
}
else{
echo "ingredient number ".($integer+1)." is missing values and cannot be inserted.";
}
$integer = ($integer + 1);
}
$component=$\u POST['component'];
$amount=$_POST['amount'];
$integer=0;
$component=array\u map('mysql\u real\u escape\u string',$component);
$amount=array\u map('mysql\u real\u escape\u string',$amount);
while(计数($component)>$integer){
如果(($component[$integer]“”)和($amount[$integer]“”){
$sql=“插入cafe.contracents(配料名称、数量、记录id)
值(“$Component[$integer]。”、“$amount[$integer]。”、$rec_id)”;
echo“echo值为“..$component[$integer]”,错误如下
";
mysql_query($sql)或die(mysql_error());
}
否则{
echo“配料编号”。($integer+1)。“缺少值,无法插入。”;
}
$integer=($integer+1);
}
感谢您的回复,但这会告诉我您的SQL语法有错误;请查看与您的MySQL服务器版本相对应的手册,以了解第2RageZ thnks行中使用接近“s”、“64”)的语法的正确性。现在非常有效!!但在我的代码中还有一个问题,您可以看到它动态生成两个文本框(成分、数量),如果我想添加另一个动态文本框,即指令,那么我是否应该为javascript中的指令创建另一个函数…?@hunter:很抱歉,我的回答是肯定的,您应该再添加几行javascript来实现这一点
Warning: mysql_real_escape_string() expects parameter 1 to be string, array given in /var/www/book/books.php on line 123
$ingredient = $_POST['ingredient'];
$amount = $_POST['amount'];
$integer = 0;
$ingredient = array_map('mysql_real_escape_string', $ingredient);
$amount = array_map('mysql_real_escape_string', $amount);
while (count($ingredient)>$integer) {
if (($ingredient[$integer] <> "") && ($amount[$integer] <> "")){
$sql = "INSERT INTO cafe.ingredients (ingredient_name, ammount, rec_id)
VALUES ('".$ingredient[$integer]."', '".$amount[$integer]."', $rec_id)" ;
echo "the echo value is".$ingredient[$integer]."and the error is below
";
mysql_query($sql) or die(mysql_error());
}
else{
echo "ingredient number ".($integer+1)." is missing values and cannot be inserted.";
}
$integer = ($integer + 1);
}