如果每次循环后提交语句都没有通过

发布于 2025-01-02 20:06:17 字数 1055 浏览 0 评论 0原文

预先感谢,这是发生的事情:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

时光无声 2025-01-09 20:06:17

那么,$_POST['content'] 到底定义了吗?在这种情况下,您可能需要使用 print_r($_POST) 开始调试以查看获得的所有值。

您可能想要做的是:

foreach ($_POST as $key => $val)

编辑:
而且你应该真的转义$_GET['var']以及循环中的$area!始终转义所有用户输入!

Well, is $_POST['content'] defined at all? In cases like this you may want to start debugging with print_r($_POST) to see all values that you get.

What you probably intended to do is:

foreach ($_POST as $key => $val)

Edit:
And you should really escape that $_GET['var'] as well as the $area in the loop!!! Escape ALL user input, always!!!

俯瞰星空 2025-01-09 20:06:17

表名不会显示。还添加一些内容来显示 mysql 错误消息以帮助调试:

$result = 'UPDATE '.$table.' SET '.$area.' = "'.mysql_real_escape_string(stripslashes($contents)).'" WHERE var = "'.$_GET['var'].'";';
mysql_query ($result) or die(mysql_error());

The table name wont be showing. Also add something to show the mysql error message to help debug:

$result = 'UPDATE '.$table.' SET '.$area.' = "'.mysql_real_escape_string(stripslashes($contents)).'" WHERE var = "'.$_GET['var'].'";';
mysql_query ($result) or die(mysql_error());
终止放荡 2025-01-09 20:06:17

尝试添加 *ini_set('display_errors',1);* 添加脚本的开头以确保错误输出。
并使用*var_dump(mysql_error());*显示第二个查询的mysql错误。

还有一个小建议。
对 $area 进行开关以确保传递有效字段:

switch ($area) {
  case 'field1': $area='field1'; break;
  case 'field2': $area='field2'; break;
  default: echo 'No valid field'; continue; break;
}

祝你好运!

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:

switch ($area) {
  case 'field1': $area='field1'; break;
  case 'field2': $area='field2'; break;
  default: echo 'No valid field'; continue; break;
}

good luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文