Forums
This topic is locked
Checkbox Array INSERT to mySQL
Posted 09 Sep 2010 13:35:40
1
has voted
09 Sep 2010 13:35:40 Mark Taylor posted:
Hi, I'd appreciate any help that anyone can give me to solve the following work-based project problem. Please bear with me as i'm fairly new to PHP. I've searched this and several other forums for 3 days now but am still no further forward:
I have a simple mysql table tbl_friends consisting of two fields: friends_id (an autonumber primary key) and first_name (varchar)
The following is my attempt at trying to get a checkbox array consisting of 3 names Tom, Dick and Harry to INSERT into the table.
So if the user checks Tom and Harry say, these names will be inserted into the first_name field. When I click the Submit button, the values aren't being inserted into the first_name field. I'd greatly appreciate any help - ideally if you could load up the code for my page checkbox.php (shown below) and try creating the simple table i mention.
p.s. I know that i'm connecting to the database ok as i'm able to insert from a text box into a column on another table
<?php
require_once('mysqli_connect.php');
$fn=trim($_POST['first_name']);
foreach($_POST['first_name'] as $fn) {
$q1="INSERT INTO tbl_friends (first_name) VALUES ('$fn')";
mysqli_query($dbc,$q1);
}
?>
<html>
<body>
<form method="post" action="checkbox.php">
<input type="checkbox" name="first_name[]" value="Tom"> Tom <br />
<input type="checkbox" name="first_name[]" value="Dick"> Dick <br />
<input type="checkbox" name="first_name[]" value="Harry"> Harry <br />
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
Replies
Replied 09 Sep 2010 19:28:57
09 Sep 2010 19:28:57 Mark Taylor replied:
I managed to solve my problem. Here is the solution:
<html>
<body>
<form id="form1" name="form1" method="post" action="checkbox.php">
<label>
<input type="checkbox" name="first_name[]" value="Tom" id="test_0" />
Tom</label>
<br />
<label>
<input type="checkbox" name="first_name[]" value="Dick" id="test_1" />
Dick</label>
<br />
<label>
<input type="checkbox" name="first_name[]" value="Harry" id="test_2" />
Harry</label>
<br />
<input type="submit" name="button" id="button" value="Submit" />
</form>
</body>
</html>
<?php
require_once('mysqli_connect2.php');
foreach($_POST['first_name'] as $index => $val){
$q1="INSERT INTO tbl_friends (first_name) VALUES('$val')";
$r = mysqli_query($dbc,$q1);
}
?>
and the code contained in for mysqli_connect2.php is:
<?php
DEFINE ('DB_USER','mark');
DEFINE ('DB_PASSWORD','sprite');
DEFINE ('DB_HOST','localhost');
DEFINE ('DB_NAME','sitename');
$dbc=@mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME) OR die ('Could not connect to MySQL:'.mysqli_connect_error());
?>
where i'd set up a mysql user called mark with password sprite
<html>
<body>
<form id="form1" name="form1" method="post" action="checkbox.php">
<label>
<input type="checkbox" name="first_name[]" value="Tom" id="test_0" />
Tom</label>
<br />
<label>
<input type="checkbox" name="first_name[]" value="Dick" id="test_1" />
Dick</label>
<br />
<label>
<input type="checkbox" name="first_name[]" value="Harry" id="test_2" />
Harry</label>
<br />
<input type="submit" name="button" id="button" value="Submit" />
</form>
</body>
</html>
<?php
require_once('mysqli_connect2.php');
foreach($_POST['first_name'] as $index => $val){
$q1="INSERT INTO tbl_friends (first_name) VALUES('$val')";
$r = mysqli_query($dbc,$q1);
}
?>
and the code contained in for mysqli_connect2.php is:
<?php
DEFINE ('DB_USER','mark');
DEFINE ('DB_PASSWORD','sprite');
DEFINE ('DB_HOST','localhost');
DEFINE ('DB_NAME','sitename');
$dbc=@mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME) OR die ('Could not connect to MySQL:'.mysqli_connect_error());
?>
where i'd set up a mysql user called mark with password sprite