hello , can u plaese help me guys

I have tried several ways to UPDATE data to a mysql database from radio buttons that have 1,2,3,4 options..

for submitting i have used

if (isset($_POST['submit']))
      {

  $v1 = intval($_POST['v1']);
  $v2 = intval($_POST['v2']);
  $v3 = intval($_POST['v3']);
  $v4 = intval($_POST['v4']);
  $v5 = intval($_POST['v5']);
  $v6 = intval($_POST['v6']);
  $v7 = intval($_POST['v7']);
  $v8 = intval($_POST['v8']);
  $total = $v1 + $v2 + $v3 + $v4 + $v5 + $v6 + $v7 + $v8 ;
  $Percentage = $total / 32 * 10 ;


 mysql_query("UPDATE grades_presentation SET P1='$v1',P2='$v2',P3='$v3',P4='$v4',P5='$v5',P6='$v6',P7='$v7',P8='$v8',TOTAL='$total',Percentage='$Percentage' WHERE SID='$id' ") or die(mysql_error());
 header("Location: mark.php");

And this is some of the questions, I have used Checked to give me which button user have checked before!

    <tr>
    <th bgcolor='FF6600'> Clarity of speaking
    (Could you hear the speaker properly and clearly?)<font size="4" > </font></th>
    <td>  <input type="radio" name ="v1" value = "4" <?php echo ($v1=='4')?'checked':'' ?>   onclick="updateTotal();" /></td>
    <td>  <input type="radio" name ="v1" value = "3" <?php echo ($v1=='3')?'checked':'' ?>  onclick="updateTotal();" /></td>
    <td>  <input type="radio" name ="v1" value = "2" <?php echo ($v1=='2')?'checked':'' ?>  onclick="updateTotal();" /></td>
    <td>  <input type="radio" name ="v1" value = "1" <?php echo ($v1=='1')?'checked':'' ?>  onclick="updateTotal();" /></td>    
    </tr>

<tr>
<th bgcolor='FF6600'> Audio-visuals?(Use of OHP, Board etc.)<font size="4" > </font></th>
<td>  <input type="radio" name ="v2" value = "4"  <?php echo ($v2=='4')?'checked':'' ?>     onclick="updateTotal();" /></td>
<td>  <input type="radio" name ="v2" value = "3"  <?php echo ($v2=='3')?'checked':'' ?>    onclick="updateTotal();" /></td>
<td>  <input type="radio" name ="v2" value = "2"  <?php echo ($v2=='2')?'checked':'' ?>     onclick="updateTotal();"/></td>
<td>  <input type="radio" name ="v2" value = "1"  <?php echo ($v2=='1')?'checked':'' ?>    onclick="updateTotal();" /></td> 
</tr>

all working fine but when i changed some values and click submit nothing changed in the database !

Recommended Answers

All 4 Replies

Member Avatar for diafol

Can't see why you're updating with js and then using a separate php system to calculate. If you're using js to calculate a total, then you can either use ajax to make changes to the server on the hoof (no form submission req'd) or you can store the total in a hidden field, so when the form is submitted it takes the value from that - no need for the php calc. However, this can't be the case as your DB has separate columns for each option. I'd assume that you only need a single column (or two for each set of radiobuttons).

For two total DB fields without hidden form field:

$p1 = array_map('intval', $_POST['v1']);
$total_1 = array_sum($p1);
$p2 = array_map('intval', $_POST['v2']);
$total_2 = array_sum($p2);

Or with hidden field:

$total_1 = intval($_POST['total_1']);
$total_2 = intval($_POST['total_2']);    

sorry i don't get your point .. the problem is the DB doesn't update the new data even when i try to insert some id as a input type text! why used two sepertae total? the way i used as total cal works correctly and stored the Data in the database the only probelm is how to updated !

Member Avatar for diafol

why used two separate total?

You are inserting 10 items of data to the DB, where actually you only need ONE. I thought two would be less of a culture shock. :)

You do all this:

  $v1 = intval($_POST['v1']);
  $v2 = intval($_POST['v2']);
  $v3 = intval($_POST['v3']);
  $v4 = intval($_POST['v4']);
  $v5 = intval($_POST['v5']);
  $v6 = intval($_POST['v6']);
  $v7 = intval($_POST['v7']);
  $v8 = intval($_POST['v8']);

But only two of these will be set (v1 and v2). The others do not exist. v1 will have a value between 1-4 as will v2.

Apart from what diafol pointed out: the code you use for updating only shows a mysql_query using an $id. Are you sure you are connected to the database correctly, and that you've set $id to a correct value?

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.