rmmo Posted July 3, 2008 Share Posted July 3, 2008 hi all right so here i go again! 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! thanks in advanced! 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 Link to comment https://forums.phpfreaks.com/topic/113104-solved-iterate-through-array-selecting-keys-only-into-sql-statement-best-way/ Share on other sites More sharing options...
lemmin Posted July 3, 2008 Share Posted July 3, 2008 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. Link to comment https://forums.phpfreaks.com/topic/113104-solved-iterate-through-array-selecting-keys-only-into-sql-statement-best-way/#findComment-581027 Share on other sites More sharing options...
rmmo Posted July 3, 2008 Author Share Posted July 3, 2008 ummm... sorry i guess the first post wasnt very clear. ill just post the form // 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. thanks again RMMO Link to comment https://forums.phpfreaks.com/topic/113104-solved-iterate-through-array-selecting-keys-only-into-sql-statement-best-way/#findComment-581063 Share on other sites More sharing options...
lemmin Posted July 3, 2008 Share Posted July 3, 2008 Use a foreach loop to find the keys: foreach ($_POST as $key => $value) //$key is the name of the checkbox that was checked. Link to comment https://forums.phpfreaks.com/topic/113104-solved-iterate-through-array-selecting-keys-only-into-sql-statement-best-way/#findComment-581092 Share on other sites More sharing options...
rmmo Posted July 3, 2008 Author Share Posted July 3, 2008 so what will that do? i mean i need to make the insert statement run in the loop as well dont i? like so it inserts a row for each checkbox? Link to comment https://forums.phpfreaks.com/topic/113104-solved-iterate-through-array-selecting-keys-only-into-sql-statement-best-way/#findComment-581093 Share on other sites More sharing options...
Barand Posted July 3, 2008 Share Posted July 3, 2008 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 } Link to comment https://forums.phpfreaks.com/topic/113104-solved-iterate-through-array-selecting-keys-only-into-sql-statement-best-way/#findComment-581118 Share on other sites More sharing options...
rmmo Posted July 3, 2008 Author Share Posted July 3, 2008 thanks alot Barand! i didnt do exactly what you said but i got the idea. thanks all RMMO <SOLVED> Link to comment https://forums.phpfreaks.com/topic/113104-solved-iterate-through-array-selecting-keys-only-into-sql-statement-best-way/#findComment-581159 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.