poymode Posted April 26, 2008 Share Posted April 26, 2008 I have this code here, It retrieves a list of products from the database and displays the product's item code as a checkbox with the item code as its value, this value, the item code is used for inserting into a table called buy where it has itemcode and customerID are foreign keys in table buy. Before inserting it into buy, I wanted to check if there are really values stored into the checkboxes with the item code values. <?php include('connectdb.php'); $itemCodes = $_POST['ItemCode']; for($i = 0; $i < count($_POST['ItemCode']);$i++) { echo $itemCode[$i]; } ?> <form method="post"> <table width="500" border="1" cellpadding="3" cellspacing="3"> <tr> <td width="130"><div align="center">Item Code</div></td><td width="145"><div align="center">Name</div></td><td width="107"><div align="center">Brand</div></td><td width="89"><div align="center">Price</div></td> </tr> <?php $query = "SELECT * FROM products"; $result = mysql_query($query); while($row = mysql_fetch_assoc($result)) { $itemValue = $row['Item_Code']; ?> <tr> <td><div align="center"><?php echo '<input type="checkbox" name="ItemCode[]" value="$itemValue" />'; ?></div></td> <td><div align="center"><?php echo $row['Product_name']; ?></div></td> <td><div align="center"><?php echo $row['Brand']; ?></div></td> <td><div align="center"><?php echo $row['Price']; ?></div></td> </tr> <?php } mysql_free_result($result); ?> </table> <br> <input type="submit" value="Buy" /> </form> the problem is, it doesnt display anything. I hope you could help me trace where I went wrong. It has no errors. Link to comment https://forums.phpfreaks.com/topic/103004-php-sql-and-checkboxes/ Share on other sites More sharing options...
Barand Posted April 26, 2008 Share Posted April 26, 2008 Only the values of checked checkboxes are posted Link to comment https://forums.phpfreaks.com/topic/103004-php-sql-and-checkboxes/#findComment-527655 Share on other sites More sharing options...
zenag Posted April 26, 2008 Share Posted April 26, 2008 <?php $con=mysql_connect("localhost","root",""); mysql_select_db("test",$con); if($_POST["submit"]=='Buy') {//echo "ddd"; $itemCodes = $_POST['ItemCode']; // print_r($itemCodes); foreach($itemCodes as $key=>$value) { echo $value; } //for($i = 0; $i < count($itemCodes);$i++) // { // echo $itemCode[$i]; // } } ?> <form method="post"> <table width="500" border="1" cellpadding="3" cellspacing="3"> <tr> <td width="130"><div align="center">Item Code</div></td><td width="145"><div align="center">Name</div></td><td width="107"><div align="center">Brand</div></td><td width="89"><div align="center">Price</div></td> </tr> <?php $query = "SELECT * FROM products"; $result = mysql_query($query); while($row = mysql_fetch_assoc($result)) { $itemValue = $row['Item_Code']; ?> <tr> <td><div align="center"><?php echo "<input type='checkbox' name='ItemCode[]' value='$row[item_Code]' />"; ?></div></td> <td><div align="center"><?php echo $row['Product_name']; ?></div></td> <td><div align="center"><?php echo $row['Brand']; ?></div></td> <td><div align="center"><?php echo $row['Price']; ?></div></td> </tr> <?php } mysql_free_result($result); ?> </table> <input type="submit" name="submit" value="Buy" /> </form> Link to comment https://forums.phpfreaks.com/topic/103004-php-sql-and-checkboxes/#findComment-527656 Share on other sites More sharing options...
poymode Posted April 26, 2008 Author Share Posted April 26, 2008 There is still a problem. I only checked two boxes and print_r displays Array ( [0] => [1] => ) No values. Link to comment https://forums.phpfreaks.com/topic/103004-php-sql-and-checkboxes/#findComment-527660 Share on other sites More sharing options...
zenag Posted April 26, 2008 Share Posted April 26, 2008 check ur database values.... Link to comment https://forums.phpfreaks.com/topic/103004-php-sql-and-checkboxes/#findComment-527661 Share on other sites More sharing options...
poymode Posted April 26, 2008 Author Share Posted April 26, 2008 Thanks. there was a slight case of Case sensitivity. Link to comment https://forums.phpfreaks.com/topic/103004-php-sql-and-checkboxes/#findComment-527663 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.