1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Data type mismatch error

Discussion in 'PHP' started by davanderbilt, Mar 19, 2007.

  1. #1
    I have a php form that links up to an Access database where all of the fields are required and one of the fields asks for a date for females only. I can't figure out how to not require a value for this date field (first_day) if the person entering the form is male. I also don't want to have to put in a real date for a value if the person filling out the form is male, because then that means we have inaccurate data in the database. Here is some of the code I'm working with.

    I've never tried to do this before so I thought I'd write a gender function and reference it further on below.

    
    <script type="text/javascript" language="JavaScript">
    
    function gender()
    {
    if (document.dlw.Gender.value == '2') {
    	document.dlw.first_day.value="00/00/00";
    	document.dlw.first_day.disabled=true;
    } 
    else {
    	document.dlw.first_day.disabled=false;
    }
    }
    
    </script>
    
    PHP:
    I need to be able to make first_day a fillable field only for females, so I created this and have it referencing the gender function above

    
    <p align="justify"><strong><span class="style24">Are You Female?</span>
       <select name="Gender" id="Gender" onChange="gender()">
    	<option value="1">Yes</option>
    	<option value="2">No</option>
       </select>
    </strong></p>
    <p align="justify"><strong>Females only (Date of first day of menstrual cycle): </strong> (Format:01/01/07)
       <input name="first_day" type="text" id="first_day" ;"  size="10" maxlength="10">
    </p>
    
    HTML:
    Here is the insert statement to put first_day into the Access database. I stripped out all the other variables that I have in this include statement for space sake. Gender doesn't need to go into the database. I just need it to make the first_day field fillable for females only and the first_day field needs to go into the database.

    
    <?php
    echo '<br><table><tr><td><b>';
    foreach ($HTTP_POST_VARS as $key => $value) {
    $temp = stripslashes($value);
    $HTTP_POST_VARS[$key] = $temp;
    if($temp==" ")
     {
       $error_rep=1;
       echo $key,' has not been filled <br/>';
     } 
    }
    echo '</td></tr></table></b>';
    
    $Gender=isset($HTTP_POST_VARS['Gender'])?$HTTP_POST_VARS['Gender']:'NA';
    $first_day=isset($HTTP_POST_VARS['first_day']) ? $HTTP_POST_VARS['first_day']:'00/00/0000';
    
    $conn = odbc_connect ('LTE_Master', '', '');
     $id= odbc_exec($conn,$get_stat);
    
    $insert_statement = "
       insert into 
    	[AccessTable]
    	values	 ('$first_day')";
    	
    if($error_rep==0)
    {
    $result = odbc_exec($conn,$insert_statement);
    if(!$result)
    {
      //echo 'There has been error while submitting your survey. Contact...';
    ?>
    </p>
    <h1 align="center" class="style1">&nbsp; </h1>
    
    <p align="center" class="style1"><font color="#FF0000">There has been an ERROR submitting your Data.</font> </p>
    
    <?php
    }
    elseif ($result)
    {
    ?>
    
    PHP:
    The first_day value needs to go into an Access database, where the field type is Date/Time. When I submit, I get the following error:

    
    Warning: SQL error: [Microsoft][ODBC Microsoft Access Driver] Data type mismatch in criteria expression., SQL state 22005 in SQLExecDirect in c:\program files\apache group\apache\htdocs\lte\dlw_add.php on line 157
     
    There has been an ERROR submitting your Data. 
    
    Code (markup):
    If I remove the part of the form where it asks if you are female and comment out the gender function, it still gives a data type mismatch if I manually type in 00/00/00 as the first_day date. Then if I manually type in 01/01/01 as the first_day date, the data goes into the database just fine (all of the other field values go in just fine too). Then I uncommented out the function and the female qualifier portion of the form and tried submitting a test where I said Yes for Female and filled in a date and it gave me the same SQL error. So my conclusion is that it must have something to do with the gender function, but I can't see what could be causing it.

    I have another form with a similar qualifying Yes/No statement that pushes through a text value and it is working. Is the date/time type causing this problem?

    I have a lot of other fields on this page and all the others require values, and I am stuck trying to figure out how to not require a value for first_day if the person entering the form is male. I also don't want to have to put in a real date for a value if the person filling out the form is male. Is there a simpler way to do this?
     
    davanderbilt, Mar 19, 2007 IP