MYSQL Query not working - Contact Form

Discussion in 'PHP' started by mrwizard3, Jul 11, 2009.

  1. #1
    HI,

    I have a contact form which validates fields, adds entrys to database and emails data to admin.ALL WORKS FINE!!

    I want to add a query so that if same date of birth already exists in database it should change repeat variable to no. or else yes

    this is how i have done

    I have used echo command to know the results and all the time it says yes
    
    
    <?php    include_once( "lib.php" );
    
          include_once( "database.php" ); ?>
    
    
    <?php
    
    	if( !$isHideForm ): 
    
    		global $sErr ;
    
    		if( $sErr ) print "<br><a name='error'></a><center><font class='form_error' >$sErr</font></center><br>"; 
    
    
    
    		$starColor = $sErr ? "#ff0000" : "#000000";
    
    		$style=" class='form_text' ";
    
     ?>
    
    
    
    <form name="frmFormMail" action="<?php print PHP_SELF ?>" method='post' enctype='multipart/form-data'>
    
    <input type='hidden' name='formmail_submit' value='Y'>
    
    <input type='hidden' name='esh_formmail_subject' value="Free Query">
    
    
    
    
    
    <table cellspacing='16' cellpadding='0' border='0'  >
    
    	
    		<td class="form_field" valign='top' align='left'>First Name </td><td width='10'  aligh='right' valign='top'> <font size='2' color='#ff0000'>*</font> </td>
    
    		<td class="form_text">
    
    <input type="text" name="fname"  value="<?php  print HtmlSpecialChars( $_POST[ "fname" ] ); ?>" class='text_box'>
    
    		</td>
    
    	</tr>
    
    
    
    	<tr>
    
    		<td class="form_field" valign='top' align='left'>Last Name </td><td width='10'  aligh='right' valign='top'> <font size='2' color='#ff0000'>*</font> </td>
    
    		<td class="form_text">
    
    <input type="text" name="lname"  value="<?php  print HtmlSpecialChars( $HTTP_POST_VARS[ "lname" ] ); ?>" class='text_box'>
    
    		</td>
    
    	</tr>
    
    
    
    	<tr>
    
    		<td class="form_field" valign='top' align='left'>Email ID </td><td width='10'  aligh='right' valign='top'> <font size='2' color='#ff0000'>*</font> </td>
    
    		<td class="form_text">
    
    <input type="email" name="email"  value="<?php  print HtmlSpecialChars( $HTTP_POST_VARS[ "email" ] ); ?>" class='text_box'>
    
    		</td>
    
    	</tr>
    
    
    
    	<tr>
    
    		<td class="form_field" valign='top' align='left'>Date of Birth </td><td width='10'  aligh='right' valign='top'> <font size='2' color='#ff0000'>*</font> </td>
    
    		<td class="form_text">
    
    <?php 
    
    selectList( "dob_DD", $HTTP_POST_VARS["dob_DD"], 1, 31, "DD", $style ) ;
    
    selectList( "dob_MM", $HTTP_POST_VARS["dob_MM"], 1, 12, "MM", $style ) ;
    
    
    
    selectList( "dob_YYYY", $HTTP_POST_VARS["dob_YYYY"], 1900, 2009, "YYYY", $style ) ;
    
    ?>
    
    
    
    		</td>
    
    	</tr>
    
    	
    
    	
    
    	
    
    	
    	<tr><td colspan=3 align='center'><input type='submit' value='Submit'> &nbsp;&nbsp; <input type='button' value='Cancel' onClick="location.href='/';"></td></tr>
    
    </table>
    
    
    
    
    
    </form>
    
    
    <?php 
    								  
    		if( $sErr ) print "<script language='javascript' type='text/javascript'>location.href='#error';</script>";;; 
    
    
    
    else: 
    
    				
    $sql="SELECT count(*) FROM question WHERE DOB='{$HTTP_POST_VARS['dob']}'";  
    
    $result = mysql_fetch_object(mysql_query($sql));
    
    if (is_object($result)) {
    
      $HTTP_POST_VARS['repeat'] = "yes";
      echo $HTTP_POST_VARS['repeat'];
    
    
    
    } else {
    
      $HTTP_POST_VARS['repeat'] = "no";
      echo $HTTP_POST_VARS['repeat'];
    
    }
    
    		
    
    endif;
    
    
    $sql="INSERT INTO `question` (`FirstName`, `LastName`, `Email`, `DOB`,  `NewQuestion`) 
    
    VALUES ( '{$HTTP_POST_VARS['fname']}', 
    
        '{$HTTP_POST_VARS['lname']}',
    
    	    '{$HTTP_POST_VARS['email']}',
    
        '{$HTTP_POST_VARS['dob']}',
    
    
        '{$HTTP_POST_VARS['repeat']}')";  
    
    $result = mysql_query($sql);
    
    
    
    ?>
    Code (markup):

     
    mrwizard3, Jul 11, 2009 IP
  2. Dirty-Rockstar

    Dirty-Rockstar Guest

    Messages:
    252
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I really messed around with your code there. Just trying to help. haha



    
    <?php    include_once( "lib.php" );
    
          include_once( "database.php" ); ?>
    
    
    <?php
    
    	if( !$isHideForm ): 
    
    		global $sErr ;
    
    		if( $sErr ) print "<br><a name='error'></a><center><font class='form_error' >$sErr</font></center><br>"; 
    
    
    
    		$starColor = $sErr ? "#ff0000" : "#000000";
    
    		$style=" class='form_text' ";
    
     ?>
    
    
    
    <form name="frmFormMail" action="<?php print PHP_SELF ?>" method='post' enctype='multipart/form-data'>
    
    <input type='hidden' name='formmail_submit' value='Y'>
    
    <input type='hidden' name='esh_formmail_subject' value="Free Query">
    
    
    
    
    
    <table cellspacing='16' cellpadding='0' border='0'  >
    
    	
    		<td class="form_field" valign='top' align='left'>First Name </td><td width='10'  aligh='right' valign='top'> <font size='2' color='#ff0000'>*</font> </td>
    
    		<td class="form_text">
    
    <input type="text" name="fname"  value="<?php  print HtmlSpecialChars( $_POST[ "fname" ] ); ?>" class='text_box'>
    
    		</td>
    
    	</tr>
    
    
    
    	<tr>
    
    		<td class="form_field" valign='top' align='left'>Last Name </td><td width='10'  aligh='right' valign='top'> <font size='2' color='#ff0000'>*</font> </td>
    
    		<td class="form_text">
    
    <input type="text" name="lname"  value="<?php  print HtmlSpecialChars( $_POST[ "lname" ] ); ?>" class='text_box'>
    
    		</td>
    
    	</tr>
    
    
    
    	<tr>
    
    		<td class="form_field" valign='top' align='left'>Email ID </td><td width='10'  aligh='right' valign='top'> <font size='2' color='#ff0000'>*</font> </td>
    
    		<td class="form_text">
    
    <input type="email" name="email"  value="<?php  print HtmlSpecialChars( $_POST[ "email" ] ); ?>" class='text_box'>
    
    		</td>
    
    	</tr>
    
    
    
    	<tr>
    
    		<td class="form_field" valign='top' align='left'>Date of Birth </td><td width='10'  aligh='right' valign='top'> <font size='2' color='#ff0000'>*</font> </td>
    
    		<td class="form_text">
    
    <?php 
    
    selectList( "dob_DD", $_POST["dob_DD"], 1, 31, "DD", $style ) ;
    
    selectList( "dob_MM", $_POST["dob_MM"], 1, 12, "MM", $style ) ;
    
    
    
    selectList( "dob_YYYY", $_POST["dob_YYYY"], 1900, 2009, "YYYY", $style ) ;
    
    ?>
    
    
    
    		</td>
    
    	</tr>
    
    	
    
    	
    
    	
    
    	
    	<tr><td colspan=3 align='center'><input type='submit' value='Submit'> &nbsp;&nbsp; <input type='button' value='Cancel' onClick="location.href='/';"></td></tr>
    
    </table>
    
    
    
    
    
    </form>
    
    
    <?php 
    								  
    		if( $sErr ) print "<script language='javascript' type='text/javascript'>location.href='#error';</script>";;; 
    
    
    
    else: 
    $dob=mysql_escape_string($_POST['dob']);
    $sql="SELECT count(*) FROM question WHERE DOB='$dob'";  
    $result = mysql_fetch_object(mysql_query($sql));
    $rows=mysql_num_rows($result);
    if($rows>=1) {
    $return="yes"
    } else {
    $return="no";
    }
    
    		
    
    endif;
    $fname=mysql_escape_string($_POST['fname']);
    $lname=mysql_escape_string($_POST['lname']);
    $email=mysql_escape_string($_POST['email']);
    $repeat=mysql_escape_string($_POST['repeat']);
    
    $sql="INSERT INTO `question` (`FirstName`, `LastName`, `Email`, `DOB`,  `NewQuestion`) 
    VALUES ('$fname', '$lname','$email','$dob','$repeat')";  
    $result = mysql_query($sql);
    
    
    print $return;
    
    
    
    
    ?>
    
    PHP:
     
    Dirty-Rockstar, Jul 11, 2009 IP