Mysqli - 用于从数据库收集数据的PHP

时间:2016-10-09 00:43:47

标签: php mysql mysqli

我正在尝试从HTML表单中收集信息,然后将数据传输到我的sql数据库。我用的是Zampp phpmyadmin。

当我点击提交时,输入的数据消失但弹出的成功不会弹出。没有错误。它只是没有被发送到数据库。直接在第三个IF语句之后是无效的ELSE语句 - 将数据传输到数据库 - 这是在第二个代码文件中。我不知道为什么。

此代码是索引页

<!DOCTYPE html>

<?php 
include("functions/functions.php");

?>

<html>
    <head>

        <link rel="stylesheet" href="styles/style.css" media="all"/>
    </head>


    <body>

    <div class = "container">

        <div id="head_wrap">
            <div id="header">
            <!-- logo goes here -->
                <img src="images/logo.png"/ style="float:left"/>
                <form method="post" action="" id="form1">
                    <strong>Email:</strong>
                    <input type="email" name="email" placeholder="Email" required="required"/>
                    <strong>Password:</strong>
                    <input type="password" name="pass" placeholder="*****" required="required"/>
                    <!-- the submit can be a button -->
                        <button name="login">Login</button>
                </form>
            </div>
        </div>
        <!-- this is where the second form and picture comes in on 
            main page -->
        <div id="content">
        <!-- second picture-->
            <div>
            <img src="images/image.png" style="float:left; margin-left:-40px;"/>
            </div>
            <div id="form2">
                <form action="" method="post">
                <h2>Sign Up Here</h2>
                <table>
                    <tr>
                        <td align="right">Name:</td>
                        <td><input type="text" name="u_name" placeholder="Enter your 
                            name" required="required"/>
                        </td>
                    </tr>

                    <tr>
                        <td align="right">Password:</td>
                        <td><input type="password" name="u_pass" placeholder="Enter your 
                            password" required="required"/>
                        </td>
                    </tr>

                    <tr>
                        <td align="right">Email:</td>
                        <td><input type="email" name="u_email" placeholder="Enter your 
                            email" required="required"/>
                        </td>
                    </tr>

                    <tr>
                    <!-- this is where they will pick their state -->
                        <td align="right">Country:</td>
                        <td>
                        <select name="u_country"required="required">
                            <option>Select a country</option>
                            <option>USA</option>
                            <option>Afghanistan</option>
                            <option>India</option>
                        </select>
                        </td>
                    </tr>

                    <tr>
                    <td align="right" required="required">Gender:</td>
                        <td>
                        <select name="u_gender">
                            <option>Select a Gender</option>
                            <option>Female</option>
                            <option>Male</option>
                            <option>Other</option>
                        </select>
                        </td>
                    </tr>

                    <tr>
                        <td align="right" required="required">Birthday:</td>
                        <td><input type="date" name="u_birthday"/>
                        </td>
                    </tr>

                    <tr>

                        <td colspan="6">
                        <button name="sign_up">Sign up</button>
                        </td>
                    </tr>

                </table>
                </form>
                <?php InsertUser();?>
            </div>
    </div>
<!-- this is where the second form and picture ends on 
            main page -->



</body>
</html>

此代码是将信息传输到数据库的functions.php。

<?php

 $con = mysqli_connect("localhost","root","","social_network") 
 or die("Connection was not established");

 function InsertUser(){
global $con;

if(isset($_POST['sign_up'])){
    $name = $_POST['u_name'];
    $pass = $_POST['u_pass'];
    $email = $_POST['u_email'];
    $country = $_POST['u_country'];
    $gender = $_POST['u_gender'];
    $b_day = $_POST['u_birthday'];
    $date = date("d-m-y");
    $status = "unverified";
    $posts = "No";

    $get_email = "select * from users where user_email='$email'";
    $run_email = mysqli_query($con,$get_email);
    $check = mysqli_num_rows($run_email);

    if($check==1){
        echo "<script>alert('Email has already been registered. Please try another one.')</script>";
        exit();     
    }
    if(strlen($pass)<8){
    echo "<script>alert('Password should be minimum 8 characters.')</script>";
        exit(); 
    }
    //this does not work. need to figure out why

    else {
        $insert = "INSERT into users (user_name,user_pass,user_email,user_country,user_gender,user_b_day,user_image,register_date,last_login,status,posts) values ('$name','$pass','$email','$country','$gender','$b_day','default.jpg','$date','$date','$status','$posts')";
        $run_insert = mysqli_query($con,$insert);

            if($run_insert){
                echo "<script>alert('Registration Successful!')</script>";
            }

    }


}



 }

 ?>

1 个答案:

答案 0 :(得分:1)

使用mysqli_error获取有意义的错误消息:

if($run_insert)
{
  echo "<script>alert('Registration Successful!')</script>";
}
else
{
  echo sprintf("Error %s executing %s", mysqli_error($con), $insert);
}