Log in

View Full Version : php code to compare user input with field in mySQL database



John0779
01-12-2010, 12:01 AM
Hi,
I am trying to develop a multiple choice web page where users select an answer option from a radiogroup (true/false) and submit the form. I have a database of questions (each row of the database has a main theme field, 5 question fields regarding that theme and 5 answers fields for each of the questions (true or false).
I am displaying the theme and questions as a recordset in a form. I then want the user to select the either true or false from radiogroups. I have searched internet forums for a php script that will compare this user input with fields in my database but have not had any luck. I am very new to webdesign and im using dreamweaver and MAMP (mySQL).


Ive tried this but im very new to php so its obviously wrong.


<?php
if($submit)
{
if ($row_Recordset1 ['ans1'] == RadioGroup1) {
echo "correct";
} else {
echo "incorrect";
}
}
?>

Any help would be really appreciated.

Thanks,

John

PS.

This is my code so far:



<?php require_once('Connections/conntest.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}

$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}

$currentPage = $_SERVER["PHP_SELF"];

$maxRows_Recordset1 = 1;
$pageNum_Recordset1 = 0;
if (isset($_GET['pageNum_Recordset1'])) {
$pageNum_Recordset1 = $_GET['pageNum_Recordset1'];
}
$startRow_Recordset1 = $pageNum_Recordset1 * $maxRows_Recordset1;

mysql_select_db($database_conntest, $conntest);
$query_Recordset1 = "SELECT test.regarding, test.q1, test.q2, test.q3, test.q4, test.q5, test.ans1, test.ans2, test.ans3, test.ans4, test.ans5 FROM test";
$query_limit_Recordset1 = sprintf("%s LIMIT %d, %d", $query_Recordset1, $startRow_Recordset1, $maxRows_Recordset1);
$Recordset1 = mysql_query($query_limit_Recordset1, $conntest) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);

if (isset($_GET['totalRows_Recordset1'])) {
$totalRows_Recordset1 = $_GET['totalRows_Recordset1'];
} else {
$all_Recordset1 = mysql_query($query_Recordset1);
$totalRows_Recordset1 = mysql_num_rows($all_Recordset1);
}
$totalPages_Recordset1 = ceil($totalRows_Recordset1/$maxRows_Recordset1)-1;

$queryString_Recordset1 = "";
if (!empty($_SERVER['QUERY_STRING'])) {
$params = explode("&", $_SERVER['QUERY_STRING']);
$newParams = array();
foreach ($params as $param) {
if (stristr($param, "pageNum_Recordset1") == false &&
stristr($param, "totalRows_Recordset1") == false) {
array_push($newParams, $param);
}
}
if (count($newParams) != 0) {
$queryString_Recordset1 = "&" . htmlentities(implode("&", $newParams));
}
}
$queryString_Recordset1 = sprintf("&totalRows_Recordset1=%d%s", $totalRows_Recordset1, $queryString_Recordset1);

?>



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<script src="SpryAssets/SpryValidationRadio.js" type="text/javascript"></script>
<link href="SpryAssets/SpryValidationRadio.css" rel="stylesheet" type="text/css" />
</head>

<body>
<form id="form1" name="form1" method="post"><MM_REPEATEDREGION SOURCE="@@rs@@">
<form id="form1" name="form1" method="post" action="">
<?php do { ?>
<p><?php echo $row_Recordset1['regarding']; ?></p>
<table width="200" border="1">
<tr>
<th scope="col"><?php echo $row_Recordset1['q1']; ?></th>
<th scope="col"><span id="spryradio1">
<label>
<input type="radio" name="RadioGroup1" value="true" id="RadioGroup1_0" />
true</label>
<br />
<label>
<input type="radio" name="RadioGroup1" value="false" id="RadioGroup1_1" />
false</label>
<br />
<span class="radioRequiredMsg">Please make a selection.</span></span></th>
</tr>
<tr>
<th scope="row"><?php echo $row_Recordset1['q2']; ?></th>
<td>&nbsp;</td>
</tr>
<tr>
<th scope="row"><?php echo $row_Recordset1['q3']; ?></th>
<td>&nbsp;</td>
</tr>
<tr>
<th scope="row"><?php echo $row_Recordset1['q4']; ?></th>
<td>&nbsp;</td>
</tr>
<tr>
<th scope="row"><?php echo $row_Recordset1['q5']; ?></th>
<td>&nbsp;</td>
</tr>
</table>
<br />
<label>submit
<input type="submit" name="submit" id="submit" value="Submit" />
</label>
<br />
<br />
<p>&nbsp;<a href="<?php printf("%s?pageNum_Recordset1=%d%s", $currentPage, min($totalPages_Recordset1, $pageNum_Recordset1 + 1), $queryString_Recordset1); ?>">Next</a></p>
<?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
<table width="200" border="1">
<tr> </tr>
</table>
</form>
</MM_REPEATEDREGION>
<p>&nbsp;</p>
<script type="text/javascript">
<!--
var spryradio1 = new Spry.Widget.ValidationRadio("spryradio1");
//-->
</script>
</body>
</html>
<?php
mysql_free_result($Recordset1);

mysql_free_result($Recordset1);
?>

Nile
01-12-2010, 11:45 PM
In the MySQL database, you'd make the field a BOOL and give it a value of 1 (being true), and 0 (being false);
Then, PHP:


if($_GET['test'] == $row['answer']){
}

John0779
01-16-2010, 11:58 PM
Hi,

Thanks so much for your help. The suggestion of the $_GET function got me onto the correct track.

With a bit of tweaking i got this solution:


<?php
if(isset($_POST['submit']))
{
if($_POST["RadioGroup1"] == $row_Recordset1['ans1']) {
echo "correct";
} else {
echo "incorrect";
}
}
?>



With the form action code as:


<form id="form1" name="form1" method="post" action="<?=$_SERVER['PHP_SELF'];?>">


Just out of interest:
I used the $_POST function instead of $_GET just because it happened to work and I havnt tried the $_GET in its place for fear of messing it up!
Is the only difference between these a security thing as the $_GET function shows up in the URL and therefore allows you to use the browser 'back' option to redisplay inputted data?

bluewalrus
01-17-2010, 12:05 AM
The post type in your form passes the value as a post value. If it were set to get it would go into the address bar. You can manually make links with values like http://www.site.com/index.php?id=12, index.php?id=12 or ?id=12, and with the $_get the value for id would be 12.


<form id="form1" name="form1" method="post"