mrwizard Posted July 11, 2009 Share Posted July 11, 2009 HI, I have a contact form which adds entrys to database and emails admin. I want to add a query so that if same date of birth already exists in database it should change repeat variable to no. this is how i have done I have used echo command to know the results and all the time it says yes <table cellspacing='6' cellpadding='0' border='0' align='center' ><tr><td> <?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'> <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); ?> Link to comment https://forums.phpfreaks.com/topic/165638-mysql-query-not-working/ Share on other sites More sharing options...
Aeglos Posted July 12, 2009 Share Posted July 12, 2009 I don't see the POST variable "dob" set anywhere, you have three distinct variables dob_DD, dob_MM and dob_YYYY (Through PHP generated forms I assume...) but no plain 'dob' anywhere in the form. So I assume your "DOB" column is empty for all users in the database and you are searching for an empty date on an empty column wich evaluates to true always. Also, the HTTP_VARS_* arrays are horribly deprecated, you should use $_POST, $_GET and similar arrays instead. You should also try not to skip braces in the if conditional blocks, it makes the code harder to understand and leads to errors. And finally, using globals (i.e. global $sErr) is a very bad practice unless you really know what you are doing. Cheers. Link to comment https://forums.phpfreaks.com/topic/165638-mysql-query-not-working/#findComment-873820 Share on other sites More sharing options...
mrwizard Posted July 12, 2009 Author Share Posted July 12, 2009 Actually i made it thru freeformmaker.com. On top the code is <?php include_once( "lib.php" ); include_once( "database.php" ); ?> I am not sure if I can switch from http_var because everything works perfectly. it emails fine, adds to database fine. lib.php has this code <?php error_reporting(E_PARSE); define("ADMIN_MAIL", ""); define("HOST_NAME", $_SERVER['HTTP_HOST']); define("PHP_SELF", $_SERVER['PHP_SELF']); define("ERR_MISSING", "Missing required field : "); define("ERR_EMAIL", "Please enter a valid e-mail address : "); define('FORM_RECIPIENT', ''); define('FORM_RECIPIENT1', ''); $form_mail[] = array( "name" => "fname", "text" => "First Name", "type" => "text", "required" => "Required" ) ; $form_mail[] = array( "name" => "lname", "text" => "Last Name", "type" => "text", "required" => "Required" ) ; $form_mail[] = array( "name" => "email", "text" => "Email ID", "type" => "email", "required" => "Required" ) ; $form_mail[] = array( "name" => "dob", "text" => "Date of Birth", "type" => "date(dd-mm-yyyy)", "required" => "Required" ) ; $HTTP_POST_VARS[ "dob" ] = ( $HTTP_POST_VARS[ "dob_DD" ] && $HTTP_POST_VARS[ "dob_MM" ] && $HTTP_POST_VARS[ "dob_YYYY" ] && $HTTP_POST_VARS[ "tob_HH" ] && $HTTP_POST_VARS[ "tob_MM" ] ) ? $HTTP_POST_VARS[ "dob_DD" ] . "/" . $HTTP_POST_VARS[ "dob_MM" ] . "/" . $HTTP_POST_VARS[ "dob_YYYY" ] . " " . $HTTP_POST_VARS[ "tob_HH" ] . ":" . $HTTP_POST_VARS[ "tob_MM" ] : "" ; $isHideForm = false; if( $HTTP_POST_VARS["formmail_submit"] ){ $sErr = checkPass(); if( ! $sErr ){ sendFormMail( $form_mail, "") ; $isHideForm = true; $redirect = ""; if( strlen(trim($redirect)) ): header( "Location:$redirect" ); exit; endif; } } ?> <? function sendFormMail( $form_mail, $sFileName = "" ) { global $HTTP_POST_VARS ; if (ereg('//', HOST_NAME)) { return; } $to = FORM_RECIPIENT; $from = $HTTP_POST_VARS["email"]; $subject = $HTTP_POST_VARS["esh_formmail_subject"]; $sWhatToDo = $sFileName ? "mailandfile" : "" ; $cc = FORM_RECIPIENT1; $bcc = $HTTP_POST_VARS["esh_formmail_bcc"]; $charset = $HTTP_POST_VARS["esh_formmail_charset"]; for( $i = 0; $i < count( $form_mail ); $i ++ ){ $value = trim( $HTTP_POST_VARS[ $form_mail[ $i ][ "name" ] ] ); $content .= $form_mail[ $i ][ "text" ] . " \t : " . $value ."\n"; $line .= remove_newline( $value ) . "\t" ; if( strtolower("Sender's email") == strtolower($form_mail[ $i ][ "type" ]) ) { $from = $value ; } }; $content .= "\n\nIP:" . getEnv( "REMOTE_ADDR" ); switch( strtolower($sWhatToDo) ){ case "mailandfile" : mailAttachments( $to , $subject , $content, $from, $charset, $cc , $bcc ) ; if( ! appendToFile( $sFileName, $line ) ) mailReport( $content . "\n\nWrite Form Mail to File Fail." ); break; case "fileonly" : if( ! appendToFile( $sFileName, $line ) ) mailReport( $content . "\n\nWrite Form Mail to File Fail.", $from ); break; default : mailAttachments( $to , $subject , $content, $from, $charset, $cc , $bcc ) ; } mailAutoResponse( $from ) ; } function mailAutoResponse( $to ){ global $HTTP_POST_VARS ; $subject = $HTTP_POST_VARS["esh_formmail_return_subject"]; $responseMsg = $HTTP_POST_VARS["esh_formmail_return_msg"]; if( $to && $responseMsg ) mail( $to, $subject, $responseMsg, "From: " . FORM_RECIPIENT); } function mailReport( $content = "", $from = "" ){ mail( ADMIN_MAIL, "Error@" . HOST_NAME . PHP_SELF, $content, "From:$from" ); } function remove_newline( $str = "" ){ $newliner = "<!--esh_newline-->" ; $newtaber = "<!--esh_newtaber-->" ; $str = ereg_replace( "\t", $newtaber, $str ); $str = ereg_replace( "\r\n", $newliner, $str ); return ereg_replace( "\n", $newliner, $str ); } function checkPass() { global $form_mail ; global $HTTP_POST_VARS ; global $HTTP_POST_FILES ; for( $i = 0; $i < count( $form_mail ); $i ++ ){ $type = strtolower( $form_mail[ $i ][ "type" ] ); $value = trim( $HTTP_POST_VARS[ $form_mail[ $i ][ "name" ] ] ); $required = $form_mail[ $i ][ "required" ] ; $text = stripslashes( $form_mail[ $i ][ "text" ] ); if( !strlen($value) && ( $required == "Required" ) && $type != "attachment" ) return ERR_MISSING . $text ; switch( $type ){ case strtolower("Sender's Name") : break; case strtolower("Generic email"): case "email": if( ! formIsEMail($value) ) return ERR_EMAIL . $text ; break; case "text" : break; case "textarea" : break; case "checkbox" : case "radio" : break; case "select" : break; case "attachment" : $upload_file = $HTTP_POST_FILES[ $form_mail[ $i ]["name"] ][ "tmp_name" ] ; if( ! is_uploaded_file($upload_file) ) return ERR_SELECT_UPLOAD . $text; break; case strtolower("Date(MM-DD-YYYY)"): break; case strtolower("Date(MM-YYYY)"): break; case strtolower("Time(HH:MM:SS)"): break; case strtolower("Time(HH:MM)"): break; default : } } return "" ; } //------------------------------------------------------------------------------------------ function formSelected( $var, $val ) { echo ( $var == $val ) ? "selected" : ""; } //------------------------------------------------------------------------------------------ function formChecked( $var, $val ) { echo ( $var == $val ) ? "checked" : ""; } //------------------------------------------------------------------------------------------ function formIsEMail( $email ){ return ereg( "^(.+)@(.+)\\.(.+)$", $email ); } //------------------------------------------------------------------------------------------ function selectList( $name, $selectedValue, $start, $end, $prompt = "-Select-", $style = "" ) { $tab = "\t" ; print "<select name=\"$name\" $style>\n" ; print $tab . "<option value=''>$prompt</option>\n" ; $nLen = strlen( "$end" ) ; $prefix_zero = str_repeat( "0", $nLen ); for( $i = $start; $i <= $end ; $i ++ ){ $stri = substr( $prefix_zero . $i, strlen($prefix_zero . $i)-$nLen, $nLen ); $selected = ( $stri == $selectedValue ) ? " selected " : "" ; print $tab . "<option value=\"$stri\" $selected >$stri</option>\n" ; } print "</select>\n\n" ; } function mailAttachments( $to = "" , $subject = "" , $message = "" , $from = "[email protected]" , $charset = "iso-8859-1", $cc = "" , $bcc = "" ){ global $HTTP_POST_FILES ; if( ! strlen( trim( $to ) ) ) return "Missing \"To\" Field." ; $boundary = "====_My_PHP_Form_Generator_" . md5( uniqid( srand( time() ) ) ) . "===="; $headers = "From: $from\r\n"; if ($cc) $headers .= "CC: $cc\r\n"; if ($bcc) $headers .= "BCC: $bcc\r\n"; $plainHeaders = $headers ; $headers .= "MIME-Version: 1.0\nContent-type: multipart/mixed;\n\tboundary=\"$boundary\"\n"; $txtMsg = "\nThis is a multi-part message in MIME format.\n" . "\n--$boundary\n" . "Content-Type: text/plain;\n\tcharset=\"$charset\"\n\n" . $message . "\n"; $sError = "" ; $nFound = 0; foreach( $HTTP_POST_FILES as $aFile ){ $sFileName = $aFile[ "tmp_name" ] ; $sFileRealName = $aFile[ "name" ] ; if( is_file( $sFileName ) ): if( $fp = fopen( $sFileName, "rb" ) ) : $sContent = fread( $fp, filesize( $sFileName ) ); $sFName = basename( $sFileRealName ) ; $sMIME = getMIMEType( $sFName ) ; $bPlainText = ( $sMIME == "text/plain" ) ; if( $bPlainText ) : $encoding = "" ; else: $encoding = "Content-Transfer-Encoding: base64\n"; $sContent = chunk_split( base64_encode( $sContent ) ); endif; $sEncodeBody .= "\n--$boundary\n" . "Content-Type: $sMIME;\n" . "\tname=\"$sFName\"\n" . $encoding . "Content-Disposition: attachment;\n" . "\tfilename=\"$sFName\"\n\n" . $sContent . "\n" ; $nFound ++; else: $sError .= "<br>File $sFileName can not open.\n" ; endif; else: $sError .= "<br>File $sFileName doesn't exist.\n" ; endif; }; $sEncodeBody .= "\n\n--$boundary--" ; $sSource = $txtMsg . $sEncodeBody ; $nFound ? mail( $to, $subject, $sSource, $headers ) : mail( $to, $subject, $message, $plainHeaders ); return $sError ; } function getMIMEType( $sFileName = "" ) { $sFileName = strtolower( trim( $sFileName ) ); if( ! strlen( $sFileName ) ) return ""; $aMimeType = array( "txt" => "text/plain" , "pdf" => "application/pdf" , "zip" => "application/x-compressed" , "html" => "text/html" , "htm" => "text/html" , "avi" => "video/avi" , "mpg" => "video/mpeg " , "wav" => "audio/wav" , "jpg" => "image/jpeg " , "gif" => "image/gif" , "tif" => "image/tiff " , "png" => "image/x-png" , "bmp" => "image/bmp" ); $aFile = split( "\.", basename( $sFileName ) ) ; $nDiminson = count( $aFile ) ; $sExt = $aFile[ $nDiminson - 1 ] ; return ( $nDiminson > 1 ) ? $aMimeType[ $sExt ] : ""; } //------------------------------------------------------------------------------------------ function appendToFile( $sFileName = "", $line = "" ){ if( !$sFileName || !$line ) return 0; $hFile = fopen( "$sFileName", "a+w" ); $nBytes = 0; if( $hFile ){ $nBytes = fputs( $hFile , trim($line)."\r\n" ); fclose( $hFile ); }; return $nBytes ; } ?> Link to comment https://forums.phpfreaks.com/topic/165638-mysql-query-not-working/#findComment-873866 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.