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')");
}
}
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";
}
?>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>