php - 空格和特殊字符更新查询不起作用,具有 50 个输入字段的 html 表单 47 是 int 没有问题,3 是文本/字符串问题

标签 php html mysql

运行 MySQL 版本 5.5.22 这里是我的代码、问题和尝试的解决方案。 我真的很困惑在哪里以及如何转义 3 个字符串字段的空格和特殊字符。我有多年的编程经验,但只使用了一个月的 PHP,我知道我需要从最近的搜索中习惯 PDO。我想让它在没有 PDO 的情况下工作(重新开始)。这是下一个项目的内容。

表单是这样的,它有 50 个字段,除了 3 个字符串(包括“comments”)外,都是 int 如果我删除字符串字段,一切都会正常,所以我遇到了空格和特殊字符的问题。我还使用几乎相同的代码通过 createTable.php 运行,并且运行良好。哦,仅供引用,我根本不担心注入(inject)...在内部网络中受到保护。

提交页面有这个

这用于使用数据库表中已有的内容自动填充 html 表单页面 //$Comments="\"".$rows['Comments'] ."\"";//并且它有效。

非常感谢任何帮助。 这是 update.php 上的代码。 评论中的大部分内容都是我迄今为止尝试过的。 ”; $ID=$_POST['ID'];

//******* put this text on the html page *******************

echo "This is what your update table query string will look like. for record ID=" . $_POST['ID'];
echo "</p>" ;
echo "The ID, pagetitle and Submit have been removed.";
echo "<p><hr>" ;

//********** Prepare the string **************

$query_string = "";
//if not post ie someone got here without post
if ($_POST) {
  $kv = array();
//loop thru submitted field-value keys
  foreach ($_POST as $key => $value) {

// Trim pagetitle and Submit from string
if ($key == 'pagetitle' OR $key == 'Submit' OR $key == 'ID') 
    {   }
  //else {$kv[] = "$key"."="."$value".",";} //format and add to array
  else {$kv[] = "$key"."="."'"."$value"."'".",";} //format and add to array

            } //end of foreach statement
  $query_string = join($kv);
    } //create querystring from array
else {$query_string = $_SERVER['QUERY_STRING']; } //end of if post statement
?>
<?php
    $myqrystring="";
  //$myqrystring = '"UPDATE MyTable SET '.$query_string;
  //$myqrystring = $query_string;
$query_string = rtrim($query_string, ","); // Trim last comma from string
//$myqrystring = "\"" . $query_string . "\"";

//******* put string on the html page for review.***********

// put myqrystring on the html page for review.
//echo $myqrystring;
echo $query_string;
echo "<hr>" ;
echo "<p>" ;

//******* Update DataBase Table **************

$testquery_string="Area=400, PID=400,Comments='Filled by someone@somewhere.com'";
//,Comments=Filled by someone@somewhere.com   htmlspecialchars()  htmlentities()
//urlencode()  $safeHtml = htmlentities($evilHtml, ENT_QUOTES, 'UTF-8');'".$val2."'
//this works ,Comments='Filled by someone@somewhere.com' in $testquery_string
//$testquery_string=rawurlencode($testquery_string);//Does not work
//$testquery_string = htmlentities($testquery_string, ENT_QUOTES, 'UTF-8');//Does not work
$con=mysqli_connect("localhost","root","ireene","IEC");
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
//mysqli_query($con,"UPDATE MyTable SET Area=10 WHERE ID= $_POST['ID']");
//mysqli_query($con,'myqrystring');
mysqli_query($con,"UPDATE MyTable SET $testquery_string WHERE ID= $ID");
//$testquery_string works not $query_string $myqrystring
//mysqli_query($con,"UPDATE MyTable SET $query_string WHERE ID= $ID");
mysqli_close($con);
?>

我将其返回页面进行审核,看起来正确但不起作用” 区域='400',PID='400',评论='某事'。单引号引用的 Area 和 PID 确实有效。 因此,我希望循环遍历所有字段,执行相同的修复操作,而无需分隔 3 个字符串。

感谢一百万, 丹尼斯

最佳答案

尝试

mysqli_real_escape_string($con, $string)

像这样转义你的值(value)观可能会解决你的问题。

关于php - 空格和特殊字符更新查询不起作用,具有 50 个输入字段的 html 表单 47 是 int 没有问题,3 是文本/字符串问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20688015/

相关文章:

mysql - 在 mysql select 语句中重用内部查询

php - 如何用PHP制作不规则表格?

PHP 脚本无法正确找到 MySQL 表行

php - 网页上的重复资源管理器/outlook 类型应用程序

html - 当我将网格行设置为跨度 2 时,为什么我的网格行跨度为 1 行?

php - 选项卡式 jQuery 搜索结果

php数值数组

php - Doctrine2,Symfony2。甲骨文错误 :Class has no field or association named user_id

连接行的MySQL顺序?

php - 参数号无效 : parameter was not defined - PHP/MYSQL