Forum Moderators: coopster
I want to pass the values of multiple checkboxes to a table in the database. But of course how I am doing it now, only one value is passed and not the array.
Any help to get me past this mental block would be appreciated. Thanks.
Here is some sample code: (Please understand that this is only part of a larger file.
-----------------------------------------------
SECTION ONE:
-----------------------------------------------
// ********************* //
// BEGIN PRODUCT SECTION //
// ********************* //
$verify_products = "SELECT * FROM news_product order by product_rank DESC";
$verify_products_res = mysql_query($verify_products, $conn)
or die(mysql_error());
if (mysql_num_rows($verify_products_res) < 1) {
// *********************************** //
// NO PRODUCT EXISTS - DISPLAY MESSAGE //
// *********************************** //
$display .="<p align=\"center\">There are no products in the database.<br><a href=\"product_add.php\">Click to Add a Product.</a></p>";
// ****************** //
// END VERIFY PRODUCT //
// ****************** //
}else
//LIST PRODUCT(s)
{
// PULL PRODUCT DATA FOR LIST //
while ($newArray = mysql_fetch_array ($verify_products_res)){
$product_id = $newArray['product_id'];
$product_name = $newArray['product_name'];
$product_rank = $newArray['product_rank'];
//STRIPSLASHES
$product_name = stripslashes($product_name);
$product_rank = stripslashes($product_rank);
//DISPLAY LIST
$display .="<input type=\"checkbox\" name=\"product_id[]\" value=\"$product_id\">$product_name<br>";
//END DATA PULL FOR LIST
}
//END PRODUCT LIST
}
--------------------------------------------------------
SECTION TWO:
--------------------------------------------------------
<td colspan=\"2\"><input type=\"submit\" value=\"Submit\" name=\"submit_button\">
<input type=\"reset\" value=\"Reset Form\" name=\"reset_button\">
<a href=\"index.php\">Cancel Button Here</a>
</td>
</tr>
";
}
else {
// PROCESS FORM
// CLEAN DATA
$testimonial_title = mysql_real_escape_string($_POST['testimonial_title']);
$testimonial_hyperlink = mysql_real_escape_string($_POST['testimonial_hyperlink']);
// SEND DATA
$sql = "INSERT news_testimonial VALUES ('', '$customer_id', '$product_id', '$testimonial_title', '$testimonial_hyperlink', '$testimonial_rank')";
if (mysql_query($sql)) {
$display = "<center><br><br>Database Updated</center>";
$display .="<meta http-equiv=refresh content='5;URL=index.php'>";
} else {
die(mysql_error());
}
$_POST['testimonial_title'] and $_POST['testimonial_hyperlink'] suppose to be arrays from checkboxes? If so you are going about the procedure wrong. You are going to have to loop through the array to add them to the database, or maybe even implode [php.net] them. and Welcome to WebmasterWorld! :)
I'm looking to pull product_id and product_name from a table, then let the visitor select one or more products using the check box system. I was hoping to display the product name, but only use the product id in the new table called news_testimonials. This way, if the admin changes how the products are spelled, all associated testimonials are updated as well.
I agree implode sounds like the best option. I'm not sure how to make that work in this situation since I'm pulling the data from one table, to another (many to one).
Thanks again for your time!
JC
<form method="post">
<input type="checkbox" value="one" name="value[]" />One<br/>
<input type="checkbox" value="two" name="value[]" />Two<br/>
<input type="checkbox" value="three" name="value[]" />Three<br/>
<input type="checkbox" value="four" name="value[]" />Four<br/>
<input type="submit" value="Click me!" />
</form>
<?php
if(isset($_POST['value'])) {
$checkboxes = $_POST['value'];
$string = implode($checkboxes,",");
echo $string;
}
?>
Once you get the $string value, you add that string to the database like you would any other string. To retrieve the information, you do that normally, but then you explode [us3.php.net] the data back into an array for processing.
Try this method out first, and see where you get. Get back to use if you need any further assistance.
Good luck! :)
I'll try to sum up the code:
I created a form, query data for a list of customers - make a combobox, query a list of products - make check boxes, and allow the admin to select one customer, one or multiple products, and post a short testimonial to display on the website.
The combobox works fine, the check boxes are queried and displayed assigning appropriate values to each. But when the testimonial is submitted to the database only the last check box value is stored. I can't seen to find a way to use the implode when there aren't specific set values for each check box. I kinda think I need a way to store the array upon submitting the query.
Here is a little of the code:
$display .="<input type=\"checkbox\" name=\"product_id[]\" value=\"$product_id\">$product_name<br>";
..........skip to processing the form..............
if(isset($_POST['product_id'])) {
$checkboxes = $_POST['product_id'];
$string = implode($checkboxes,",");
echo $string;
}
// SEND DATA
$sql = "INSERT news_testimonial VALUES ('', '$customer_id', '$string', '$testimonial_title', '$testimonial_hyperlink', '$testimonial_rank')";
I'm sure it is a simple step I keep missing.
Thank you again for your time and help!
JC
Second, all I needed was:
$checkboxes = $_POST['product_id'];
$string = implode($checkboxes,",");
And, to update the database field from INT to VARCHAR. My original thought was to only have a number, not realizing the , needed the different type of field.
All that work for a simple issue.
Thank you again!
JC
Now I have the values in the table (example: 2,3,9).
How can I build a query to explode or preg_split() the values and list the names associated with them?
Table 1 has Product_ID with Product_Name. Table 2 has a record with several values to Product_ID (2,3,9).
Here is a bit of code to help:
/$verify_product = "SELECT * FROM news_product WHERE product_id='$product_string'";
while ($newArray = mysql_fetch_array ($verify_product_res)){
$product_id = $newArray['product_id'];
$product_name = $newArray['product_name'];
echo "$product_id is $product_name";
The product id is stored as 2,3,9 and I want to list the names associated with each ID. But all I get is the first value.
Glad you got everything sorted! :)