I want to store the multiple checkbox values to store in a single field. I use that link http://www.mindfiresolutions.com/Storing-array-data-to-MySQL-using-PHP-1296.php. But i dont get the result.Give Help to find that problem.
Set your column as 'set' (specify the all possible values.) data type and than run the below query.
$comma_separated = implode(",", $values);
$insert_query = "INSERT INTO TABLE_NAME(col_name) VALUES('$comma_separated')";
$result_insert = mysql_query($insert_query);
I hope this will solve your problem.
I hope this will helpful
<html>
<body>
<form action="" method="post">
<p><input type="checkbox" name="color[]" value="red" />Red</p>
<p><input type="checkbox" name="color[]" value="blue" />Blue</p>
<p><input type="checkbox" name="color[]" value="orange" />orange</p>
<input type="submit" value="submit" name="sub" />
</form>
<?php
if(isset($_POST['sub']))
{
mysql_connect("localhost","root","") or die("Server Could not be connected");
mysql_select_db("gobinath") or die("database connection problam");
$color=implode(',',$_POST['color']);
mysql_query("insert into mcheck values('','$color')") or die("insert problam");
}
?>
</body>
</html>
Related
I have searched everywhere and still cannot find the answer. Can anybody help?
I have a PHP form that consists of a textbox (with data that will go into column name_id) and a checkbox, that has the option to go to 5 different MySQL tables:
'table A'
'table B'
'table C'
'table D'
'table E'
People can choose which table they want, and the name_id will go to the tables selected.
How can I do this?
my Answere in different from #RandD-SexyBoy- . my sql query is different from #RandD-SexyBoy- he has used $sql = "INSERT INTO $tables[$i](nameID_column); with out VALUES (:nameID_column)
were is have used $sql->prepare("INSERT INTO ".$v." (name_id) VALUES (?)"
Here are 3 cases the user responds
case first : user may select a single table
case second : user may select multiple tables. here we must use foreach() loop and dynamic sql queries.
case third : user may not select any table in this case we must give user a message table not selected
html form :
`
<html>
<head>
</head>
<body>
<form action="" method="post">
<input type="text" name="name_id" required>
<p>select your table to add data</p>
<input type="checkbox" name="tables[]" value="tblA">Table A<br>
<input type="checkbox" name="tables[]" value="tblB">Table B<br>
<input type="checkbox" name="tables[]" value="tblC">Table C<br>
<input type="checkbox" name="tables[]" value="tblD">Table D<br>
<input type="checkbox" name="tables[]" value="tblE">Table E<br>
<input type="submit" name="submit">
</form>
</body>
</html>
`
php file :
<?php
$con = new mysqli('localhost','root','admin','demo');
if(!$con){
die("Connection ".$con->connect_error);
}
if(isset($_POST['submit'])){
$name_id = $_POST['name_id'];
$tables = $_POST['tables'];
if(!empty($tables)){
foreach($tables as $key=>$v){
$sql = $con->stmt_init();
if($sql->prepare("INSERT INTO ".$v." (name_id) VALUES (?)")){
$sql->bind_param($name_id);
$sql->execute();
echo "DATA INSERTED";
}
else
{
echo "Error".$con->error;
}
}
}
else
{
echo "You have not selected tables";
}
}
?>
So one naive approach could be, when the user submits the form, on your php side, you check which checkboxes are ticked and your condition could be something like this
if(checkbox_for_table_A) {// insert query}
if(checkbox_for_table_B) {// insert query}
if(checkbox_for_table_C) {// insert query}
if(checkbox_for_table_D) {// insert query}
if(checkbox_for_table_E) {// insert query}
This way if one checkbox is selected it will save into that table, if more than one tables are selected, it will go in that table.
Hope this helps! Ask any doubt you get in the comment section.
Adapting from sources : post checkbox value and tutorial http://www.mustbebuilt.co.uk/php/insert-update-and-delete-with-pdo/, you can use this approach :
HTML Form :
<form action="checkbox-form.php" method="post">
<input type="text" name="name_id" /><br />
Select your database: <br/>
<input type="checkbox" name="formTables[]" value="A" />Table A<br />
<input type="checkbox" name="formTables[]" value="B" />Table B<br />
<input type="checkbox" name="formTables[]" value="C" />Table C<br />
<input type="checkbox" name="formTables[]" value="D" />Table D<br />
<input type="checkbox" name="formTables[]" value="E" />Table E<br />
<input type="submit" name="formSubmit" value="Submit" />
</form>
Now the PHP script :
<?php
$pdo_connection = new PDO('mysql:host=localhost;dbname=test', 'demouser', 'demopass');
$tables = $_POST['formTables'];
if(empty($tables))
{
echo("You didn't select any tables.");
}
else
{
$N = count($tables);
echo("You selected $N table(s): ");
for($i=0; $i < $N; $i++)
{
$sql = "INSERT INTO $tables[$i](nameID_column);
$stmt = $pdo_connection->prepare($sql);
$stmt->bindParam(':nameID_column', $_POST['name_id'], PDO::PARAM_STR);
$stmt->execute();
echo("Your ID Name was inserted into table $tables[$i] ! <br /> ");
}
}
?>
I have php code like this :
<?php
if(isset($_POST['submit']))
{
if(!empty($_POST['vehicles']))
{
$vehicles=$_POST['vehicles'];
$hostname='localhost';
$username='root';
$password='root';
$dbname='pet';
$connect=mysqli_connect($hostname,$username,$password,$dbname) or die("can't connect to server");
$query="UPDATE information SET transportation='$vehicles'";
$query=mysqli_query($connect,$query) or die("can't execute query");
echo "inserted";
}
else
{
echo "error";
}
}
else
{
echo "choose your vehicle";
}
?>
<!DOCTYPE html>
<html>
<head>
<title> Select transportation you have</title>
</head>
<body><h2>Select transportation you have</h2>
<form action="#" method="POST">
<input type="checkbox" name="vehicles" value="bike"/>I have bike<br/>
<input type="checkbox" name="vehicles" value="car"/>I have car<br/>
<input type="checkbox" name="vehicles" value="motor"/>I have motor<br/>
<input type="submit" name="submit" value="submit"/>
</form>
</html>
when i choose more than one options there is only one value in my database.For example i choose bike and motor.There is only motor in the database
change the name of the inputs to be an array by add [] at the end of the name
<input type="checkbox" name="vehicles[]" value="bike"/>I have bike<br/>
<input type="checkbox" name="vehicles[]" value="car"/>I have car<br/>
<input type="checkbox" name="vehicles[]" value="motor"/>I have motor<br/>
then in php you will be able to access them using the array syntax, and you can simply use implode to make a comma separated list of the vehicles.
$_POST['vehicles'][0]
$_POST['vehicles'][1]
$_POST['vehicles'][2]
//etc...
$vehicles = implode(",",$_POST['vheicles']);
$query="UPDATE information SET transportation='$vehicles'";
//$query would contain something like:
//UPDATE information SET transportation='bike,car'
Of course do sanitation on the POST variables before putting them in the db.
You need to change the html to:
<input type="checkbox" name="vehicles[]" value="bike"/>I have bike<br/>
<input type="checkbox" name="vehicles[]" value="car"/>I have car<br/>
<input type="checkbox" name="vehicles[]" value="motor"/>I have motor<br/>
Change the PHP to:
$vehicles=implode(',',$_POST['vehicles']);
I have an HTML form with several various text and checkbox fields. I have the text fields posting to the correct table but the checkbox responses are not posting at all (intended to post to a separate table).
Here's my HTML form:
<!Doctype html>
<html>
<?php include 'C:\xampp\htdocs\paxdb\head.php';
include 'config/menu.php';?>
<div id="dataentry">
<!--This section is the demographic text field area-->
<form method="post" action="dataentered.php">
First Name: <input type="text" id="First_Name" name="First_Name"/></br>
</br>
Last Name: <input type="text" id="Last_Name" name="Last_Name"/></br>
</br>
E-mail: <input type="text" id="email" name="email"/></br>
</br>
<!--This section is the age range checkbox selection area-->
<p><u><b>Age Range</b></u></p>
<input type="checkbox" name="age[]" id="20-25" value="1"/> 20-25</br>
<input type="checkbox" name="age[]" id="26-30" value="1"/> 26-30</br>
<input type="checkbox" name="age[]" id="31-35" value="1"/> 31-35</br>
<input type="checkbox" name="age[]" id="36-40" value="1"/> 36-40</br>
<input type="checkbox" name="age[]" id="41-45" value="1"/> 41-45</br>
</div>
<p><u><b>What City or region would you like to visit in the US or Canada?:</b></u></p>
<textarea name="comment2" rows="4" cols="50"></textarea>
<?php include 'footer.php';?>
</div>
</body>
</html>
and here is the PHP code I am trying but only have the text fields working:
<html>
<?php
$host="localhost";
$username="someusername";
$password="somepassword";
$dbname="somedbname";
$dbc = mysql_connect($host, $username, $password, $dbname);
if (!$dbc)
{
die('Error connecting to MySQL server' . mysql_error());
}
mysql_select_db($dbname, $dbc);
//send pax data to pax database table
$first_name=$_POST['First_Name'];
$last_name=$_POST['Last_Name'];
$email=$_POST['email'];
mysql_query("INSERT INTO pax (First_Name, Last_Name, email)
VALUES('$first_name','$last_name','$email')");
//send age checkbox data to age database table
$age = $_POST['age'];
foreach($age as $range) mysql_query("INSERT INTO age ($age) VALUES ('$range')") or die (mysql_error());
mysql_close($dbc);
Any help is appreciated.
EDIT To clarify: the mysql table 'age' has the following fields:
age_id (key),
pax_id (index for the text field data at the beginning of the form),
20-25
26-30
and so on through the age ranges.
before you try to actually run mysql queries, you should test to make sure the queries youre constructing have the correct information in them.
You could do something like this to quickly check:
foreach($age as $range) {
print "INSERT INTO age ($age) VALUES ('$range')";
}
Also, you shouldn't be sticking $_POST data straight into your queries, someone could easily write SQL code into a field and delete your database. Look up the topic of "mysql input sanitation"
I have edited the code for you
<!Doctype html>
<html>
<?php include 'C:\xampp\htdocs\paxdb\head.php';
include 'config/menu.php';?>
<div id="dataentry">
<!--This section is the demographic text field area-->
<form method="post" action="dataentered.php">
First Name: <input type="text" id="First_Name" name="First_Name"/></br>
</br>
Last Name: <input type="text" id="Last_Name" name="Last_Name"/></br>
</br>
E-mail: <input type="text" id="email" name="email"/></br>
</br>
<!--This section is the age range checkbox selection area-->
<p><u><b>Age Range</b></u></p>
<!-- Change your checkbox value -->
<input type="checkbox" name="age[]" id="20-25" value="20-25"/> 20-25</br>
<input type="checkbox" name="age[]" id="26-30" value="26-30"/> 26-30</br>
<input type="checkbox" name="age[]" id="31-35" value="31-35"/> 31-35</br>
<input type="checkbox" name="age[]" id="36-40" value="36-40"/> 36-40</br>
<input type="checkbox" name="age[]" id="41-45" value="41-45"/> 41-45</br>
</div>
<p><u><b>What City or region would you like to visit in the US or Canada?:</b></u></p>
<textarea name="comment2" rows="4" cols="50"></textarea><br/>
<input type="submit" name="submit" value="Submit"/>
<?php include 'footer.php';?>
</div>
</body>
</html>
You cannot insert the array($age) directly into the database as you have done in INSERT statement...One of the solution that i did is convert the array into string and then insert it into database as shown...
<?php
$host="localhost";
$username="someusername";
$password="somepassword";
$dbname="somedbname";
$dbc = mysql_connect($host, $username, $password, $dbname);
if (!$dbc)
{
die('Error connecting to MySQL server' . mysql_error());
}
mysql_select_db($dbname, $dbc);
//send pax data to pax database table
$first_name=$_POST['First_Name'];
$last_name=$_POST['Last_Name'];
$email=$_POST['email'];
mysql_query("INSERT INTO pax (First_Name, Last_Name, email)
VALUES('$first_name','$last_name','$email')");
//send age checkbox data to age database table
$age = $_POST['age'];
$my_range = "";
foreach($age as $range)
$my_range = $my_range . $range . " ";
//You have written this query wrong
mysql_query("INSERT INTO age(age) VALUES ('$my_range')") or die (mysql_error());
mysql_close($dbc);
While retrieving it from the database you can use expode() to get each age range...Here is the sample code
$range_string = "20-25 26-30 31-35";
$range_array = explode(" ", $range_string);
echo $range_array [0]; // 20-25
echo $range_array [1]; // 26-30
echo $range_array [2]; // 31-35
You might also want to look at http://www.html-form-guide.com/php-form/php-form-checkbox.html for some information on checkboxes...
Hope this helps
I am trying to insert the value of this multiple checklist into the db column. This code not working. Can anyone spot the problem?
My database consists of a table called "colors" and one column called "color".
<?php
// connect to database
require "mysql_connect.php";
?>
<?php
// get value from the form
$color = $_POST['color'];
foreach($_POST['color'] as $colors){
$insert = mysql_query("INSERT INTO colors (color) VALUES ('$color')");
}
?>
<form action="add_color.php" method="post" enctype="multipart/form-data" name="colorform" id="colorform">
<input type="checkbox" name="color[]" value="black" /> Black
<input type="checkbox" name="color[]" value="red" /> Red
<input type="checkbox" name="color[]" value="blue" /> Blue
<input type="checkbox" name="color[]" value="white" /> White
<input name="submit" type="submit" value="Add color" />
</form>
Thanks
This is a nice way to add your colors
<?php
require "mysql_connect.php";
// connect to database
$colors=array();
// get value from the form
if (isset($_POST['color'])) $colors = $_POST['color'];
foreach($colors as $color)
{
mysql_query ("INSERT INTO colors ('color') VALUES ('$color')");
}
?>
<form action="add_color.php" method="post" enctype="multipart/form-data" name="colorform" id="colorform">
<input type="checkbox" name="color[]" value="black" /> Black
<input type="checkbox" name="color[]" value="red" /> Red
<input type="checkbox" name="color[]" value="blue" /> Blue
<input type="checkbox" name="color[]" value="white" /> White
<td><input name="submit" type="submit" value="Add color" />
</form>
if (isset($_POST['color']))
This condition is important because it will prevent an indexing error in case the array is empty
$colors=array();
Also, do declare your variables to prevent getting undeclared varibles, previously, in your code, this will happen if the user does not specify any color
Remember PHP is server-side and thus getting errors on PHP create loopholes for attacks. Try to read about PHP Best Practices, Its very impotant
Hopes it helps :-)
I would also suggest that you sanitize your from inputs before inserting into your database. You don't mention what type your color column is, could be a mismatch there as well.
When you say INSERT INTO $colors -- is that what you mean? Your table name is variable? You should probably have a proper table name in place of $colors.
In addition, you have used $color which I don't see defined, you probably meant to use $colors so it should be more like this:
INSERT INTO tblColors (color) VALUES ('$colors')
To check your return value to see what error you're getting:
$query = "INSERT INTO tblColors (color) VALUES ('$colors')";
$insert = mysql_query($query) or die("A MySQL error has occurred.<br />Your Query: " . $query . "<br /> Error: (" . mysql_errno() . ") " . mysql_error());
$insert = mysql_query("INSERT INTO $colors (color) VALUES ($color)");
Change it to:
$insert = mysql_query("INSERT INTO colors_table_name (color) VALUES ($color)");
Also, please check the return value of insert, maybe you are getting errors?
First obvious problem was that the table name was being replaced with the color because of the variable, is this the desired effect?
<?php
// connect to database
require "mysql_connect.php";
?>
<?php
// get value from the form
$colors = $_POST['color'];
foreach($colors as $color){
$insert = mysql_query("INSERT INTO colors (color) VALUES ($color)");
}
<form action="add_color.php" method="post" enctype="multipart/form-data" name="colorform" id="colorform">
<input type="checkbox" name="color[]" value="black" /> Black
<input type="checkbox" name="color[]" value="red" /> Red
<input type="checkbox" name="color[]" value="blue" /> Blue
<input type="checkbox" name="color[]" value="white" /> White
<td><input name="submit" type="submit" value="Add color" />
</form>
You've got your variables backwards, SQL syntax errors, SQL injection vulnerabilities, and a total lack of error handling
$color = $_POST['color']; <---stuff the POST data array into $color
foreach($_POST['color'] as $colors){ <--- loop over the POST data directly
$insert = mysql_query("INSERT INTO colors (color) VALUES ($color)");
^^^^^^---insert the array
^^^^^^---no quotes
You use $colors (with an S) to store the individual colors, but then insert $color, which is an array.
Never assume that a query has suceeded. If you'd have the bare minimum or die(...) error handling, you've have seen why your queries were failing:
foreach($_POST['color'] as $color) {
$safe_color = mysql_real_escape_string($color);
$result = mysql_query("INSERT INTO colors (color) VALUES ('$safe_color');") or die(mysql_error());
}
How do I wrap the selections in a session that can be carried over multiple pages?
This is what I have:
session_start();
<form action="checkbox-form.php" method="post">
Select your options<br />
<input type="checkbox" name="options[]" value="A" />A<br />
<input type="checkbox" name="options[]" value="B" />B<br />
<input type="checkbox" name="options[]" value="C" />C<br />
<input type="submit" name="formSubmit" value="Submit" />
</form>
var_dump($_POST['options']);
foreach($_POST['options'] as &$option){
mysql_real_escape_string($option);
}
$insert = "INSERT INTO submitted (statuses)
VALUES ('". implode(",", $_POST['options']) ."')";
$query = mysql_query($insert) or die ("Error: ".mysql_error());
So, how do I put $_POST['options']; in a session. I tried doing
$_SESSION['options'] = mysql_real_escape_string($_SESSION['options']);
When i do that i get "array" as the result.
You are storing a array in session variable and complaining that its returning me a array!!!
Of course it will return an array...to access a particular key of the same use..
$_SESSION['options'][0]
$_SESSION['options'][1]
$_SESSION['options'][2]
which consecutively correspond to your A,B,C checkboxes on the form...
simply do it
session_start();
$_SESSION['options'] = $_POST['options'];
and you can access checkbox value by
$_SESSION['options'][$i] where $i <count($_SESSION['options'])