如何将数据存储到MySQL以进行数组输入

发布于 2024-09-06 04:35:46 字数 1568 浏览 3 评论 0原文

希望我的问题对帖子来说是正确的。

  <form action="process.php?id=<?php echo intval($order['id']);?>" method="post">
  <ul>
  <?php
  $sd = 'SELECT * FROM download WHERE pid IN ('.$order['pid'].') ORDER BY pid ASC'; // pid IN (3,4,5)
  $qd = $db->rq($sd);
  $no = 1; while($download = $db->fetch($qd)) {
  ?>
  <li>
  <?php echo $no; ?> <?php echo $download['title']; ?>
  <input type="hidden" name="mid[]" value="<?php echo $order['mid']; ?>" />
  <input type="hidden" name="pid[]" value="<?php echo $download['pid']; ?>" />
  </li>
  <?php $no++; } ?>
  </ul>
  <input type="submit" name="submit" value="Submit" /> 
  </form>

输出

  1. 索尼爱立信驱动程序
  2. 索尼爱立信应用程序
  3. 三星驱动
  4. 程序摩托罗拉驱动程序

问题

  1. 如何将数据输出存储到下面的表结构中>process.php
  2. 数据将被保存为类似这样的内容。

    <前><代码>id |中| PID |标题 ---------------------------------------------------- 1 | 1 | 3 |索尼爱立信驱动程序 ---------------------------------------------------- 2 | 1 | 3 |索尼爱立信应用程序 ---------------------------------------------------- 3 | 1 | 4 |三星驱动程序 ---------------------------------------------------- 4 | 1 | 5 |摩托罗拉驱动程序 ----------------------------------------------------

process.php

if (isset($_POST['submit']) && !empty($_POST['submit'])) {
// I'm blur how to get dynamic mid[] & pid[] here
}

Hope my question is correct to the post.

  <form action="process.php?id=<?php echo intval($order['id']);?>" method="post">
  <ul>
  <?php
  $sd = 'SELECT * FROM download WHERE pid IN ('.$order['pid'].') ORDER BY pid ASC'; // pid IN (3,4,5)
  $qd = $db->rq($sd);
  $no = 1; while($download = $db->fetch($qd)) {
  ?>
  <li>
  <?php echo $no; ?> <?php echo $download['title']; ?>
  <input type="hidden" name="mid[]" value="<?php echo $order['mid']; ?>" />
  <input type="hidden" name="pid[]" value="<?php echo $download['pid']; ?>" />
  </li>
  <?php $no++; } ?>
  </ul>
  <input type="submit" name="submit" value="Submit" /> 
  </form>

Output

  1. Sony Ericsson Drivers
  2. Sony Ericsson Apps
  3. Samsung Drivers
  4. Motorola Drivers

Question

  1. How to store (save) the data Output to table structure at below on process.php
  2. Data will be save something like these.

    id | mid | pid | title
    -----------------------------------------
    1  |  1  |  3  |  Sony Erricson Drivers
    -----------------------------------------
    2  |  1  |  3  |  Sony Erricson Apps
    -----------------------------------------
    3  |  1  |  4  |  Samsung Drivers
    -----------------------------------------
    4  |  1  |  5  |  Motorola Drivers  
    -----------------------------------------
    

process.php

if (isset($_POST['submit']) && !empty($_POST['submit'])) {
// I'm blur how to get dynamic mid[] & pid[] here
}

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

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

发布评论

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

评论(3

浅听莫相离 2024-09-13 04:35:46

为标题创建第三个隐藏字段,例如:

 <input type="hidden" name="title[]" value="<?php echo $download['title']; ?>" />
 <input type="hidden" name="mid[]" value="<?php echo $order['mid']; ?>" />
 <input type="hidden" name="pid[]" value="<?php echo $download['pid']; ?>" />

现在您可以像这样存储它,假设您的页面被提交到 process.php,这就是您应该放在那里的内容:

if (isset($_POST['submit']))
{
  for($i = 0; $i<=count($_POST['title']); $i++)
  {
     $title = mysql_real_escape_string($_POST['title'][$i]);
     $mid = intval($_POST['mid'][$i]);
     $pid = intval($_POST['pid'][$i]);

     // database code
     $query = "insert into table set title = '$title', mid = $mid, pid = $pid";
     // mysql_query and/or more code....
  }
}

Create a third hidden field for titles eg:

 <input type="hidden" name="title[]" value="<?php echo $download['title']; ?>" />
 <input type="hidden" name="mid[]" value="<?php echo $order['mid']; ?>" />
 <input type="hidden" name="pid[]" value="<?php echo $download['pid']; ?>" />

Now you can store it like this, suppose your page gets submitted to process.php, this is what you should put in there:

if (isset($_POST['submit']))
{
  for($i = 0; $i<=count($_POST['title']); $i++)
  {
     $title = mysql_real_escape_string($_POST['title'][$i]);
     $mid = intval($_POST['mid'][$i]);
     $pid = intval($_POST['pid'][$i]);

     // database code
     $query = "insert into table set title = '$title', mid = $mid, pid = $pid";
     // mysql_query and/or more code....
  }
}
放血 2024-09-13 04:35:46

您可以简单地对 $_POST['mid'] 和 $_POST['pid'] 数组进行循环,如下所示:

for ($i=0; $i<count($_POST['mid']); $i++)
{
  $mid = $_POST['mid'][$i];
  $pid = $_POST['pid'][$i];
}

如果您也想传递项目的名称,则可以像发送 'mid' 一样传递它' 和 'pid' 字段。

You can simply do a loop over the $_POST['mid'] and the $_POST['pid'] array, like this:

for ($i=0; $i<count($_POST['mid']); $i++)
{
  $mid = $_POST['mid'][$i];
  $pid = $_POST['pid'][$i];
}

If you want to pass the item's name too you can just pass it the same way you sent 'mid' and 'pid' fields.

┼── 2024-09-13 04:35:46

您可以通过访问 POST< 以与检查 submit 相同的方式获取 mid[]pid[] 的值/code> 变量:

$mid = $_POST['mid'];
$pid = $_POST['pid'];

由于它们作为数组发布,因此它们将是 $_POST 变量中的数组。您还可以通过以下方式检查单个 $_POST 变量或整个 $_POST 变量的结构:

var_dump($_POST['mid']);
var_dump($_POST);

对于调试或找出您不确定其内容的变量,这是一个有用的函数。

You get the values for mid[] and pid[] the same way you're checking submit, by accessing the POST variables:

$mid = $_POST['mid'];
$pid = $_POST['pid'];

Since they're posted as arrays, they'll be arrays in the $_POST variable. You can also check the structure of the individual $_POST variables or the entire $_POST variable by the following:

var_dump($_POST['mid']);
var_dump($_POST);

This is a useful function for debugging or figuring out a variable whose contents you're unsure about.

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