samuel_lopez Posted October 15, 2015 Share Posted October 15, 2015 Hi to all, I have a problem in inserting values into php mysql. Please help me. Thank you I have this radio buttons and textboxes: <input type="radio" id="stat" name="improvement[<?php echo $row['grade_id'] ?>]" value="1"/> Very Good <input type="radio" id="stat" name="improvement[<?php echo $row['grade_id'] ?>]" value="2" /> Good <input type="radio" id="stat" name="improvement[<?php echo $row['grade_id'] ?>]" value="3"/> Average <input type="text" id = "remarks" name="remarks[<?php echo $row['grade_id'] ?>]"/>Remarks <input type="text" id = "rem" name="filename[<?php echo $row['grade_id'] ?>]"/> // Uploaded Card this is my insert //its values id = improvement id grade_id = grade id coming from other table status = 1, 2 or 3 remarks = coming from remarks textbox filename = coming from file upload foreach ($grade_id as $grade_id => $status) { $sql = sprintf( "Insert into tblimprovements(id,grade_id,Status_ID,Remarks,Attachment) Values('".$id."','".$grade_id."','".$status.'","'.$remarks.'","'.$filename."')", mysqli_real_escape_string($mysqli,$result), intval($id)); $result = mysqli_query($mysqli,$sql)or die(mysqli_error($mysqli)); } I want to save data in my database like this/ please refer to grade.png attached ID GRADE_ID STATUS_ID, REMARKS, ATTACHMENT 1 g-01 1 Keep up the good work cards/card1.doc 2 g-02 2 Need Imporvement cards/card2.doc 3 g-03 3 Need Big Improvement cards/card3.doc 4 g-04 1 Excellent card/card4.doc My problem here is how can I insert the remarks and filename value ? Please refer to the image attached. Your response is much appreciated. Thanks Quote Link to comment Share on other sites More sharing options...
ginerjm Posted October 15, 2015 Share Posted October 15, 2015 I'd like to see the code that you actually used to receive the input values from your html. PS - why do you use a loop on the radio button input when there will only be one? Quote Link to comment Share on other sites More sharing options...
samuel_lopez Posted October 15, 2015 Author Share Posted October 15, 2015 (edited) Hi ginerjm, Thank you for the response. this is what I used to recieve input values from my html $grade_id = $_POST['improvement']; $remarks = $_POST['remarks']; $filename = $_POST['filename']; I used loop because I have multiple grades to save. Edited October 15, 2015 by samuel_lopez Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted October 15, 2015 Share Posted October 15, 2015 What exactly is your problem? Are you getting an error? An unexpected result? Do you have a specific question? We need to know if you want us to help you. In any case, you definitely need to learn the basics of MySQLi. Your database code makes no sense. When you need to pass PHP values to a query, use a prepared statement. Literally inserting variables into query strings is mostly obsolete and dangerous, because it's easy to forget the escaping (as you can see). Never show SQL errors on the screen (I'm talking about the die(...) stuff). This is extremely confusing for legitimate users, and at the same time it helps attackers gain information about your system. Your users are not your database administrators, so SQL errors are none of their business. Log the error and show a generic error message. Don't use intval(), it has problems. You generally should never change the user input. If you want a specific format, then validate the input accordingly, e. g. with ctype_digit(). MySQLi can actually take care of the error reporting on its on: $mysqli_driver = new mysqli_driver(); $mysqli_driver->report_mode = MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT If you put this before your database connection code, MySQLi will throw an exception whenever a query fails. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted October 15, 2015 Share Posted October 15, 2015 YOu need to show us some contiguous code here. A radio button only returns one selection so there is no need for a loop. Show us the whole code where you build your html (the query that produces the data would be nice) and then how you completely receive the data. We can't possibly understand what you mean by having multiple ids in one radio button. Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted October 15, 2015 Share Posted October 15, 2015 He doesn't want multiple values for the radio buttons. He has multiple grades, and each grade has a set of radio buttons. The rest is unclear. Besides the fudged up MySQLi code, there seems to be an issue with doubles quotes vs. single quotes: Insert into tblimprovements(id,grade_id,Status_ID,Remarks,Attachment) Values('1','2','3","some remark","some attachment') Maybe that's the issue? Quote Link to comment Share on other sites More sharing options...
samuel_lopez Posted October 16, 2015 Author Share Posted October 16, 2015 (edited) I used loop because I get all the students in table students table students id name s-01 peter s-02 John s-03 Doe then i will assign status(radio button),remarks(text field),document(file) in every student..Assuming i had selected and input value for every students. <input type="radio" id="stat" name="improvement[<?php echo $row['grade_id'] ?>]" value="1"/> Very Good <input type="radio" id="stat" name="improvement[<?php echo $row['grade_id'] ?>]" value="2" /> Good <input type="radio" id="stat" name="improvement[<?php echo $row['grade_id'] ?>]" value="3"/> Average <input type="text" id = "remarks" name="remarks[<?php echo $row['grade_id'] ?>]"/>Remarks <input type="text" id = "filename" name="filename[<?php echo $row['grade_id'] ?>]"/> // Uploaded Card Like this. id name status(textfield) remarks(textfield) uploaded document(file) s-01 Peter pass excellent cards/peter.doc s-02 John failed needs improvement cards/john.doc s-03 Doe pass very good cards/doe.doc I want to save those info using only 1 button on another table name student_evaluation student_evaluation table will be like this when the button save was clicked id student id status remarks uploaded document 1 s-01 pass excellent cards/peter.doc 2 s-02 failed needs improvement cards/john.doc 3 s-03 pass very good cards/doe.doc All I want to know is how to save those data using 1 button. Please help. I was stuck with this Edited October 16, 2015 by samuel_lopez Quote Link to comment Share on other sites More sharing options...
samuel_lopez Posted October 16, 2015 Author Share Posted October 16, 2015 (edited) please see attached image. Thank you Edited October 16, 2015 by samuel_lopez Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted October 16, 2015 Share Posted October 16, 2015 So your question is how to get the improvement, remarks and filename values in your foreach loop? I would loop over the $_POST['improvment'] values, using the array key (which will contain the grade id) to reference the corresponding remarks and filename field values inside the loop. Example // loop over each submitted improvement value // the array key comtains the grade_id // the grade_id is use for referencing the corresponding remarks and filename values foreach ($_POST['improvment'] as $grade_id => $status) { $remarks = $_POST['remarks'][$grade_id]; $filename = $_POST['filename'][$grade_id]; // insert values into db } Quote Link to comment Share on other sites More sharing options...
samuel_lopez Posted October 16, 2015 Author Share Posted October 16, 2015 (edited) Hi Ch0cu3r, that was exactly what I want to do.Thanks. Will update you when i applied your solution. Thanks again. Edited October 16, 2015 by samuel_lopez Quote Link to comment Share on other sites More sharing options...
samuel_lopez Posted October 16, 2015 Author Share Posted October 16, 2015 (edited) Hi Ch0cu3r,No value of remarks and filename are saved into the database.this is my insert query. foreach ($_POST['improvment'] as $grade_id => $status){ $remarks = $_POST['remarks'][$id]; $filename = $_POST['filename'][$id]; $sql = sprintf( "Insert into student_evaluation(student_id,improvement,Remarks,document) Values('".$grade_id ."','".$improv."','".$remarks."','".$filename."')", mysqli_real_escape_string($mysqli,$result), intval($id)); $result = mysqli_query($mysqli,$sql)or die(mysqli_error($mysqli)); } Edited October 16, 2015 by samuel_lopez Quote Link to comment Share on other sites More sharing options...
Barand Posted October 16, 2015 Share Posted October 16, 2015 (edited) You are blindly using sprintf without a clue of how to use it.(It uses placeholders in the format string to indicate the type of expression passed in the parameters) You use $grade_id in the first line then $id in subsequent lines. The values you pass to sprintf seem to plucked out of nowhere. foreach ($_POST['improvment'] as $grade_id => $status) { $remarks = $_POST['remarks'][$grade_id]; $filename = $_POST['filename'][$grade_id]; $sql = sprintf("Insert into student_evaluation(student_id,improvement,Remarks,document) Values(null, '%s', '%s', '%s')", mysqli_real_escape_string($mysqli,$improvement), mysqli_real_escape_string($mysqli,$remarks), mysqli_real_escape_string($mysqli,$filename) ); $result = mysqli_query($mysqli,$sql)or die(mysqli_error($mysqli)); } [edit] A better way is to use a prepared statement $sql = "Insert into student_evaluation(student_id,improvement,Remarks,document) Values(null, ?, ?, ?)"; $stmt = $mysqli->prepare($sql); $stmt->bind_param('sss', $improvement, $remarks, $filename); foreach ($_POST['improvment'] as $grade_id => $status) { $remarks = $_POST['remarks'][$grade_id]; $filename = $_POST['filename'][$grade_id]; $sql->execute(); } Edited October 16, 2015 by Barand Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.