Php 奇异布尔反应 $alerter2=“false”; 对于($counter=0;$counter
您需要更改Php 奇异布尔反应 $alerter2=“false”; 对于($counter=0;$counter,php,string,return,Php,String,Return,您需要更改 $alerter2="false"; for ( $counter = 0; $counter <= count($filter); $counter++) { $questionsubmitted=strtolower($_POST[question]); $currentcheck =$filter[$counter]; $foundvalue=stripos((string)$questionsubmitted,(string)$currentch
$alerter2="false";
for ( $counter = 0; $counter <= count($filter); $counter++) {
$questionsubmitted=strtolower($_POST[question]);
$currentcheck =$filter[$counter];
$foundvalue=stripos((string)$questionsubmitted,(string)$currentcheck);
echo $foundvalue;
if ($foundvalue==0) {
$alerter2="true";
} else { }
}
if (!($alerter2=="true")) {
$sql="INSERT INTO Persons (Name, Email, Question)
VALUES
('$_POST[name]','$_POST[email]','$_POST[question]')";
} else {
echo "Please only post appropriate questions";
}
到
或者类似的东西,取决于你的逻辑(我不太明白发生了什么)
但正如大家所说,这段代码容易受到SQL注入攻击(以及其他问题).如果找不到值,则返回false,如果它是第一个字符,则返回0。问题是,php自动将布尔值转换为0整数,或将0整数转换为false。因此,我认为这里正在进行转换,因此条件不符合您的要求
您还可以使用==
检查变量的类型:
if ($foundvalue===0) // three equals signs
有关此问题的更多详细信息,请参阅stripos
的链接文档
您还应该删除清空的else
子句以获得更干净的代码,并在将值放入数据库之前使用该子句对值进行清理。此外
if ($foundvalue === 0) {
$alerter2="true";
}
应该是:
$questionsubmitted=strtolower($_POST[question]);
在
$\u POST
上使用mysql\u escape\u real\u string();
在$sql
中使用变量将是另一件事,用字符串表示“真”和“假”.真的吗?谁在教这种糟糕的方法?为什么这么多人在编写易受SQL注入攻击的代码,而简单的修复程序已经提供了十多年了!?!?!确实如此。另外,添加一个骰子(mysql_error())或/和一个error_log(),以便更好地查看SQL错误和跟踪them@TimeCoder:请确定你的问题的标题。“为什么这不起作用”作为描述性标题是不可接受的。是的,这确实起作用,但每次这样做时,您都会在日志文件中得到一个PHP通知。question
(没有撇号)被视为常量表达式(即之前由define('question',somevalue'定义)
,尽管常量应该先用大写字母书写),但在您的情况下不是这样。
$questionsubmitted=strtolower($_POST[question]);
$questionsubmitted=strtolower($_POST['question']);