PHP发布表单 - 在错误的数据库列中发布值

时间:2015-12-09 05:35:01

标签: php html5

我有一个简单的表格,我从新员工那里收集信息。

如果我将mobilenumber输入字段添加到sql INSERT INTO中,它会将mobilenumber放入Last Name ,覆盖Last Name值。我无法绕过我出错的地方?

如果我保留First Name, Middle Name, Last Name, Email,Activation,则效果很好......添加mobilenumber并且它无效。

这是一个缩短的脚本......

    <script>
    function validate(evt) {
      var theEvent = evt || window.event;
      var key = theEvent.keyCode || theEvent.which;
      key = String.fromCharCode( key );
      var regex = /[0-9]|\./;
      if( !regex.test(key) ) {
        theEvent.returnValue = false;
        if(theEvent.preventDefault) theEvent.preventDefault();
      }
    }
    </script>
    <?php 
    include ('database_connection.php');
    echo '<div class="body">';
    if (isset($_POST['formsubmitted'])) {
        $error = array(); //Declare An Array to store any error message


      //  if (empty($_POST['FirstName'])) { //if no name has been supplied
      //      $error[] = 'Please Enter a name '; //add to array "error"
      //  } else {
      //      $FirstName = $_POST['FirstName']; //else assign it a variable
      //  }

    //<!-- -->



        if (empty($_POST['FirstName'])) { //if no name has been supplied
            $error[] = 'Please Enter a name '; //add to array "error"
        } else {
            $FirstName = $_POST['FirstName']; //else assign it a variable
        }

        if (empty($_POST['MiddleName'])) {
            $error[] = 'Please Enter Your Middle Name ';
        } else {
            $MiddleName = $_POST['MiddleName'];
        }

        if (empty($_POST['LastName'])) {
            $error[] = 'Please Enter Your LastName ';
        } else {
            $LastName = $_POST['LastName'];
        }

        if (empty($_POST['mobilenumber'])) {
            $error[] = 'Please Enter Your LastName ';
        } else {
            $LastName = $_POST['mobilenumber'];
        }   

        if (empty($_POST['e-mail'])) {
            $error[] = 'Please Enter your Email ';
        } else {

            if (preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/",
                $_POST['e-mail'])) {
                //regular expression for email validation
                $Email = $_POST['e-mail'];
            } else {
                $error[] = 'Your Email address does not appear to be a valid email address. Please check the spelling.  ';
            }

        }

        if (empty($error)) //send to Database if there's no error '

        { // If everything's OK...

            // Make sure the email address is available:
            $query_verify_email = "SELECT * FROM newhire  WHERE Email ='$Email'";
            $result_verify_email = mysqli_query($dbc, $query_verify_email);
            if (!$result_verify_email) { //if the Query Failed ,similar to if($result_verify_email==false)
                echo ' Database Error Occured ';
            }
            if (mysqli_num_rows($result_verify_email) == 0) { // IF no previous user is using this email .

                // Create a unique  activation code:
                $Activation = md5(uniqid(rand(), true));

                $query_insert_user =
                    "INSERT INTO `newhire` ( `FirstName`, `MiddleName`, `LastName`, `mobilenumber`, `Email`, `Activation`) VALUES ( '$FirstName', '$MiddleName', '$LastName', '$mobilenumber', '$Email', '$Activation')";

                $result_insert_user = mysqli_query($dbc, $query_insert_user);
                if (!$result_insert_user) {
                    echo 'Query Failed ';
                }

                if (mysqli_affected_rows($dbc) == 1) {
} else { // If it did not run OK.
                    echo '<div class="errormsgbox">You could not be registered due to a system
    error. We apologize for any
    inconvenience.</div>';
                }

            } else { // The email address is not available.
                echo '<div class="errormsgbox" >That email
    address has already been registered.
    </div>';
            }

        } else { //If the "error" array contains error msg , display them

            echo '<div class="errormsgbox"> <ol>';
            foreach ($error as $key => $values) {

                echo '  <li>' . $values . '</li>';

            }
            echo '</ol></div>';

        }

        mysqli_close($dbc); //Close the DB Connection

    } // End of the main Submit conditional.
    ?>
    <form action="index.php" method="post" class="registration_form">
    <center>
      <fieldset style='width:400;'>
        <legend>New Contractor Application</legend>

        <p>Please fill out all fields below. If you have questions please speak with your hiring manager.</p>

        <div class="elements">
          <input type="text" placeholder="First Name" id="FirstName" name="FirstName" size="25" />
        </div>

        <div class="elements">
          <input type="text" placeholder="Middle Name" id="MiddleName" name="MiddleName" size="25" />
        </div>

        <div class="elements">
          <input type="text" placeholder="Last Name" id="LastName" name="LastName" size="25" />
        </div>

        <div class="elements">
          <input type="text" placeholder="Phone #" id="mobilenumber" name="mobilenumber" size="25" />
        </div>    

        <div class="elements">
          <input type="email" placeholder="Email Address" id="e-mail" name="e-mail" size="25" />
        </div>

        <div class="submit"><input type="hidden" name="formsubmitted" value="TRUE" /><input type="submit" value="Submit" /></div>
      </fieldset>
    </center>  
    </form>
    </div>

这是我的DB Scheme

   CREATE TABLE IF NOT EXISTS `newhire` (
      `id` int(10) NOT NULL AUTO_INCREMENT,
      `FirstName` varchar(20) NOT NULL,
      `MiddleName` varchar(20) NOT NULL,
      `LastName` varchar(10) NOT NULL,
      `mobilenumber` varchar(10) NOT NULL,
      `Email` varchar(50) NOT NULL,
      `Activation` varchar(40) DEFAULT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=43 ;

2 个答案:

答案 0 :(得分:2)

你有一个拼写错误 - &gt;将$lastname更改为$mobilenumber

if (empty($_POST['mobilenumber'])) {
        $error[] = 'Please Enter Your LastName ';
    } else {
        $mobilenumber = $_POST['mobilenumber'];
    }  

答案 1 :(得分:2)

我认为问题在于:

   if (empty($_POST['mobilenumber'])) {
        $error[] = 'Please Enter Your LastName ';
    } else {
        $LastName = $_POST['mobilenumber'];
    } 

应该是:

   if (empty($_POST['mobilenumber'])) {
        $error[] = 'Please Enter Your Mobile Number ';
    } else {
        $mobilenumber = $_POST['mobilenumber'];
    }