如果每次循环后提交语句都没有通过
预先感谢,这是发生的事情:
if(isset($_POST['submit']))
{
$var = $_GET['var'];
foreach((array)$_POST['content'] as $area => $contents)
{
$result = 'UPDATE $table SET '.$area.' = "'.mysql_real_escape_string(stripslashes($contents)).'" WHERE var = "'.$_GET['var'].'";';
mysql_query ($result);
}
$query = 'UPDATE $table SET '.
'title = "'.mysql_real_escape_string(stripslashes($_POST["title"])).'", '.
'template = "'.mysql_real_escape_string($_POST["templateid"]).'", '.
'description = "'.mysql_real_escape_string(stripslashes($_POST["description"])).'", '.
'keywords = "'.mysql_real_escape_string(stripslashes($_POST["keywords"])).'" '.
' WHERE var = "'.$_GET['var'].'";';
mysql_query ($query);// or die(mysql_error());
}
我无法让 if 语句超出 foreach 循环。它不会使用 $query mysql 语句更新数据库。它简单地更新 foreach 循环内所有内容的表,然后存在。我需要执行 If while 或 else if 吗?我尝试查找 php 条件语句,是否有 and 语句之类的东西?如:如果提交后做这个和这个?这本质上就是我想要做的,如果提交提交,则为每个循环执行并执行 $query。
如果您需要更多信息,请告诉我。
Thanks in advance, here is what is going on:
if(isset($_POST['submit']))
{
$var = $_GET['var'];
foreach((array)$_POST['content'] as $area => $contents)
{
$result = 'UPDATE $table SET '.$area.' = "'.mysql_real_escape_string(stripslashes($contents)).'" WHERE var = "'.$_GET['var'].'";';
mysql_query ($result);
}
$query = 'UPDATE $table SET '.
'title = "'.mysql_real_escape_string(stripslashes($_POST["title"])).'", '.
'template = "'.mysql_real_escape_string($_POST["templateid"]).'", '.
'description = "'.mysql_real_escape_string(stripslashes($_POST["description"])).'", '.
'keywords = "'.mysql_real_escape_string(stripslashes($_POST["keywords"])).'" '.
' WHERE var = "'.$_GET['var'].'";';
mysql_query ($query);// or die(mysql_error());
}
I am unable to get the if statement to go beyond the for each loop. It will not update the database with the $query mysql statement. It simple updates the table for everything inside the for each loop and then exists. Do I need to do an If while or else if? And I tried looking up php conditional statements, is there such a thing as and statements? Such as: If post submit do this AND this? That is essentially what I am trying to do, If post submit, do for each loop AND do $query.
Let me know if you need more information.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
那么,
$_POST['content']
到底定义了吗?在这种情况下,您可能需要使用print_r($_POST)
开始调试以查看获得的所有值。您可能想要做的是:
编辑:
而且你应该真的转义
$_GET['var']
以及循环中的$area
!始终转义所有用户输入!Well, is
$_POST['content']
defined at all? In cases like this you may want to start debugging withprint_r($_POST)
to see all values that you get.What you probably intended to do is:
Edit:
And you should really escape that
$_GET['var']
as well as the$area
in the loop!!! Escape ALL user input, always!!!表名不会显示。还添加一些内容来显示 mysql 错误消息以帮助调试:
The table name wont be showing. Also add something to show the mysql error message to help debug:
尝试添加 *ini_set('display_errors',1);* 添加脚本的开头以确保错误输出。
并使用*var_dump(mysql_error());*显示第二个查询的mysql错误。
还有一个小建议。
对 $area 进行开关以确保传递有效字段:
祝你好运!
try and add *ini_set('display_errors',1);* add the start of the script to ensure error ouput.
And use *var_dump(mysql_error());* to display the mysql error of the second query.
And a small suggestion.
do a switch on $area to ensure the valid fields are passed:
good luck!