表单值成功存储到数据库,但IF语句不会执行

时间:2016-10-03 06:05:13

标签: php html mysql if-statement refresh

我的注册表格有问题。是的,它成功地将信息存储到我的数据库,但它只是重新加载页面(注册页面)之后,并且不会执行if语句中的代码。这是我的完整代码(来自表单和php / mysql代码):

        <form class="formoid-metro-black" id="close" style="background-color:transparent;font-size:14px;font-family:'Open Sans','Helvetica Neue','Helvetica',Arial,Verdana,sans-serif;color:#FFFFFF;max-width:480px;min-width:150px" method="post" enctype="multipart/form-data">

          <div class="title"><h2>Basic Information</h2></div>

          <div class="element-name"><label class="title"><span class="required">*</span></label><span class="nameFirst"><input  type="text" size="8" name="fname" required="required"/><label class="subtitle">First Name</label></span><span class="nameLast"><input  type="text" size="14" name="lname" required="required"/><label class="subtitle">Last Name</label></span></div>
          <div class="element-select"><label class="title">Gender<span class="required">*</span></label><div class="large"><span><select name="gender" required="required">

            <option value="Male" style="color:#000000">Male</option>
            <option value="Female" style="color:#000000">Female</option></select><i></i></span></div></div>
            <div class="element-date" style="color:#000000; background-color:transparent;"><label class="title" style="color:#fff;">Birthdate</label><input class="large" data-format="yyyy-mm-dd" type="date" name="bdate" placeholder="yyyy-mm-dd" style="color:#fff;"  /></div>
            <div class="element-separator"><hr><h3 class="section-break-title"></h3></div>
            <div class="element-address"><label class="title">Address</label><span class="addr1"><input  type="text" name="street" /><label class="subtitle">Barangay / Street Address</label></span><span class="city"><input  type="text" name="city" /><label class="subtitle">City</label></span><span class="state"><input  type="text" name="province" /><label class="subtitle">Province / State</label></span><span class="zip"><input  type="text" maxlength="15" name="zcode" /><label class="subtitle">Zip Code</label></span><div class="country"><select name="country" ><!--<option selected="selected" value="" disabled="disabled">--- Select a country ---</option> --><option selected="selected" value="Philippines">Philippines</option><option style="color:#000000" value="United States">United States</option><option style="color:#000000" value="United Kingdom">United Kingdom</option><option style="color:#000000" value="Australia">Australia</option><option style="color:#000000" value="Canada">Canada</option><option style="color:#000000" value="France">France</option><option style="color:#000000" value="New Zealand">New Zealand</option><option style="color:#000000" value="India">India</option><option style="color:#000000" value="Brazil">Brazil</option><option style="color:#000000" value="----" disabled="disabled">----</option><option style="color:#000000" value="Afghanistan">Afghanistan</option><option style="color:#000000" value="Aland Islands">Aland Islands</option><option style="color:#000000" value="Albania">Albania</option><option style="color:#000000" value="Algeria">Algeria</option><option style="color:#000000" value="American Samoa">American Samoa</option><option style="color:#000000" value="Andorra">Andorra</option><option style="color:#000000" value="Angola">Angola</option><option style="color:#000000" value="Anguilla">Anguilla</option><option style="color:#000000" value="Antarctica">Antarctica</option><option style="color:#000000" value="Antigua and Barbuda">Antigua and Barbuda</option><option style="color:#000000" value="Argentina">Argentina</option><option style="color:#000000" value="Armenia">Armenia</option><option style="color:#000000" value="Aruba">Aruba</option><option style="color:#000000" value="Austria">Austria</option><option style="color:#000000" value="Azerbaijan">Azerbaijan</option><option style="color:#000000" value="Bahamas">Bahamas</option><option style="color:#000000" value="Bahrain">Bahrain</option><option style="color:#000000" value="Bangladesh">Bangladesh</option><option style="color:#000000" value="Barbados">Barbados</option><option style="color:#000000" value="Belarus">Belarus</option><option style="color:#000000" value="Belgium">Belgium</option><option style="color:#000000" value="Belize">Belize</option><option style="color:#000000" value="Benin">Benin</option><option style="color:#000000" value="Bermuda">Bermuda</option><option style="color:#000000" value="Bhutan">Bhutan</option><option style="color:#000000" value="Bolivia">Bolivia</option><option style="color:#000000" value="Bosnia and Herzegovina">Bosnia and Herzegovina</option><option style="color:#000000" value="Botswana">Botswana</option><option style="color:#000000" value="British Indian Ocean Territory">British Indian Ocean Territory</option><option style="color:#000000" value="Brunei Darussalam">Brunei Darussalam</option><option style="color:#000000" value="Bulgaria">Bulgaria</option><option style="color:#000000" value="Burkina Faso">Burkina Faso</option><option style="color:#000000" value="Burundi">Burundi</option><option style="color:#000000" value="Cambodia">Cambodia</option><option style="color:#000000" value="Cameroon">Cameroon</option><option style="color:#000000" value="Cape Verde">Cape Verde</option><option style="color:#000000" value="Cayman Islands">Cayman Islands</option><option style="color:#000000" value="Central African Republic">Central African Republic</option><option style="color:#000000" value="Chad">Chad</option><option style="color:#000000" value="Chile">Chile</option><option style="color:#000000" value="China">China</option><option style="color:#000000" value="Colombia">Colombia</option><option style="color:#000000" value="Comoros">Comoros</option><option style="color:#000000" value="Democratic Republic of the Congo">Democratic Republic of the Congo</option><option style="color:#000000" value="Republic of the Congo">Republic of the Congo</option><option style="color:#000000" value="Cook Islands">Cook Islands</option><option style="color:#000000" value="Costa Rica">Costa Rica</option><option style="color:#000000" value="Cote d'Ivoire">Cote d'Ivoire</option><option style="color:#000000" value="Croatia">Croatia</option><option style="color:#000000" value="Cuba">Cuba</option><option style="color:#000000" value="Cyprus">Cyprus</option><option style="color:#000000" value="Czech Republic">Czech Republic</option><option style="color:#000000" value="Denmark">Denmark</option><option style="color:#000000" value="Djibouti">Djibouti</option><option style="color:#000000" value="Dominica">Dominica</option><option style="color:#000000" value="Dominican Republic">Dominican Republic</option><option style="color:#000000" value="East Timor">East Timor</option><option style="color:#000000" value="Ecuador">Ecuador</option><option style="color:#000000" value="Egypt">Egypt</option><option style="color:#000000" value="El Salvador">El Salvador</option><option style="color:#000000" value="Equatorial Guinea">Equatorial Guinea</option><option style="color:#000000" value="Eritrea">Eritrea</option><option style="color:#000000" value="Estonia">Estonia</option><option style="color:#000000" value="Ethiopia">Ethiopia</option><option style="color:#000000" value="Faroe Islands">Faroe Islands</option><option style="color:#000000" value="Fiji">Fiji</option><option style="color:#000000" value="Finland">Finland</option><option style="color:#000000" value="Gabon">Gabon</option><option style="color:#000000" value="Gambia">Gambia</option><option style="color:#000000" value="Georgia">Georgia</option><option style="color:#000000" value="Germany">Germany</option><option style="color:#000000" value="Ghana">Ghana</option><option style="color:#000000" value="Gibraltar">Gibraltar</option><option style="color:#000000" value="Greece">Greece</option><option style="color:#000000" value="Grenada">Grenada</option><option style="color:#000000" value="Guatemala">Guatemala</option><option style="color:#000000" value="Guinea">Guinea</option><option style="color:#000000" value="Guinea-Bissau">Guinea-Bissau</option><option style="color:#000000" value="Guyana">Guyana</option><option style="color:#000000" value="Haiti">Haiti</option><option style="color:#000000" value="Honduras">Honduras</option><option style="color:#000000" value="Hong Kong">Hong Kong</option><option style="color:#000000" value="Hungary">Hungary</option><option style="color:#000000" value="Iceland">Iceland</option><option style="color:#000000" value="Indonesia">Indonesia</option><option style="color:#000000" value="Iran">Iran</option><option style="color:#000000" value="Iraq">Iraq</option><option style="color:#000000" value="Ireland">Ireland</option><option style="color:#000000" value="Israel">Israel</option><option style="color:#000000" value="Italy">Italy</option><option style="color:#000000" value="Jamaica">Jamaica</option><option style="color:#000000" value="Japan">Japan</option><option style="color:#000000" value="Jordan">Jordan</option><option style="color:#000000" value="Kazakhstan">Kazakhstan</option><option style="color:#000000" value="Kenya">Kenya</option><option style="color:#000000" value="Kiribati">Kiribati</option><option style="color:#000000" value="North Korea">North Korea</option><option style="color:#000000" value="South Korea">South Korea</option><option style="color:#000000" value="Kuwait">Kuwait</option><option style="color:#000000" value="Kyrgyzstan">Kyrgyzstan</option><option style="color:#000000" value="Laos">Laos</option><option style="color:#000000" value="Latvia">Latvia</option><option style="color:#000000" value="Lebanon">Lebanon</option><option style="color:#000000" value="Lesotho">Lesotho</option><option style="color:#000000" value="Liberia">Liberia</option><option style="color:#000000" value="Libya">Libya</option><option style="color:#000000" value="Liechtenstein">Liechtenstein</option><option style="color:#000000" value="Lithuania">Lithuania</option><option style="color:#000000" value="Luxembourg">Luxembourg</option><option style="color:#000000" value="Macedonia">Macedonia</option><option style="color:#000000" value="Madagascar">Madagascar</option><option style="color:#000000" value="Malawi">Malawi</option><option style="color:#000000" value="Malaysia">Malaysia</option><option style="color:#000000" value="Maldives">Maldives</option><option style="color:#000000" value="Mali">Mali</option><option style="color:#000000" value="Malta">Malta</option><option style="color:#000000" value="Marshall Islands">Marshall Islands</option><option style="color:#000000" value="Mauritania">Mauritania</option><option style="color:#000000" value="Mauritius">Mauritius</option><option style="color:#000000" value="Mexico">Mexico</option><option style="color:#000000" value="Micronesia">Micronesia</option><option style="color:#000000" value="Moldova">Moldova</option><option style="color:#000000" value="Monaco">Monaco</option><option style="color:#000000" value="Mongolia">Mongolia</option><option style="color:#000000" value="Montenegro">Montenegro</option><option style="color:#000000" value="Morocco">Morocco</option><option style="color:#000000" value="Mozambique">Mozambique</option><option style="color:#000000" value="Myanmar">Myanmar</option><option style="color:#000000" value="Namibia">Namibia</option><option style="color:#000000" value="Nauru">Nauru</option><option style="color:#000000" value="Nepal">Nepal</option><option style="color:#000000" value="Netherlands">Netherlands</option><option style="color:#000000" value="Netherlands Antilles">Netherlands Antilles</option><option style="color:#000000" value="Nicaragua">Nicaragua</option><option style="color:#000000" value="Niger">Niger</option><option style="color:#000000" value="Nigeria">Nigeria</option><option style="color:#000000" value="Norway">Norway</option><option style="color:#000000" value="Oman">Oman</option><option style="color:#000000" value="Pakistan">Pakistan</option><option style="color:#000000" value="Palau">Palau</option><option style="color:#000000" value="Palestine">Palestine</option><option style="color:#000000" value="Panama">Panama</option><option style="color:#000000" value="Papua New Guinea">Papua New Guinea</option><option style="color:#000000" value="Paraguay">Paraguay</option><option style="color:#000000" value="Peru">Peru</option><option style="color:#000000" value="Poland">Poland</option><option style="color:#000000" value="Portugal">Portugal</option><option style="color:#000000" value="Puerto Rico">Puerto Rico</option><option style="color:#000000" value="Qatar">Qatar</option><option style="color:#000000" value="Romania">Romania</option><option style="color:#000000" value="Russia">Russia</option><option style="color:#000000" value="Rwanda">Rwanda</option><option style="color:#000000" value="Saint Kitts and Nevis">Saint Kitts and Nevis</option><option style="color:#000000" value="Saint Lucia">Saint Lucia</option><option style="color:#000000" value="Saint Vincent and the Grenadines">Saint Vincent and the Grenadines</option><option style="color:#000000" value="Samoa">Samoa</option><option style="color:#000000" value="San Marino">San Marino</option><option style="color:#000000" value="Sao Tome and Principe">Sao Tome and Principe</option><option style="color:#000000" value="Saudi Arabia">Saudi Arabia</option><option style="color:#000000" value="Senegal">Senegal</option><option style="color:#000000" value="Serbia">Serbia</option><option style="color:#000000" value="Seychelles">Seychelles</option><option style="color:#000000" value="Sierra Leone">Sierra Leone</option><option style="color:#000000" value="Singapore">Singapore</option><option style="color:#000000" value="Slovakia">Slovakia</option><option style="color:#000000" value="Slovenia">Slovenia</option><option style="color:#000000" value="Solomon Islands">Solomon Islands</option><option style="color:#000000" value="Somalia">Somalia</option><option style="color:#000000" value="South Africa">South Africa</option><option style="color:#000000" value="Spain">Spain</option><option style="color:#000000" value="Sri Lanka">Sri Lanka</option><option style="color:#000000" value="Sudan">Sudan</option><option style="color:#000000" value="Suriname">Suriname</option><option style="color:#000000" value="Swaziland">Swaziland</option><option style="color:#000000" value="Sweden">Sweden</option><option style="color:#000000" value="Switzerland">Switzerland</option><option style="color:#000000" value="Syria">Syria</option><option style="color:#000000" value="Taiwan">Taiwan</option><option style="color:#000000" value="Tajikistan">Tajikistan</option><option style="color:#000000" value="Tanzania">Tanzania</option><option style="color:#000000" value="Thailand">Thailand</option><option style="color:#000000" value="Togo">Togo</option><option style="color:#000000" value="Tonga">Tonga</option><option style="color:#000000" value="Trinidad and Tobago">Trinidad and Tobago</option><option style="color:#000000" value="Tunisia">Tunisia</option><option style="color:#000000" value="Turkey">Turkey</option><option style="color:#000000" value="Turkmenistan">Turkmenistan</option><option style="color:#000000" value="Tuvalu">Tuvalu</option><option style="color:#000000" value="Uganda">Uganda</option><option style="color:#000000" value="Ukraine">Ukraine</option><option style="color:#000000" value="United Arab Emirates">United Arab Emirates</option><option style="color:#000000" value="United States Minor Outlying Islands">United States Minor Outlying Islands</option><option style="color:#000000" value="Uruguay">Uruguay</option><option style="color:#000000" value="Uzbekistan">Uzbekistan</option><option style="color:#000000" value="Vanuatu">Vanuatu</option><option style="color:#000000" value="Vatican City">Vatican City</option><option style="color:#000000" value="Venezuela">Venezuela</option><option style="color:#000000" value="Vietnam">Vietnam</option><option style="color:#000000" value="Virgin Islands, British">Virgin Islands, British</option><option style="color:#000000" value="Virgin Islands, U.S.">Virgin Islands, U.S.</option><option style="color:#000000" value="Yemen">Yemen</option><option style="color:#000000" value="Zambia">Zambia</option><option style="color:#000000" value="Zimbabwe">Zimbabwe</option></select><i></i><label class="subtitle">Country</label></div></div>
            <div class="element-separator"><hr><h3 class="section-break-title"></h3></div>
            <div class="title"><h2>Log-in Information</h2></div>
            <div class="element-email"><label class="title">E-mail<span class="required">*</span></label><input class="large" type="email" name="email" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,3}$" value="" required="required"/></div>
            <div class="element-input"><label class="title">Username<span class="required">*</span></label><input class="large" type="text" name="username" required="required"/></div>
            <div class="element-password"><label class="title">Password<span class="required">*</span></label><input class="large" type="password" name="password" id="password" pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9]).{8,}" value="" required="required"/><label class="subtitle">Password must be at least 8 characters composed of at least 1 uppercase, 1 number and  letters. </label></div>
            <div class="element-password"><label class="title">Confirm Password<span class="required">*</span></label><input class="large" type="password" name="password1" id="confirm_password" pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9]).{8,}" value="" required="required"/></div>
            <div class="element-separator"><hr><h3 class="section-break-title"></h3></div>
            <div class="element-phone"><label class="title">Phone<span class="required">*</span></label><input class="large" type="tel" pattern="[+]?[#0-9]{3,}" maxlength="14" name="phone" required="required" value=""/></div>
            <div class="element-separator"><hr><h3 class="section-break-title"></h3></div>
            <div class="title"><h2>Profile Information</h2></div>
            <div class="element-file"><label class="title">Profile Picture</label><label class="large" ><div class="button">Choose Photo</div><input type="file" name="profilephoto" class="file_input" value="" /><div class="file_text">...</div></label></div>
            <div class="element-input"><label class="title">School</label><input class="large" type="text" name="school" value=""/><label class="subtitle">Name of last school graduated.</label></div>
            <div class="element-input"><label class="title">Work</label><input class="large" type="text" name="work" value=""/></div>
            <div class="submit"><input type="submit" name="submit" value="Sign Up"/></div></form><script type="text/javascript" src="forms/sign-up-form_files/formoid1/formoid-metro-black.js"></script>

            <?php include("includes/db-connect.php");


            if(isset($_POST['submit'])){
              $fname = $_POST['fname'];
              $lname = $_POST['lname'];                  
              $gender = $_POST['gender'];
              $bdate = $_POST['bdate'];
              $street = $_POST['street'];
              $city = $_POST['city'];
              $province = $_POST['province'];
              $zcode = $_POST['zcode'];
              $country = $_POST['country'];  

              $email = $_POST['email'];
              $username = $_POST['username'];
              $password = $_POST['password'];
              $password1 = $_POST['password1'];
              $phone = $_POST['phone'];

              $school = $_POST['school'];
              $work = $_POST['work'];

              $profilephoto = $_FILES['profilephoto']['name'];
              $photo_tmp = $_FILES['profilephoto']['tmp_name'];



              move_uploaded_file($photo_tmp,"includes/user/profile/$profilephoto");

              $userquery = "insert into user (`user-firstname`, `user-lastname`, `user-address`, `user-address-city`, `user-address-province`, `user-address-zipcode`, `user-email`, `user-username`, `user-password`, `user-phone`, `user-gender`, `user-address-country`, `user-birthdate`, `user-profile-photo`, `user-school`,`user-work`) values ('$fname', '$lname', '$street', '$city', '$province', '$zcode', '$email', '$username', '$password', '$phone', '$gender', '$country', '$bdate', '$profilephoto', '$school', '$work')";
              $dt = \DateTime::createFromFormat('m/d/Y', $_POST['bdate']);


              if (mysqli_query($conn, $userquery)) {
                ?><div id='openModal' class='modalDialog'><div><a href='log-in.php' title='Close' class='close'>X</a> <h3>Thank you! You have successfully registered.</h3></div></div>      

                <?php
              } else {
                ?><div id='openModal' class='modalDialog'><div><a href='../sign-up.php' title='Close' class='close'>X</a><h3>Error. Please try again.</h3><?php $userquery . mysqli_error($conn)?></div></div><?php ;
              }



              mysqli_close($conn);
            }

            ?>

如果if语句中包含代码,我希望代码在插入值后执行:

if (mysqli_query($conn, $userquery)) {

3 个答案:

答案 0 :(得分:0)

说明使用以下IF条件。成功插入时,它将返回TRUE,失败时返回FALSE

   if (mysqli_query($conn, $userquery) == TRUE) {

      //YOUR Logic

   } else {

      //YOUR Logic

   }

答案 1 :(得分:0)

您的代码有很多改进点,正如许多评论所指出的那样。最重要的是,您必须阅读SQL注入,XSS攻击以及所有其他常见安全漏洞。 OWASP is an excellent source有关此事的信息。

其次,我还建议您阅读PHP: The Right Way网站。它对于帮助您提高代码质量非常宝贵。

然后,我对您的代码的建议:

<?php

include ("includes/db-connect.php");

// If a form has been submitted, attempt to create a new user.
if (isset ($_POST['submit'])) {
    $output = create_user ($db);
}

?>

<form class="formoid-metro-black" id="close" method="post" enctype="multipart/form-data">
    <fieldset>
        <h2 class="title">Basic Information</h2>

        <div class="element-name">
            <span class="required">*</span>
            <input id="nameFirst" type="text" size="8" name="fname" required="required">
            <label for="nameFirst" class="subtitle">First Name</label>

            <input id="nameLast" type="text" size="14" name="lname" required="required">
            <label for="nameLast" class="subtitle">Last Name</label>
        </div>

        <div class="element-select">
            <label for="gender" class="title">Gender<span class="required">*</span></label>
            <select id="gender" name="gender" required="required" class="large">
                <option value="Male">Male</option>
                <option value="Female">Female</option>
            </select>
        </div>

        <div class="element-date">
            <label for="birth" class="title">Birthdate</label>
            <input id="birth" class="large" data-format="yyyy-mm-dd" type="date" name="bdate" placeholder="yyyy-mm-dd">
        </div>
    </fieldset>

    <fieldset class="element-address">
        <legend>Address</legend>

        <input id="street" type="text" name="street">
        <label for="street" class="subtitle">Barangay / Street Address</label>

        <input id="city" type="text" name="city">
        <label for="city" class="subtitle">City</label>

        <input id="state" type="text" name="province">
        <label for="state" class="subtitle">Province / State</label>

        <input id="zip" type="text" maxlength="15" name="zcode">
        <label for="zip" class="subtitle">Zip Code</label>

        <select id="country" name="country">
            <!--<option selected="selected" value="0" disabled="disabled">--- Select a country ---</option> -->
            <option selected="selected" value="1">Philippines</option>
            <option value="2">United States</option>
            <option value="3">United Kingdom</option>
            .....
            <option value="71">Zimbabwe</option>
        </select>
        <label for="country" class="subtitle">Country</label>
    </fieldset>

    <fieldset>
        <h2>Log-in Information</h2>
        ......
    </fieldset>

    <fieldset class="submit">
        <input type="submit" name="submit" value="Sign Up">
    </fieldset>
</form>

<script type="text/javascript" src="forms/sign-up-form_files/formoid1/formoid-metro-black.js"></script>

<?php

// Now we can simply and safely echo the output.
echo $output;

?>

<?php
/**
 * Validates the input and creates a new user.
 * In case of errors it returns an error string.
 *
 * @param PDO $db The database connection to use.
 * @return string
 */
function create_user (PDO $db) {
    // We're going to need these ready to be used.
    $errors = $data = array ();

    // First off: Check that the data you receive is within acceptable parameters of
    // what you expect. Such as a name containing letters, dash, dot, and possibly numbers.
    // White-list only! You cannot know all of the bad stuff.
    $data[':fname'] = $_POST['fname'];
    $data[':lfname'] = $_POST['lname'];

    // Example: Test gender for M/F as those are the only valid values.
    if (empty ($_POST['gender']) || ($_POST['gender'] != 'm' && $_POST['gender'] == 'f')) {
        // The value is either missing, or _not_ one of the accepted values.
        // Add error message to the user here.
        $errors[] = 'Gender';
    } else {
        $data[':gender'] = $_POST['gender'];
    }

    // Since birthdate, and other, fields can be empty. Check for that as well.
    if (!empty ($_POST['bdate'])) {
        $dt = \DateTime::createFromFormat ('m/d/Y', $_POST['bdate']);
        if (!$dt) {
            // Not a valid input format for the birthdate. Add error message here.
            $errors[] = 'Birthdate';
        }

        // Just used an (so far) undefined constant here for simplicity.
        $data[':birth'] = $dt->format (MYSQL_FORMAT);
    }

    // Complete these.
    $street = $_POST['street'];
    $city = $_POST['city'];
    $province = $_POST['province'];
    $zcode = $_POST['zcode'];
    $country = $_POST['country'];

    $email = $_POST['email'];
    $username = $_POST['username'];
    $password = $_POST['password'];
    $password1 = $_POST['password1'];
    $phone = $_POST['phone'];
    $school = $_POST['school'];
    $work = $_POST['work'];

    // Here you need to verify that the file uploaded is indeed a photo!
    // Not to mention that the upload was indeed succesful, and that other parameters
    // such as filename, length and so forth is acceptable.
    $profilephoto = $_FILES['profilephoto']['name'];
    $photo_tmp = $_FILES['profilephoto']['tmp_name'];

    if (!move_uploaded_file ($photo_tmp, "includes/user/profile/$profilephoto")) {
        // Failed moving (uploading) the file. Add error here.
        $errors[] = 'Profile picture';
    }

    // If we have any errors added to the array, abort and give he user notice about what's wrong.
    if (!empty ($errors)) {
        return 'Following fields failed validation: '.implode (',', $errors);
    }

    // Use prepared statements to avoid SQL-injections.
    // Also, moved the city name, province and country to their own tables.
    // This to avoid duplication of data and data integrity problems (typos).
    $userquery = <<<EOQ
INSERT INTO user (`user-firstname`, `user-lastname`, `user-address`, `user-address-zipcode`,
`user-email`, `user-username`, `user-password`, `user-phone`, `user-gender`, `user-birthdate`,
`user-profile-photo`, `user-school`,`user-work`)
VALUES (:fname, :lname, :street, :zip, :email, :user, :pass, :phone, :gender, :birth, :photo, :school, :work)";
EOQ;
    $stmt = $db->prepare($userquery);
    $res = $stmt->execute($data);

    // We need the user ID for the session, and to check if a user was actually added.
    $userID = $db->lastInsertId();

    // If successful, return dialog string.
    if ($res && $userID) {
        // I'd much rather redirect the user to the modal dialoge here, as it
        // would prevent problems with re-submitting data on refresh.

        return <<<EOF
<div id='openModal' class='modalDialog'>
    <div>
        <a href='log-in.php' title='Close' class='close'>X</a>
        <h3>Thank you! You have successfully registered.</h3>
    </div>
</div>
EOF;
    }

    // Not successful, return error string.
    return <<<EOF
<div id='openModal' class='modalDialog'>
    <div>
        <a href='../sign-up.php' title='Close' class='close'>X</a>
        <h3>Error. Please try again.</h3><?php $userquery . mysqli_error($conn)?></div>
</div>
EOF;
}

您会注意到我已经清理了很多HTML代码。看到你已经在使用类,你不需要也不希望内联CSS混乱。对于你和他人来说,它只会让事情变得更加困难 其次,我删除了大量不必要的标记。以及向输入元素添加ID(而不是类),以便您可以正确使用标签 第三部分是我添加了fieldsets而不是divshr来创建表单的各个部分。正如您所看到的,这为您提供了更少的代码,并且您可以通过几行CSS实现完全相同的效果。

我还将DB类更改为PDO,以便我可以在语句中使用命名占位符。 MySQLi遗憾地缺乏一些东西。

建议仔细阅读评论并彻底阅读我所做的更改,以及尝试理解/研究我为什么这样做。如果您有任何疑问,请不要害怕。 :)

答案 2 :(得分:-1)

您可以将用户重定向到登录页面。 对于那种情况:

 if (mysqli_query($conn, $userquery)) {

          echo "<script type="javascript">window.location.href='log-in.php'</script>";
          } else {
            ?><div id='openModal' class='modalDialog'><div><a href='../sign-up.php' title='Close' class='close'>X</a><h3>Error. Please try again.</h3><?php $userquery . mysqli_error($conn)?></div></div><?php ;
          }

我没有在你的代码中找到与模式相关的代码。