Jump to content

How to insert multiple values of radio button with textbox using php mysqli


Recommended Posts

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
 
 
 
 
 
 

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 by samuel_lopez

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.

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.

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?

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

post-181301-0-78542000-1444983299_thumb.png

Edited by samuel_lopez

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
}

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 by samuel_lopez
  • 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 by Barand
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.