I am trying to insert checkbox values form a form into different rows in a table using this php code but it seems not to be working <?php $con=mysqli_connect("localhost","wilson_3","qwerty123","wilson_3"); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } $values = $_POST['form']['AnyAssociatedSymptoms']; foreach($values as $value) { $sql = sprintf("INSERT INTO `dr_obhelpdesk3_field_values`(`field_id`,`department_id`, `ticket_id`,`value`) VALUES('13','2', '100', '$value')"); } if (!mysqli_query($con,$sql)) { die('Error: ' . mysqli_error($con)); } echo "Good News. Records Updated"; ?> <br /> <?php // Print arrays print_r($values); ?> PHP: Only one value is inserted in the table in one row. Please help.
Uhm, silly question, are you actually setting those inputs in your markup as: name="[form][AnyAssociatedSymptoms][0]" name="[form][AnyAssociatedSymptoms][1]" name="[form][AnyAssociatedSymptoms][2]" Code (markup): and so forth? That asked, you're using mysqli, use if how it's MEANT to be used, not how the old mysql_ functions worked. You have no business wasting time building a query string by adding values into it and/or screwing around with sprintf. Whoever taught you that needs a good swift boot in the patoot. They're called "prepared queries", USE THEM. Particularly since they are basically DESIGNED from the start to do exactly what you are trying to do here! Some better error handling and condition checking of the inputs wouldn't hurt either. <?php $con = new mysqli('localhost','wilson_3','qwerty123','wilson_3'); if (mysqli->connect_error) { echo 'Failed to connect to MySQL: ' . mysqli_connect_error(); // since you didn't DIE, you should at the very least ELSE. } else if (isset($_POST['form']['AnyAssociatedSymptoms'])) { $statement = $con->prepare(' INSERT INTO dr_obhelpdesk3_field_values ( field_id, department_id, ticket_id, value ) VALUES ( 13, 2, 100, ? ) '); $statement->bindParam('s',$value); $count = 0; foreach ($_POST['form']['AnyAssociatedSymptoms'] as $value) { $statement->execute(); $count += $statement->affected_rows; } echo ' Inserted ', $count, ' Rows out of ', count($_POST['form']['AnyAssociatedSymptoms']),' CheckBoxes sent from the form<br />'; } else { echo 'Invalid Form Submit<br />'; } ?> Code (markup): Though with all those ID in the query, are you sure your table structure doesn't have at least one of those set to unique? Really though, I'm wondering if the markup for your form might also be contributing to your problems -- when you're nesting array passing three deep in the name attribute, there's probably something wrong there.
Here is my HTML markup <form action="process.php" method="post" name="form"> <div class="formBody"> <p> <input name="form[AnyAssociatedSymptoms[]" type="checkbox" value="Fever" id="AnyAssociatedSymptoms0"> <label for="AnyAssociatedSymptoms0">Fever</label> </p> <p> <input name="form[AnyAssociatedSymptoms[]" type="checkbox" value="Weight loss" id="AnyAssociatedSymptoms1"> <label for="AnyAssociatedSymptoms1">Weight loss</label> </p> <p> <input name="form[AnyAssociatedSymptoms][]" type="checkbox" value="Night sweats" id="AnyAssociatedSymptoms2"> <label for="AnyAssociatedSymptoms2">Night sweats</label> </p> <p> <input name="form[AnyAssociatedSymptoms][]" type="checkbox" value="Yellowness of eyes" id="AnyAssociatedSymptoms3"> <label for="AnyAssociatedSymptoms3">Yellowness of eyes<br /> </label> <input name="form[AnyAssociatedSymptoms][]" type="checkbox" value="Swelling legs" id="AnyAssociatedSymptoms4"> <label for="AnyAssociatedSymptoms4">Swelling legs</label> </p> <p> <input name="form[AnyAssociatedSymptoms][]" type="checkbox" value="Chest Pain" id="AnyAssociatedSymptoms5"> <label for="AnyAssociatedSymptoms5">Chest Pain</label> </p> <p> <input name="form[AnyAssociatedSymptoms][]" type="checkbox" value="Difficulty in breathing" id="AnyAssociatedSymptoms6"> <label for="AnyAssociatedSymptoms6">Difficulty in breathing</label> </p> </div> <input type="submit"> </form> Code (markup):
@deathshadow your code return this error. Parse error: syntax error, unexpected '->' (T_OBJECT_OPERATOR) on line 4 I am learning php
name="form[AnyAssociatedSymptoms[]" Do you see a problem there? You're not closing as many [] as you're opening.... Though I'm wondering just what makes labels and inputs grammatical paragraphs, why you are using a DIV instead of a FIELDSET, and why you have your submit as direct child of form where it's invalid/malformed... just saying. Is there any real reason you have that "form[" part at the beginning? Seems like a waste unless you have multiples of the same form on the page. Likewise I don't trust [] for values on name, I'd really suggest that since you seem to be able to generate and/or plug in numbers for the ID, use that same number in the brackets for name. I also wonder why you have a NAME on the FORM -- since the only reason to do that is decade and a half out of date Nyetscape 4 style scripting...
@deathshadow . Thanks for your help. Ahhh I had a typo error in my HTML mark up. Fixed that I an going to have multiple forms thats why I used the "form[ " part NOw when I run the form I get this error message Fatal error: Call to undefined method mysqli_stmt::bindParam() in /process.php on line 19
Got I fixrd the above error. I changed $statement->bindParam('s',$value); PHP: to $statement->bind_param('s',$value); PHP: and everything works well now Thanks a lot @deathshadow