Good day guys =) ..., i know there are somewhat similar threads of this around here but i still cant find the one to solve my problem ... well i am currently building an online store.. and in this online store there's a table of products with the an image, id and name ... beside the list of images is a checkbox which a customer is supposed to check if she is interested with the item now this table is connected to a form wherein the customer is supposed to enter her name, contact info and address... my problem is i would like to store this info (name,contact,address, and the value of the checked boxes) altogether in the database allowing multiple boxes to be checked...

my database contains:
id, name,contact,address,order...

here's my code

<?php
$name = $_POST['name'];
$address = $_POST['address'];
$phone = $_POST['phone'];
$orders = $_POST['orders'];

mysql_connect("localhost", "root","") or die ('Error: '. mysql_error());
mysql_select_db("store");


$query="INSERT INTO orders (id, name, address, phone, custorder) VALUES ('NULL','".$name."','".$address."','".$phone."','".$orders."' )";
mysql_query($query) or die ('Error Updating the Database' . mysql_errno());
echo "Order Successfully Placed";


?>

it already stores data in the database but only one order is stored even if i check multiple check boxes

Recommended Answers

All 5 Replies

You haven't shown your form code. Each check box should have it's own name. If you are only referencing $orders, then it would make sense that you are only retrieving one value.

Use array [] in check box name in form as shown below.

Orders
    <input name="orders[]" type="checkbox" value="One" /> One
    <input name="orders[]" type="checkbox" value="Two" /> Two
    <input name="orders[]" type="checkbox" value="Three" /> Three

Then you need to change php code:

<?php
$name = $_POST['name'];
$address = $_POST['address'];
$phone = $_POST['phone'];
$orders = $_POST['orders'];

mysql_connect("localhost", "root","") or die ('Error: '. mysql_error());
mysql_select_db("store");

if(count($orders)>0)
{
	foreach($orders as $key=>$order)
	{
		$query="INSERT INTO orders (id, name, address, phone, custorder) VALUES ('NULL','".$name."','".$address."','".$phone."','".$order."' )";
		mysql_query($query) or die ('Error Updating the Database' . mysql_errno());		
	}
	echo "Order Successfully Placed";
}
else
	echo "No Orders";
?>

Vibhadevit is right. You must use array to store for multiple checkbox values those are coming by one group. If so, you must repeat the query to retrieve the multiple values and enter to database. Vibhadevit has shown the pretty explaining with example above.
If not separate the group with specific unique name like Chrishea said. In this manner, you don't need to repeat the query.

well i am currently building an online billing system.. and it contain registration page... in this registration page the customer has to fill up the client details in this they have to fill up the client name:,company name;,address:,email id;,cid;,Acc_No;........ and it contain types of services in this they have to just choose the types of services that they want.........the database contain the 10 tables...........i have to insert the data in 4 table.......
this are what i have tried...
<?php


$cl=$_REQUEST;
$co=$_REQUEST;
$ad=$_REQUEST;
$em=$_REQUEST;
$ci=$_REQUEST;
$ac=$_REQUEST;

$pc=$_REQUEST;
$rig=$_REQUEST;
$re=$_REQUEST;
$nip=$_REQUEST;
$rm=$_REQUEST;
$conn=mysql_connect("localhost","root","");
if (!$conn)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("druknet_automated_billing_system_trial",$conn);
mysql_query("INSERT INTO client(Cl_Name, Company_Name, Address, Email_ID, CID, Acc_No) values('$cl','$co','$ad','$em','$ci','$ac')");
mysql_query("INSERT INTO client_services(P_code, Registration_Date, Renewal_Date, No_of_IPAddress, Remarks) values('$pc','$rig','$re','$nip','$rm')");

mysql_close($conn);
header("location: thankyou.php");
?>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.