troubleshooting Question

Insert multiple records and values using checkbox

Avatar of krv123
krv123Flag for United States of America asked on
2 Comments1 Solution1792 ViewsLast Modified:
I have a form with that displays a text box that you enter a value in. Next to the text box is a checkbox. I am having trouble submitting data to the database and keeping each line matched together.
<form method="POST">
    $query = "SELECT id,itemName,itemId FROM services";
    $result = mysql_query($query) or die("Can't create query: " . mysql_error());
    while ($row = mysql_fetch_array($result))
        echo "
				<td><input type=\"hidden\" name=\"item[]\" value=\"$row[itemId]]\"><input type=\"text\" name=\"value[]\" ></td>
				<td><input type='checkbox' name='checkbox[]' value='$row[id]'></td>
<input type="submit" name="save" value="Save" />
if (isset($_POST['save']))
    if ($_POST['checkbox'] == 0)
        echo 'no items selected';
    } else
    	// selected field values where checked
        foreach ($_POST['checkbox'] as $id)
        	// grab $_POST info create vars
            $userId = $_SESSION['userid'];
            $itemId = $_POST['item'];
            $itemValue = $_POST['value'];
            I have also used a for loop here to count the posted items so the vars would be $itemId[$i],$itemValue[$i]. Extracting as 				  array from form.
            They would not match up.
            // $queries is so that it enters multiple rows at once.
            $queries = array();
            $queries[] = "('$userId','$itemId','$itemValue')";
            // $queries[] = "('$userId','$itemId[$id]','$itemValue[$id]')";
            $piece = implode(", ", $queries);
            $query = "INSERT INTO cust_services (user,item,value) VALUES $piece";
            $result = mysql_query($query) or die(mysql_error());
Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros