Good day guys.!

Just need a little help on how to create a php function that will loop all the rows with a text box in a table(name="gradetbl") in column 1 and save the value of the text box into mysql database when ok button is click. This is the content of the table loaded from mysql.

]
<table name="gradetbl" border="1">
<tr>
<td width="50"> RATING</td>
<td width="50"> SUBJECT CODE</td>
</tr>

while(&row=mysql_fetch_array($reclooptbl)){

<tr>
<td><label><input name="txtrating" type="text" id="txtrating size="24>
<td align="center"><?php echo $reclooptbl['courseid']; ?></td>
</tr>
}

When the user click the ok button, I want to update the 'tbl_grade' in mysql where 'courseid' = $reclooptbl and save the value of the txtrating in the current rows in the rating field of the 'tbl_grade'.

Im still stuck how to do this. Maybe i will assign the name of the txtrating with a counter incremented by 1 to get its data.

Pls help guys.

Thank you for giving time.

God bles us all.

Recommended Answers

All 5 Replies

Name the form elements as array by using [] at the end of name property.

1) Form:

<?php
while (&row=mysql_fetch_array($reclooptbl)) {
?>
  <tr>
    <td><input type="text" name="ratings[]" /></td>
    <td><input type="text" name="courseIds[]" value="<?php echo $row['courseid']; ?>" /></td>
  </tr>
<?php
}
?>

2) Process form when user click on submit button:

$ratings = $_POST['ratings'];
$courseIds = $_POST['courseIds'];
if (is_array($courseIds)) {
  for ($i = 0; $i < count($courseIds); $i++) {
    $sql = 'UPDATE tbl SET rating='.addslashes($ratings[$i]).' WHERE course_id='.addslashes($courseIds[$i]);
    // Execute query
    ...
  }
}

Name the form elements as array by using [] at the end of name property.

1) Form:

<?php
while (&row=mysql_fetch_array($reclooptbl)) {
?>
  <tr>
    <td><input type="text" name="ratings[]" /></td>
    <td><input type="text" name="courseIds[]" value="<?php echo $row['courseid']; ?>" /></td>
  </tr>
<?php
}
?>

2) Process form when user click on submit button:

$ratings = $_POST['ratings'];
$courseIds = $_POST['courseIds'];
if (is_array($courseIds)) {
  for ($i = 0; $i < count($courseIds); $i++) {
    $sql = 'UPDATE tbl SET rating='.addslashes($ratings[$i]).' WHERE course_id='.addslashes($courseIds[$i]);
    // Execute query
    ...
  }
}

Thank you for the response.!

But when i click the update button, it says: You have an error in you sql syntax: check syntax near 'and subjectcode=MCIT-11101 at line 1. this is how i build the update query..

<?php
mysql_select_db($database_nnscst_registrar, $nnscst_registrar);
$ratings =$_POST['ratings'];
$courseids=$_POST['courseids'];
$sy=$_POST['sy'];

if (is_array($courseids)) {

    for ($i = 0; $i < count($courseids); $i++) {

         $updategr ='UPDATE bsit_grades SET finalrating='.addslashes($ratings[$i]).',sy='.addslashes($sy[$i]).' WHERE studentid='.         $studidnum.' and subjectcode='.addslashes($courseids[$i]);
         mysql_select_db($database_nnscst_registrar, $nnscst_registrar);
         $result = mysql_query($updategr, $nnscst_registrar) or die(mysql_error());

    }
}
?>

Thank you for giving time...

try this;

WHERE studentid=('. $studidnum.' AND  subjectcode='.addslashes($courseids[$i])')';

try this;

WHERE studentid=('. $studidnum.' AND  subjectcode='.addslashes($courseids[$i])')';

thanks but it gets an error; it says: unexpected T_CONSTANT_ENCAPSED_STRING @ line 220. May i just ask, is it necessay to put addslash function on the variable array..?

<?php
mysql_select_db($database_nnscst_registrar, $nnscst_registrar);
$ratings =$_POST['ratings'];
$courseids=$_POST['courseids'];
$sy=$_POST['sy'];

if (is_array($courseids)) {

    for ($i = 0; $i < count($courseids); $i++) {

         $updategr ='UPDATE bsit_grades SET finalrating='.addslashes($ratings[$i]).', sy='.addslashes($sy[$i]).' WHERE studentid=('.         $studidnum.' and subjectcode='.addslashes($courseids[$i])')';
         mysql_select_db($database_nnscst_registrar, $nnscst_registrar);
         $result = mysql_query($updategr, $nnscst_registrar) or die(mysql_error());

    }
}
?>

this is how i array it.

<input type="text" name="ratings[]" id="ratings'' size="5">

and also will the query execute even if the where condition variable is empty.example the $studidnum.?

tnx for giving time

Thank you guys for giving time. Ive just solve the problem.. Its a matter of addslashes positioning.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.