php code to compare user input with field in mySQL database
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 Code:
<?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 Code:
<?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> </td>
</tr>
<tr>
<th scope="row"><?php echo $row_Recordset1['q3']; ?></th>
<td> </td>
</tr>
<tr>
<th scope="row"><?php echo $row_Recordset1['q4']; ?></th>
<td> </td>
</tr>
<tr>
<th scope="row"><?php echo $row_Recordset1['q5']; ?></th>
<td> </td>
</tr>
</table>
<br />
<label>submit
<input type="submit" name="submit" id="submit" value="Submit" />
</label>
<br />
<br />
<p> <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> </p>
<script type="text/javascript">
<!--
var spryradio1 = new Spry.Widget.ValidationRadio("spryradio1");
//-->
</script>
</body>
</html>
<?php
mysql_free_result($Recordset1);
mysql_free_result($Recordset1);
?>