我的问题是我想以php注册表格注册用户,但问题是它无法添加到数据库中

时间:2019-11-16 08:04:16

标签: php mysql

  

Registration.php

<?php
include('db.php')
?>

<div id="wrapper">
    <nav class="navbar-default navbar-side" role="navigation">
        <div class="sidebar-collapse">
            <ul class="nav" id="main-menu">

                <li>
                    <a  href="../index.php"><i class="fa fa-home"></i> 
Homepage</a>
                </li>

                </ul>

        </div>

    </nav>

<div id="page-wrapper" >
        <div id="page-inner">
         <div class="row">
                <div class="col-md-12">
                    <h1 class="page-header">
                        REGISTRATION <small></small>
                    </h1>
                </div>
            </div> 


        <div class="row">


                <div class="panel panel-primary">
                    <div class="panel-heading">
                         ACCOUNT INFORMATION
  

注册表中的内容

</div>
<div class="panel-body">
    <form name="form" method="post">
        <div class="form-group">
            <label>Username</label>
            <input name="username" class="form- 
   control" required>

        </div>
        <div class="form-group">
            <label>Password</label>
            <input name="pw" class="form-control" required>

        </div>
        <div class="form-group">
            <label>Confirm Password</label>
            <input name="confirmpw" class="form- 
   control" required>

        </div>
        <div class="form-group">
            <label>Subscription Date</label>
            <input name="subdate" type="date" class="form-control" required>

        </div>
        <label>Title*</label>
        <select name="title" class="form- 
    control" required>
            <option value selected></option>
            <option value="Dr.">Dr.</option>
            <option value="Miss.">Miss.
            </option>
            <option value="Mr.">Mr.</option>
            <option value="Mrs.">Mrs.
            </option>
            <option value="Prof.">Prof.
            </option>
            <option value="Rev .">Rev .
            </option>
            <option value="Rev . Fr">Rev . Fr .
            </option>
        </select>
</div>
<div class="form-group">
    <label>First Name</label>
    <input name="fname" class="form- 
    control" required>

</div>
<div class="form-group">
    <label>Last Name</label>
    <input name="lname" class="form- 
   control" required>

</div>
<div class="form-group">
    <label>Email</label>
    <input name="email" type="email" class="form-control" required>

</div>
<div class="form-group">
    <label>Date Of Birth</label>
    <input name="dob" type="date" class="form-control" required>

</div>
<?php

                                $placeofbirth = array("Johor", "Kedah", 
    "Kelantan", "Kuala Lumpur", "Labuan", "Melaka", "Negeri Sembilan", "Pahang", 
    "Perak", "Perlis", "Pulau Pinang", "Putrajaya", "Sabah", "Sarawak", 
    "Selangor", "Terengganu");

                                ?>
    <div class="form-group">
        <label>Place of Birth*</label>
        <select name="pob" class="form- 
    control" required>
            <option value selected></option>
            <?php
                                                foreach($placeofbirth as $key => 
    $value):
                                                echo '<option 
    value="'.$value.'">'.$value.'</option>'; //close your tags!!
                                                endforeach;
                                                ?>
        </select>
    </div>

    <div class="form-group">
        <label>IC/Passport No.</label>
        <input name="ic_passport" type="text" class="form-control" required>

    </div>
    <div class="form-group">
        <label>Home Address</label>
        <input name="address" type="text" class="form-control" required>

    </div>
    <div class="form-group">
        <label>Postcode</label>
        <input name="postcode" type="text" class="form-control" required>

    </div>
    <div class="form-group">
        <label>City</label>
        <input name="city" type="text" class="form-control" required>

    </div>
    <?php

                                $state = array("Johor", "Kedah", "Kelantan", 
     "Kuala Lumpur", "Labuan", "Melaka", "Negeri Sembilan", "Pahang", "Perak", 
     "Perlis", "Pulau Pinang", "Putrajaya", "Sabah", "Sarawak", "Selangor", 
     "Terengganu");

                                ?>
        <div class="form-group">
            <label>State*</label>
            <select name="state" class="form- 
     control" required>
                <option value selected></option>
                <?php
                                                foreach($state as $key => 
     $value):
                                                echo '<option 
     value="'.$value.'">'.$value.'</option>'; //close your tags!!
                                                endforeach;
                                                ?>
            </select>
        </div>
        <?php

                                $countries = array("Afghanistan", "Albania", 
     "Algeria", "American Samoa", "Andorra", "Angola", "Anguilla", "Antarctica", 
     "Antigua and Barbuda", "Argentina", "Armenia", "Aruba", "Australia", 
     "Austria", "Azerbaijan", "Bahamas", "Bahrain", "Bangladesh", "Barbados", 
     "Belarus", "Belgium", "Belize", "Benin", "Bermuda", "Bhutan", "Bolivia", 
     "Bosnia and Herzegowina", "Botswana", "Bouvet Island", "Brazil", "British 
      Indian Ocean Territory", "Brunei Darussalam", "Bulgaria", "Burkina Faso", 
     "Burundi", "Cambodia", "Cameroon", "Canada", "Cape Verde", "Cayman Islands", 
     "Central African Republic", "Chad", "Chile", "China", "Christmas Island", 
     "Cocos(Keeling) Islands", "Colombia", "Comoros", "Congo", "Congo, the 
     Democratic Republic of the", "Cook Islands", "Costa Rica", "Cote d'Ivoire", 
     "Croatia(Hrvatska)", "Cuba", "Cyprus", "Czech Republic", "Denmark", 
     "Djibouti", "Dominica", "Dominican Republic", "East Timor", "Ecuador", 
     "Egypt", "El Salvador", "Equatorial Guinea", "Eritrea", "Estonia", 
     "Ethiopia", "Falkland Islands (Malvinas)", "Faroe Islands", "Fiji", 
     "Finland", "France", "France Metropolitan", "French Guiana", "French 
     Polynesia", "French Southern Territories", "Gabon", "Gambia", "Georgia", 
    "Germany", "Ghana", "Gibraltar", "Greece", "Greenland", "Grenada", 
    "Guadeloupe", "Guam", "Guatemala", "Guinea", "Guinea-Bissau", "Guyana", 
    "Haiti", "Heard and Mc Donald Islands", "Holy See (Vatican City State)", 
    "Honduras", "Hong Kong", "Hungary", "Iceland", "India", "Indonesia", "Iran 
    (Islamic Republic of)", "Iraq", "Ireland", "Israel", "Italy", "Jamaica", 
    "Japan", "Jordan", "Kazakhstan", "Kenya", "Kiribati", "Korea, Democratic 
    People's Republic of", "Korea, Republic of", "Kuwait", "Kyrgyzstan", "Lao, 
    People's Democratic Republic", "Latvia", "Lebanon", "Lesotho", "Liberia", 
    "Libyan Arab Jamahiriya", "Liechtenstein", "Lithuania", "Luxembourg", 
    "Macau", "Macedonia, The Former Yugoslav Republic of", "Madagascar", 
    "Malawi", "Malaysia", "Maldives", "Mali", "Malta", "Marshall Islands", 
    "Martinique", "Mauritania", "Mauritius", "Mayotte", "Mexico", "Micronesia, 
    Federated States of", "Moldova, Republic of", "Monaco", "Mongolia", 
    "Montserrat", "Morocco", "Mozambique", "Myanmar", "Namibia", "Nauru", 
    "Nepal", "Netherlands", "Netherlands Antilles", "New Caledonia", "New 
    Zealand", "Nicaragua", "Niger", "Nigeria", "Niue", "Norfolk Island", 
    "Northern Mariana Islands", "Norway", "Oman", "Pakistan", "Palau", "Panama", 
    "Papua New Guinea", "Paraguay", "Peru", "Philippines", "Pitcairn", "Poland", 
    "Portugal", "Puerto Rico", "Qatar", "Reunion", "Romania", "Russian 
    Federation", "Rwanda", "Saint Kitts and Nevis", "Saint Lucia", "Saint Vincent 
    and the Grenadines", "Samoa", "San Marino", "Sao Tome and Principe", "Saudi 
    Arabia", "Senegal", "Seychelles", "Sierra Leone", "Singapore", "Slovakia 
    (Slovak Republic)", "Slovenia", "Solomon Islands", "Somalia", "South Africa", 
    "South Georgia and the South Sandwich Islands", "Spain", "Sri Lanka", "St. 
    Helena", "St. Pierre and Miquelon", "Sudan", "Suriname", "Svalbard and Jan 
    Mayen Islands", "Swaziland", "Sweden", "Switzerland", "Syrian Arab Republic", 
    "Taiwan, Province of China", "Tajikistan", "Tanzania, United Republic of", 
    "Thailand", "Togo", "Tokelau", "Tonga", "Trinidad and Tobago", "Tunisia", 
    "Turkey", "Turkmenistan", "Turks and Caicos Islands", "Tuvalu", "Uganda", 
    "Ukraine", "United Arab Emirates", "United Kingdom", "United States", "United 
    States Minor Outlying Islands", "Uruguay", "Uzbekistan", "Vanuatu", 
    "Venezuela", "Vietnam", "Virgin Islands (British)", "Virgin Islands (U.S.)", 
    "Wallis and Futuna Islands", "Western Sahara", "Yemen", "Yugoslavia", 
    "Zambia", "Zimbabwe");

                                ?>
            <div class="form-group">
                <label>Country*</label>
                <select name="country" class="form 
    control" required>
                    <option value selected></option>
                    <?php
                                                foreach($countries as $key => 
    $value):
                                                echo '<option 
    value="'.$value.'">'.$value.'</option>'; //close your tags!!
                                                endforeach;
                                                ?>
                </select>
            </div>

            <div class="form-group">
                <label>Phone Number</label>
                <input name="phone" type="text" class="form-control" required>

            </div>

            </div>

            </div>
            </div>

            <div class="row">

                <div class="col-md-12 col-sm-12">
                    <div class="well">
                        <h4>HUMAN VERIFICATION</h4>
                        <p>Type Below this code
                            <?php $Random_code=rand(); 
     echo$Random_code; ?>
                        </p>
                        <br />
                        <p>Enter the random code
                            <br />
                        </p>
                        <input type="text" name="code1" title="random code" />
                        <input type="hidden" name="code" value="<?php echo 
     $Random_code; ?>" />
                        <input type="submit" name="submit" class="btn btn- 
     primary">
                        <?php
                            if(isset($_POST['submit']))
                            {
                            $code1=$_POST['code1'];
                            $code=$_POST['code']; 
                            if($code1!="$code")
                            {
                            $msg="Invalide code"; 
                            }
                            else
                            {

这是连接数据库的代码。我认为问题出在这部分,但我找不到原因。我的数据库已经符合此代码中的详细信息

 $con=mysqli_connect("localhost","root","","hotel");
                                $check="SELECT * FROM listofmembers WHERE 
 email = '$_POST[email]'";
                                $rs = mysqli_query($con,$check);
                                $data = mysqli_fetch_array($rs, MYSQLI_NUM);
                                if($data[0] > 1) {
                                    echo "<script type='text/javascript'> 
 alert('User Already in Exists')</script>";

                                }

                                else
                                {

                                    $new ="Not Conform";
                                    $newUser="INSERT INTO 
 `listofmembers`(`Username`,`Pw`,`Confirmpw`, `Subdate`,`Title`, `Fname`, 
`Lname`, `Email`, `Dob`, `Pob`, `Ic_passport`, `Address`,`Postcode`, `City`, 
 `State`, `Country`, `Phone`) VALUES `('$_POST[username]','$_POST[pw]','$_POST[confirmpw]]','$_POST[subdate]''$_POST[title]','$_POST[fname]','$_POST[lname]','$_POST[email]','$_POST[dob]','$_POST[pob]','$_POST[ic_passport]','$_POST[address]','$_POST[postcode]','$_POST[city]','$_POST[state]','$_POST[country]','$_POST[phone]')";`

                                    if (mysqli_query($con,$newUser))
                                    {
                                        echo "<script type='text/javascript'> 
alert('Your Booking application has been sent')</script>";

                                    }
                                    else
                                    {
                                        echo "<script type='text/javascript'> 
alert('Error adding user in database')</script>";

                                    }
                                }

                        $msg="Your code is correct";
                        }
                        }
                        ?>
                    </form>

                </div>
            </div>
        </div>


            </div>
  

javascript

                </div>
         <!-- /. PAGE INNER  -->
        </div>
     <!-- /. PAGE WRAPPER  -->
    </div>
 <!-- /. WRAPPER  -->
<!-- JS Scripts-->
<!-- jQuery Js -->
<script src="assets/js/jquery-1.10.2.js"></script>
  <!-- Bootstrap Js -->
<script src="assets/js/bootstrap.min.js"></script>
<!-- Metis Menu Js -->
<script src="assets/js/jquery.metisMenu.js"></script>
  <!-- Custom Js -->
<script src="assets/js/custom-scripts.js"></script>


</body>
</html>

请帮帮我。每次按提交按钮时,都会弹出“在数据库中添加用户时出错”的提示

1 个答案:

答案 0 :(得分:-1)

首先使用准备好的语句。如果您不了解它们,请阅读它们。

现在您可以执行以下操作:
POST变量以数组形式存储,因此应该以访问数组的方式对其进行访问。

您的$_POST['variable']不太正确 POST变量应类似于$_POST['username'],而不应类似于$_POST[username] 通过mysqli_real_escape_string函数传递post变量后最好保存它们:

例如:$username = mysqli_real_escape_string($_POST['username']);

然后在查询中使用此$username