Jump to content

[SOLVED] iterate through array selecting keys only into sql statement. best way?


rmmo

Recommended Posts

hi all

 

right so here i go again!  :P

 

i have a form with checkboxes... the results are posted to the next php and they get there as an array.

 

the data i need to go into the table is this

 

customer_id , Credit Card Number, prod_id, No of products and the date.

 

could some one help me figure out how to itterate through the array and pick out only the keys (prod_id) .. and have them inserted with the other info into a row in my table (ordertable)?

 

PHULEASE!  ;D

thanks in advanced!    :P

RMMO

 

ps below is what is printed when echo the array.. with one of the boxes checked!

 

 

Array ( [submit] => order selected [prod101] => 1 )

 

so i need to just take "prod101" and have that go into this statement

 

insert into orders values ( '$productid', $user_id, $cc_number, $no_of_products, $sysdate)

 

oh pps... how can i get sysdate assigned to a $VARIABLE  ??? last ps promise

Because they are checkboxes, does that mean that more than one prod_id can be returned? Is prod101 supposed to be the array of checkboxes? If so, it should look like this in the $_GET or $_POST array: Array ( [prod101] => Array ( [0] => on [1] => on ) ) ; in which case, you need to know what index corresponds to what checkbox. If you could post your HTML it might be easier to understand what you are trying to do.

ummm... sorry i guess the first post wasnt very clear.

 

ill just post the form

:D

 


   // connect to and select db
   $conn = mysql_connect($host, $dbuser, $dbpass) or trigger_error("SQL", E_USER_ERROR);
   $db = mysql_select_db($dbname, $conn) or trigger_error("SQL", E_USER_ERROR);
$name = $_SESSION['name'];
$password = $_SESSION['password'];

if($db){
  $query = "select * from product";
  $result = mysql_query($query);

?>
<table border bgcolor ='green'>
<tr>
  <th>product id</th>
  <th>name</th>
  <th>description</th>
  <th>price ($)</th>
</tr>
<?  while($row = mysql_fetch_array($result) )
{  ?>
<form method="post" action="artsonlinestorermmoorders.php">
<tr>
  <td><?=$row['prod_id'] ?></td>
  <td><?=$row['prod_name'] ?></td>
  <td><?=$row['prod_desc'] ?></td>
  <td><?=$row['prod_price'] ?></td>
  <td><input type="checkbox" name="<?=$row['prod_id'] ?>" value="1" /></td>
   </tr>
   

   
<? }  //end while
   }else die("sorry could not retreive product listings"); ?>
<input type="submit" name="submit" value="order selected" />
<input name="reset" type="reset" value="clear" />

</form>

 

thats the one with the checkboxes. now i want to make another that takes the array thats posted from there and enters each of the keys into a new insert statement.

 

is that a bit clearer?

 

hope so if not just tell me what bit im not making sense on.  ;D

 

thanks again RMMO

Make the ids the values not the name. Name should be something like "product[]"

 

Take form tag out of the loop.

 

<form method="post" action="artsonlinestorermmoorders.php">
<?  while($row = mysql_fetch_array($result) )
{  ?>
<tr>
  <td><?=$row['prod_id'] ?></td>
  <td><?=$row['prod_name'] ?></td>
  <td><?=$row['prod_desc'] ?></td>
  <td><?=$row['prod_price'] ?></td>
  <td><input type="checkbox" name="product[]" value="<?=$row['prod_id'] ?>" /></td>
   </tr>
   

   
<? }  //end while?>
</form>

 

To process (only checked boxes are posted)

<?php
foreach ($_POST['product'] as $prodid)
{
    echo $prodid, '<br/>';
    // insert into table here
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.