Jump to content

Php sql and checkboxes


poymode

Recommended Posts

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

<?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>


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.