Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2012
    Posts
    4

    Calculate the total mark from radio input!

    hello

    i've a school evaluation form to ask users and the input will be a radio type!
    i want to know the correct way to add the input as a total and store it in the database

    my code is like

    Code:
    <?php
    
       session_start();
       $Load=$_SESSION['login_user'];
       include('../connect.php');
       $sql= "Select name from teacher where TID='$Load'";
       $username = mysql_query($sql);
        $id=$_SESSION['login_user'];
       
    				if (isset($_POST['submit']))
    		
    {  
      
       $v1 = $_POST['v1'];
       $v2 = $_POST['v2'];
       $v3 = $_POST['v3'];
       $total = $_POST['total'];
      
     mysql_query("INSERT into answr(P1,P2,P3,TOTAL)
     values('$v1','$v2','$v3','$total')") or die(mysql_error());
     header("Location: mark.php");
     }
     
     
    ?>
    
    		
    <!DOCTYPE HTML>
    <html>
    
    <head>
    
    
    
    
    <?php
    
    
    if(!isset($_SESSION['login_user']))
    
    header("Location:f.html");
    	
    
    
    ?>
      <title>Q&A Form</title>
    
    </head>
    
    <body>
    
    <div id="container">
    	<div id="header">
    		<h1>
    			
    		<img src="2.gif" width="880" height="125" alt="SPMS" />
    		</h1>
    	</div>
    	<div id="navigation">
    		<center>
    	<br>
    	<ul>
    	 &nbsp;
    			 
                           <?php 
    					   echo "Welcome, $Load";
    					   ?>
    					   &nbsp;
    					   <?php
    					   
    					    
    					     while ($row=Mysql_fetch_array($username)) 
                          {
    					    
    					  
                            echo '<td>' . $row['name'] . '</td>';
                          }
    					   ?>
    					   
    					   	
    	 <li class="selected"><a href="home.php">Home</a></li>
     
    		 </ul>
    		 </center> 
    	</div>
    	
    
        <center><form method="post" action=""  >
    	
    	<table style="width: 20%" >
    		<tr>
    	<th bgcolor='#CD3700'> Criteria <font size="5" > </font></th>
    	<th bgcolor='#CD3700'>  <font size="4" > </font></th>
    	</tr>
    	<tr>
        <th bgcolor='#CD3700'> Excellent <font size="3" > </font></th>
        <td bgcolor='#CD3700'>  4 <font size="4" > </font></td>
    	</tr>
    	<tr>
        <th bgcolor='#CD3700'> Good <font size="3" > </font></th>
        <td bgcolor='#CD3700'>  3 <font size="4" > </font></td>
    	</tr>
    	<tr>
        <th bgcolor='#CD3700'> Average <font size="3" > </font></th>
        <td bgcolor='#CD3700'>  2 <font size="4" > </font></td>
    	</tr>
    	<tr>
        <th bgcolor='#CD3700'> Poor <font size="3" > </font></th>
        <td bgcolor='#CD3700'>  1 <font size="4" > </td>
    	</tr>
    		
    	
    	
    	
    <font size='4'>
    	<table style="width: 70%">
    		<tr>
    <th bgcolor='FF6600'> School Evaluation <font size="4" > </font></th>
    <th bgcolor='FF6600'> <font size="4" > </font></th>
    <th bgcolor='FF6600'> <font size="4" > </font></th>
    <th bgcolor='FF6600'> <font size="4" > </font></th>
    <th bgcolor='FF6600'> <font size="4" > </font></th></tr>
    <tr>
    <th bgcolor='FF6600'> Criteria <font size="4" > </font></th>
    			<th bgcolor='FF6600'> 4<font size="4" > </font></th>
    			<th bgcolor='FF6600'> 3<font size="4" > </font></th>
    			<th bgcolor='FF6600'> 2<font size="4" > </font></th>
    			<th bgcolor='FF6600'> 1<font size="4" > </font></th>
    			
    		</tr>
    <tr>
    <th bgcolor='FF6600'> Your school attendance<font size="4" > </font></th>
    <td>  <input type="radio" name ="v1" value = "4"	checked = "checked" /></td>
    <td>  <input type="radio" name ="v1" value = "3"	 /></td>
    <td>  <input type="radio" name ="v1" value = "2"	 /></td>
    <td>  <input type="radio" name ="v1" value = "1"	 /></td>	
    </tr>
    		
    <tr>
    <th bgcolor='FF6600'> Your grades  <font size="4" > </font></th>
    <td>  <input type="radio" name ="v2" value = "4"	checked = "checked" /></td>
    <td>  <input type="radio" name ="v2" value = "3"	 /></td>
    <td>  <input type="radio" name ="v2" value = "2"	 /></td>
    <td>  <input type="radio" name ="v2" value = "1"	 /></td>	
    </tr>
    
    <tr>
    <th bgcolor='FF6600'>Your self-control <font size="4" > </font></th>
    <td>  <input type="radio" name ="v3" value = "4"	checked = "checked" /></td>
    <td>  <input type="radio" name ="v3" value = "3"	 /></td>
    <td>  <input type="radio" name ="v3" value = "2"	 /></td>
    <td>  <input type="radio" name ="v3" value = "1"	 /></td>	
    </tr>		
    
    
    		</tr>
    	</table>
    
    
    	<br>
    	<a href="evaE.php">  <td><input type="submit" name="submit" value="Submit">
    	
        <input type="reset" name="clear" value="clear" style="width: 70px"></td>
    <?php
    $total = $v1+ $v2 + $v3;
    	
    ?>	
    	
    
     </form> 
    </center>
    </div>
    
    
    </body>
    </html>


    all the criteria that is input from user are stored in the database
    but the total isn't .. it stored 0 each time .
    is this the correct way to calculate it and store it ?
    Code:
    <?php
    $total = $v1+ $v2 + $v3;
    	
    ?>

  2. #2
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    12,448
    what datatypes are p1,p2 & p3
    looking at your SQL it suggests they are of type string/text
    if they are numeric then remove the quote marks

    you also need to beef up the security / checking of data
    what happens if the teachers name is not found?
    what happens if you cannot save the data.

    Stylistically intermixing PHP and HTML is not a smart idea.
    I'd rather be riding on the Tiger 800 or the Norton

  3. #3
    Join Date
    Oct 2012
    Posts
    4
    p1,p2 & p3 type is int coz input will be one of these (1,2,3,4)
    hope to help me will total code
    thanx

  4. #4
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    12,448
    so if they are integer then at the very least remove the quote marks from your INSERT statement around these variables

    it helps when debugging scripts to assign the SQL to a variable which you cna then examine, rather than create the SQL inside a function call

    Also its considered 'bad form' to store derived data such as $Total at the same time as you store its constituent parts

    if you don't do anything with $V1 etc then there is little point in assigning them to variables before creating your SQL variable

    PHP Code:
    $strSQL "INSERT into answr(P1,P2,P3,TOTAL)
    values("
    .$_POST['v1'].",".$_POST['v2'].",".$_POST['v3'].",".$_POST['TOTAL'];
     
    mysql_query($strSQL) or die(mysql_error()); 
    if you are struggling on an insert then make certain your SQL is sane by debugging it (either use a debugger (Im guessing you are not using NetBeans or Eclipse or other IDE that supports debugging, or just send the value of the SQL variable straight ot he output stream using die(echo $strSQL)
    I'd rather be riding on the Tiger 800 or the Norton

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •