If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > PHP > Calculate the total mark from radio input!

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old
Registered User
 
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;
	
?>
Reply With Quote
  #2 (permalink)  
Old
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 11,649
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 my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #3 (permalink)  
Old
Registered User
 
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
Reply With Quote
  #4 (permalink)  
Old
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 11,649
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 my Versys or my Tiger 800 let alone the Norton
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On