www.webdeveloper.com
Results 1 to 4 of 4

Thread: $db->query not working.. is it a sql error or something else?

  1. #1
    Join Date
    Oct 2011
    Location
    Hamilton, Ontario
    Posts
    82

    $db->query not working.. is it a sql error or something else?

    I have tried tweaking this code in every way imaginable (to me at least) with no success.. the query does not insert the data into my db so my form still does not work.. help???

    PHP Code:
    include('inc/db.php');
    $error;
    if(
    $_SERVER['REQUEST_METHOD']=='POST'){
      
    $info['fname'] = stripslashes(htmlspecialchars($_POST['fname']));
      
    $info['lname'] = stripslashes(htmlspecialchars($_POST['lname']));
      
    $info['username'] = stripslashes(htmlspecialchars($_POST['username']));
      
    $info['email'] = stripslashes(htmlspecialchars($_POST['email']));
      
    $info['password1'] = stripslashes(htmlspecialchars(sha1($_POST['password1'])));
      
    $info['password2'] = stripslashes(htmlspecialchars(sha1($_POST['password2'])));
      
    $info['addr_line1'] = stripslashes(htmlspecialchars($_POST['addr_line1']));
      
    $info['addr_line2'] = stripslashes(htmlspecialchars($_POST['addr_line2']));
      
    $info['state_prov'] = stripslashes(htmlspecialchars($_POST['state_prov']));
      
    $info['city'] = stripslashes(htmlspecialchars($_POST['city']));
      
    $info['country'] = stripslashes(htmlspecialchars($_POST['country']));
      if(
    $info['addr_line2']=="ADDRESS LINE 2"){
        
    $info['addr_line2']="";
      }
      if (!isset(
    $error)) {
        foreach( 
    $info as $value ){
          if( 
    stripos($value,'Content-Type:') !== FALSE ){
              
    $error "There was a problem with the information you entered.";
          } else
          if(
    $value!=$info['addr_line2'] && $value==""){
            
    $error="All sections must be completed";
          }
        }
      } else if(!isset(
    $error) &&
        !
    preg_match("/^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/"$info['email'])){
          
    $error="Email is invalid";
          
      } else if(!isset(
    $error) && strlen($info['password1'])<8){
        
    $error="Password must be 8 or more characters.";
        
      } else if(!isset(
    $error) && $info['password1']!=$info['password2']){
        
    $error="Passwords do not match.  Please try again.";
        
      } else if(!isset(
    $error)){
        
    $addr $info['addr_line1']." ".$info['addr_line2']." ".$info['city']." ".$info['state_prov']." ".$info['country'];
        try{
          
    $db->query("INSERT INTO members (fname, lname, username, email, pass, addr) VALUES ('".$info['fname']."','".$info['lname']."','".$info['username']."','".$info['email']."','".$info['password1']."','".$info['addr']."')");
        }catch(
    Exception $e){
          
    $error "Could not execute query";
        }
      }

    I have looked at the db access info and all the creds are correct so I'm clueless as to whats going on here.

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,408
    Are you getting any errors? If not, the the first thing I'd check is if you are actually getting to that code, starting by seeing if you get into the first if() block (error_log() or just echo something out), and work your way down through the if/else logic to see what is actually being executed.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,408
    Ooh, wait, trying changing your if(!isset($error)) checks to if(empty($error)), since it looks like you initialize that variable before the first if() condition.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  4. #4
    Join Date
    Oct 2011
    Location
    Hamilton, Ontario
    Posts
    82
    if found out that the part it's not getting past is the email validation section... I'm horrible with regular expressions so I tried googling regex's for an email and none of the result that looked the most legit worked... i also tried using this function: http://www.linuxjournal.com/article/9585?page=0,3 but it also does not work

    so now the conditional section looks like this:
    PHP Code:
    if($info['addr_line2']=="ADDRESS LINE 2"){
        
    $info['addr_line2']="";
      }
      if (empty(
    $error)) {
        foreach( 
    $info as $value ){
          if( 
    stripos($value,'Content-Type:') !== FALSE ){
              
    $error "There was a problem with the information you entered.";
          } else
          if(
    $value!=$info['addr_line2'] && $value==""){
            
    $error="All sections must be completed";
          }
        }
      } else if(empty(
    $error) && !validEmail($info['email'])){
          
    $error="Email is invalid";
          
      } else if(empty(
    $error) && strlen($info['password1'])<8){
        
    $error="Password must be 8 or more characters.";
        
      } else if(empty(
    $error) && $info['password1']!=$info['password2']){
        
    $error="Passwords do not match.  Please try again.";
        
      } else if(empty(
    $error)){
        
    $addr $info['addr_line1']." ".$info['addr_line2']." ".$info['city']." ".$info['state_prov']." ".$info['country'];
        try{
          
    $db->query("INSERT INTO members (fname, lname, username, email, pass, addr) VALUES ('".$info['fname']."','".$info['lname']."','".$info['username']."','".$info['email']."','".$info['password1']."','".$info['addr']."')");
        }catch(
    Exception $e){
          
    $error "Could not execute query";
        }
      } 
    Last edited by thewebiphyer; 01-14-2014 at 02:11 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles