Results 1 to 4 of 4

Thread: php code to compare user input with field in mySQL database

  1. #1
    Join Date
    Jan 2010
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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>&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"$currentPagemin($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);
    ?>
    Last edited by Snookerman; 01-12-2010 at 11:14 AM. Reason: please use [PHP][/PHP] tags for php code

  2. #2
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    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:
    PHP Code:
    if($_GET['test'] == $row['answer']){

    Jeremy | jfein.net

  3. The Following User Says Thank You to Nile For This Useful Post:

    John0779 (01-16-2010)

  4. #3
    Join Date
    Jan 2010
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    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 Code:
    <?php 
            
    if(isset($_POST['submit'])) 
            {
    if(
    $_POST["RadioGroup1"] == $row_Recordset1['ans1']) { 
            echo 
    "correct"
            } else { 
            echo 
    "incorrect"
            }  
    }
    ?>


    With the form action code as:

    PHP Code:
    <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?
    Last edited by Snookerman; 01-17-2010 at 12:26 PM. Reason: please use [php] [/php] tags for php code

  5. #4
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    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"
    Corrections to my coding/thoughts welcome.

Bookmarks

Posting Permissions

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