Page 1 of 1

Inserting more than one row in database using checkboxes

Posted: Fri Dec 26, 2008 2:20 am
by Greg19
Hello everyone I have a question, currently I have two drop-down menus with information from two database tables. One menu has a customer name and the other a list of products that the company using this form offers. This codes purpose is to update the information on the customer in regards to what products the customer has and to ensure that there are no duplicate entery. What I have now works fine, but I need to change the drop down menu for the products into a series of checkboxes so that I can insert multiple rows at once. I got the checkbox part working fine, but I'm clueless as to how to insert multiple rows in two columns (customer, which would be the same for all the rows, and product_id, which would be different for each row). I'm quite new to this php stuff so any help and pointers would be great.

What I'm thinking is creating an array from the information posted from the checkboxes and using a foreach statement like the following to insert the data:

Code: Select all

if (isset ($_POST['customer'])) {
    
    $customer = ($_POST['customer']);
        
    foreach ($product_id as $ID){
        $sql = @mysql_query("INSERT INTO `customers_products` (customer, product_id) VALUES ('$customer', '$ID')");
    }
}
 
However I have no idea how to create an array from information provided by this:

Code: Select all

$conn = "SELECT product_id FROM `products`";
$result = mysql_query($conn, $query);
 
while ($row = mysql_fetch_row($result)) {
  $product[] = $row[0];
}
 
foreach ($product as $y) {
  echo "<input class='checkbox' type='checkbox'  name='product_id' value='$y'>\n" . $y . "</input>\n";
}
 
?>
Thanks for any help, and heres the code thats currently working with the two drop down menus:

Code: Select all

<?php
session_start(); 
if(!isset($_SESSION['adminctrl'])){ 
    header('Location: admin.php'); die('<a href="admin.php">Login first!</a>');
   }
$query = mysql_connect("*********.net", "******", "*******") or die(mysql_error());
mysql_select_db('******', $query) or die(mysql_error());
 
$error = array ();
if (isset ($_POST['customer'])) {
  //Find duplicate first
  $sql = sprintf(
    "SELECT customer FROM `customers_products` WHERE customer = '%s' AND product_id = '%s'",
    mysql_real_escape_string($_POST['customer']),
    mysql_real_escape_string($_POST['product_id'])
  );
  if($result = @mysql_query($sql)){
    if(!@mysql_num_rows($result)){
        
      $sql = sprintf(
        "INSERT INTO `customers_products` (customer, product_id) VALUES ('%s','%s')",
        mysql_real_escape_string($_POST['customer']),
        mysql_real_escape_string($_POST['product_id'])
      );
      if(@mysql_query($sql))
        echo 'Update Was Successful';
      else
        $error[] = 'Insert Failed: '.mysql_error();
    }else
      $error[] = 'Combination already found';
  }else
    $error[] = 'Select Failed: '.mysql_error();
}
?>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Customer Update</title>
</head>
<html>
<body>
<?php
  if(count($error)){
    print '<ul><b>Errors:</b><li>'.implode('</li><li>',$error).'</li></ul>';
  }
?>
<form method="post" action="customer_info.php">
<?php
 
$conn = "SELECT company FROM `users`";
$result = mysql_query($conn, $query);
 
while ($row = mysql_fetch_row($result)) {
  $user[] = $row[0];
}
 
echo "<select name='customer'>\n";
foreach ($user as $v) {
  echo "<option value='$v'>\n" . $v . "</option>\n";
}
echo "</select>\n";
 
echo "<br/><br/>";
 
$conn = "SELECT product_id FROM `products`";
$result = mysql_query($conn, $query);
 
while ($row = mysql_fetch_row($result)) {
  $product[] = $row[0];
}
 
echo "<select name='product_id'>\n";
foreach ($product as $y) {
  echo "<option value='$y'>\n" . $y . "</option>\n";
}
echo "</select>\n";
 
php
?>
 
<br/><br/>
<input type="submit" name="submit" value="Update" />
 
</form>
<br/>
<a href="register.php">Register New Users </a><br/><a href="doc_update.php">Upload New Documents</a><br/>
<a href="customer_info.php">Update Customer Product Info</a><br/><a href="mod_manage.php">Manage Production Login</a>
<br/><a href="product_update.php">Manage Products</a><br/>
</body>
</html>
 
 
 

Re: Inserting more than one row in database using checkboxes

Posted: Fri Dec 26, 2008 5:34 am
by sandiphgt
Hi,

Please replace your old code

echo "<input class='checkbox' type='checkbox' name='product_id' value='$y'>\n" . $y . "</input>\n";

with new one

echo "<input class='checkbox' type='checkbox' name='product_id[]' value='$y'>\n" . $y . "</input>\n";

Re: Inserting more than one row in database using checkboxes

Posted: Sat Dec 27, 2008 12:37 am
by Greg19
Okay, so how do I create an array from ($_POST['product_id[]'])? A while loop? -thanks for the help

Re: Inserting more than one row in database using checkboxes

Posted: Mon Dec 29, 2008 1:36 am
by sandiphgt
Sorry for late reply

you can retrive contents in PHP as pe followes.
$id=$_POST['product_id'];
while (list ($key,$val) = @each ($id)) {
echo "$val,";
}

You can put your insert statement in that while loop.