views:

54

answers:

3

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
}
A: 

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.

Hisamu
A: 

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....
  }
}
Sarfraz
Thanks for the code and explanation. It's working great. Just one question, when data stored to db had empty `mid pid title` on the `last id` of auto increment
kampit
@kampit: Can't say, you should check your code, know how many things are coming up in those arrays.
Sarfraz
Ok fixed. I changed the `for($i = 0; $i < count($_POST['title']);)` and put the `$i++;` before the last }
kampit
@kampit: That is good to know, i was about to write that now :)
Sarfraz
A: 

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.

Lèse majesté