Advertisement

03.13.2008 at 06:51PM PDT, ID: 23240647
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

6.8

Problem with Insert Query

Asked by dpawar in PHP and Databases, PHP Scripting Language, MySQL Server

Tags:

Hi! The insert query on line #39 has some problem,  Please let me know how can I fix it. The error is "Can't execute insert query."
<?php
/* Program: Login.php
 * Desc:    Login program for the Members Only section of
 *          the pet store. It provides two options:
 *          (1) login using an existing Login Name and
 *          (2) enter a new login name. Login Names and
 *          passwords are stored in a MySQL database.
 */
 session_start();                                      # 9
 include("dogs.inc");                                  #10
 switch (@$_POST['do'])                                #11
 {
   case "login":                                       #13
     $cxn = mysqli_connect($host,$user,$password,$database)
            or die ("Couldn't connect to server.");    #15

     $sql = "SELECT  loginName FROM Member
             WHERE loginName='$_POST[fusername]'";     #18
     $result = mysqli_query($cxn,$sql)
               or die("Couldn't execute query.");      #20
     $num = mysqli_num_rows($result);                  #21
     if ($num > 0)  // login name was found            #22
     {
        $sql = "SELECT loginName FROM Member
                WHERE loginName='$_POST[fusername]'
                AND password=md5('$_POST[fpassword]')";
        $result2 = mysqli_query($cxn,$sql)
                   or die("Couldn't execute query 2.");
        $num2 = mysqli_num_rows($result2);
        if ($num2 > 0)  // password is correct         #30
        {
           $_SESSION['auth']="yes";                    #32
           $logname=$_POST['fusername'];
           $_SESSION['logname'] = $logname;            #34
           $today = date("Y-m-d h:i:s");               #35
           $sql = "INSERT INTO Login (loginName,loginTime)
                   VALUES ('$logname','$today')";
           $result = mysqli_query($cxn,$sql)
                     or die("Can't execute insert query.");
           header("Location: Member_page.php");        #40
        }
        else    // password is not correct             #42
        {
           $message="The Login Name, '$_POST[fusername]'
                     exists, but you have not entered the
                     correct password! Please try again.<br>";
           include("login_form.inc");                  #47
        }
     }                                                 #49
     elseif ($num == 0)  // login name not found       #50
     {  
        $message = "The Login Name you entered does not
                    exist! Please try again.<br>";
        include("login_form.inc");
     }
   break;                                              #56

   case "new":
        /* Check for blanks */                           #59
     foreach($_POST as $field => $value)               #60
     {
        if ($field != "fax")                           #62
        {
           if ($value == "")                           #64
           {
              $blanks[] = $field;
           }
        }
     }
     if(isset($blanks))                                #70
     {
        $message_new = "The following fields are blank.  
                  Please enter the required information:  ";
        foreach($blanks as $value)
        {
           $message_new .= "$value, ";
        }
        extract($_POST);
        include("login_form.inc");
        exit();
     }

    /* Validate data */
     foreach($_POST as $field => $value)               #84
     {
        if(!empty($value))                             #86
        {
           if(eregi("name",$field) and
              !eregi("login",$field))
           {
              if (!ereg("^[A-Za-z' -]{1,50}$",$value))
              {
                 $errors[]="$value is not a valid name.";
              }
           }
           if(eregi("street",$field) or
             eregi("addr",$field) or eregi("city",$field))
           {
              if(!ereg("^[A-Za-z0-9.,' -]{1,50}$",$value))
              {
                 $errors[] = "$value is not a valid
                               address or city.";
              }
           }
           if(eregi("state",$field))
           {
              if(!ereg("[A-Za-z]{2}",$value))
              {
                $errors[]="$value is not a valid state.";
              }
           }
           if(eregi("email",$field))
           {
              if(!ereg("^.+@.+\\..+$",$value))
              {
                 $errors[] = "$value is not a valid
                              email address.";
              }
           }
           if(eregi("zip",$field))
           {
              if(!ereg("^[0-9]{5,5}(\-[0-9]{4,4})?$",
                  $value))
              {
                $errors[]="$value is not a valid
                            zipcode.";
              }
           }
           if(eregi("phone",$field)
              or eregi("fax",$field))
           {
              if(!ereg("^[0-9)(xX -]{7,20}$",$value))
              {
                 $errors[] = "$value is not a valid  
                              phone number. ";
              }
           }
        } // end if empty                             #138
     } // end foreach
     if(@is_array($errors))                           #140
     {
        $message_new = "";
        foreach($errors as $value)
        {
          $message_new .= $value." Please try
                                   again<br />";
        }
        extract($_POST);
        include("login_form.inc");
        exit();
     }

      /* clean data */
     $cxn = mysqli_connect($host,$user,$password,$database);

     foreach($_POST as $field => $value)              #156
     {
        if($field != "Button" and $field != "do")
        {
           if($field == "password")
           {
              $password = strip_tags(trim($value));
           }
           else
           {
              $fields[]=$field;
              $value = strip_tags(trim($value));
              $values[] =
                   mysqli_real_escape_string($cxn,$value);
              $$field = $value;                
           }
        }
     }

      /* check whether user name already exists */
     $sql = "SELECT loginName FROM Member
                    WHERE loginName = '$loginName'";  #177
     $result = mysqli_query($cxn,$sql)
               or die("Couldn't execute select query.");
     $num = mysqli_num_rows($result);                 #180
     if ($num > 0)                                    #181
     {
        $message_new = "$loginName already used.
                         Select another User Name.";
        include("login_form.inc");
        exit();
     }
    /* Add new member to database */
     else                                             #190
     {  
        $today = date("Y-m-d");
        $fields_str = implode(",",$fields);
        $values_str = implode('","',$values);
        $fields_str .=",createDate";
        $values_str .='"'.",".'"'.$today;
        $fields_str .=",password";
        $values_str .= '"'.","."md5"."('".$password."')";
        $sql = "INSERT INTO Member ";
        $sql .= "(".$fields_str.")";
        $sql .= " VALUES ";
        $sql .= "(".'"'.$values_str.")";
        $result = mysqli_query($cxn,$sql)
                or die("Couldn't execute insert query.");
        $_SESSION['auth']="yes";                      #205
        $_SESSION['logname'] = $loginName;            #206

        /* send email to new member */                #208
        $emess = "A new Member Account has been setup. ";
        $emess.= "Your new Member ID and password are: ";
        $emess.= "\n\n\t$loginName\n\t$password\n\n";
        $emess.="We appreciate your interest in Pet";
        $emess.= " Store at PetStore.com. \n\n";
        $emess.= "If you have any questions or problems,";
        $emess.= " email webmaster@petstore.com";
        $ehead="From: member-desk@petstore.com\r\n";  #216
        $subj = "Your new Member Account from Pet Store";
        $mailsnd=mail("$email","$subj","$emess","$ehead");
        header("Location: New_member.php");           #219
      }
    break;                                            #221

    default:                                          #223
        include("login_form.inc");
  }
?>
Start Free Trial
[+][-]03.13.2008 at 06:54PM PDT, ID: 21122485

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]03.13.2008 at 07:03PM PDT, ID: 21122524

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03.13.2008 at 07:17PM PDT, ID: 21122579

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]03.13.2008 at 07:17PM PDT, ID: 21122582

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]03.13.2008 at 07:42PM PDT, ID: 21122677

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03.13.2008 at 07:45PM PDT, ID: 21122687

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]03.13.2008 at 07:46PM PDT, ID: 21122693

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]03.13.2008 at 07:58PM PDT, ID: 21122729

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03.13.2008 at 08:00PM PDT, ID: 21122735

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03.13.2008 at 08:02PM PDT, ID: 21122748

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]03.13.2008 at 08:05PM PDT, ID: 21122754

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: PHP and Databases, PHP Scripting Language, MySQL Server
Tags: PHP & MySQL
Sign Up Now!
Solution Provided By: ritetek
Participating Experts: 4
Solution Grade: B
 
 
[+][-]03.14.2008 at 07:02AM PDT, ID: 21125555

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628