在 PHP 问题中使用 $_POST

发布于 2024-12-06 05:45:33 字数 944 浏览 0 评论 0原文

我正在尝试通过 PHP 中的 $_POST 获取一些信息,基本上现在我正在使用这个:

$item_name1 = $_POST['item_name1']; 
$item_name2 = $_POST['item_name2']; 
$item_name3 = $_POST['item_name3']; 
$item_name4 = $_POST['item_name4'];

我想使用 mysql 将每个项目名称插入表字段中,所以我正在尝试使用 while php 循环所以我没有很多 $item_name 变量:

$number_of_items = $_POST['num_cart_items']; 

$i=1;
while($i<=$number_of_items)
  {  
    $test = $_POST['item_name'. $i'']; 
    $i++;
  }

上面的代码失败了,解释起来相当棘手,但代码应该找到所有 item_name $_POST 并将其作为 mysql 插入的变量。

$_POST['num_cart_items'] 是商品总数。

该代码适用于正在进行的购物车的 PayPal IPN 侦听器。

帮助表示赞赏。

编辑:

我在我刚刚意识到的文档中进一步提到了这一点:

$req = 'cmd=_notify-validate';
foreach ($_POST as $key => $value) {
    $value = urlencode(stripslashes($value));
    $req .= "&$key=$value";
}

How can i insert $_POST['item_name1'], $_POST['item_name2'] 作为 mysql 插入的变量?

I'm trying get some information via $_POST in PHP, basically at the moment i'm using this:

$item_name1 = $_POST['item_name1']; 
$item_name2 = $_POST['item_name2']; 
$item_name3 = $_POST['item_name3']; 
$item_name4 = $_POST['item_name4'];

I want to insert each of the item names in a table field with mysql so i'm trying to experiment with the while php loop so i dont have lots of $item_name variables:

$number_of_items = $_POST['num_cart_items']; 

$i=1;
while($i<=$number_of_items)
  {  
    $test = $_POST['item_name'. $i'']; 
    $i++;
  }

The above code fails, its pretty tricky to explain but the code should find all the item_name $_POST and make it as a variable for mysql insertion.

The $_POST['num_cart_items'] is the total number of items.

The code is for a PayPal IPN listener for a shopping cart that is underway.

Help appreciated.

EDIT:

I have this further up the document which i just realised:

$req = 'cmd=_notify-validate';
foreach ($_POST as $key => $value) {
    $value = urlencode(stripslashes($value));
    $req .= "&$key=$value";
}

How can i insert $_POST['item_name1'], $_POST['item_name2'] as a variable for mysql insertion?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

苍白女子 2024-12-13 05:45:33

您的循环在每次迭代中有效地覆盖 $test 变量:

$test = $_POST['item_name'. $i''];

如果您想将它们放入数组中,请更改为 $test[]。它还包含 brian_d 提到的解析错误。

与表单一起发送的变量 num_cart_items 听起来有点可怕。你是用 JavaScript 设置的吗?用户可以操纵它。你不应该依赖它。我相信您需要的是将表单字段设置为:

<input type="text" name="item_name[]" />

注意名称末尾的方括号。这将在 $_POST 数组中创建一个数组:$_POST['item_name'] 将包含所有项目的名称。

那么,您的数据库是如何构建的?我猜你想将它们插入到一个查询中,如下所示:

INSERT INTO ORDERS VALUES (item_name_1, ...), (item_name_2, ...)

如果是这样,你可以从数组中生成一个字符串:

$query = 'INSERT INTO ORDERS VALUES ';
foreach($_POST['item_name'] as $item_name){
  $query .= '('.stripslashes($item_name). /*put other column values*/ '),';
}
$query = rtrim($query, ',');

请注意,使用 addslashes 不足以保护你免受 SQL 注入。

Your loop is effectively overriding the $test variable on each iteration:

$test = $_POST['item_name'. $i''];

If you want to put them in an array, change to $test[]. Also it contains the parse error as mentioned by brian_d.

It sounds a little scary to have a variable num_cart_items that is sent with the form. Are you setting it with JavaScript? The user can manipulate it. You should not rely on it. I belive what you need is to make the form feilds as:

<input type="text" name="item_name[]" />

Note the square brackets at the end of the name. This will create an array in the $_POST array: $_POST['item_name'] will contain the names of all the items.

Then, how is your DB structured? I guess you want to insert them in one query as:

INSERT INTO ORDERS VALUES (item_name_1, ...), (item_name_2, ...)

If so you can make a string out of the array:

$query = 'INSERT INTO ORDERS VALUES ';
foreach($_POST['item_name'] as $item_name){
  $query .= '('.stripslashes($item_name). /*put other column values*/ '),';
}
$query = rtrim($query, ',');

Note that the use of addslashes is not enough to protect you from SQL injection.

少女七分熟 2024-12-13 05:45:33

$test = $_POST['item_name'. $i'']; 是一个语法错误。
删除末尾的 '',使其变为:

$test = $_POST['item_name'. $i];

$test = $_POST['item_name'. $i'']; is a syntax error.
remove the end '' so it becomes:

$test = $_POST['item_name'. $i];

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