信息不会在数据库中发布

时间:2016-06-07 17:13:38

标签: php html mysqli

我尝试基于网络摄像头图像实现登录表单,显然,我没有代码错误,但信息没有发布在数据库中。

这是我的索引,其中包含用于在数据库中插入信息的php代码:

    <?php
if (isset($_POST['desc'])) {
    if (!isset($_POST['iscorrect']) || $_POST['iscorrect'] == "") {
        echo "Sorry, important data to submit your question is missing. Please press back in your browser and try again and make sure you select a correct answer for the question.";
        exit();
    }
    if (!isset($_POST['type']) || $_POST['type'] == "") {
        echo "Sorry, there was an error parsing the form. Please press back in your browser and try again";
        exit();
    }
    require_once("scripts/connect_db.php");
    $name = $_POST['name'];
    $email = $_POST['email'];
    $name = mysqli_real_escape_string($connection, $name);
    $name = strip_tags($name);
    $email = mysqli_real_escape_string($connection, $email);
    $email = strip_tags($email);
    if (isset($_FILES['image'])) {
        $name = $_FILES['image']['tmp_name'];
        $image = base64_encode(
                file_get_contents(
                        $_FILES['image']['tmp_name']
                )
        );
    }
    $sql = mysqli_query($connection, "INSERT INTO users (name,email,image) VALUES ('$name', '$email','$image')")or die(mysqli_error($connection));
    header('location: index.php?msg=' . $msg . '');
    $msg = 'merge';
}
?>      
<?php
$msg = "";
if (isset($_GET['msg'])) {
    $msg = $_GET['msg'];
}
?>   
    <!doctype html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Licenta Ionut</title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <script type="application/x-javascript"> addEventListener("load", function() { setTimeout(hideURLbar, 0); }, false); function hideURLbar(){ window.scrollTo(0,1); } </script>
        <!-- font files  -->
        <link href='//fonts.googleapis.com/css?family=Muli:400,300' rel='stylesheet' type='text/css'>
        <link href='//fonts.googleapis.com/css?family=Nunito:400,300,700' rel='stylesheet' type='text/css'>
        <!-- /font files  -->
        <!-- css files -->
        <link href="css/style.css" rel='stylesheet' type='text/css' media="all" />
        <link href="web.js" rel='stylesheet' type='text/css' media="all" />
        <script type="text/javascript" src="web.js"></script>
        <!-- /css files -->
    </head>
    <body>
        <p style="color:#06F;"><?php echo $msg; ?></p>
        <h1>LogIn with Webcam Password</h1>
        <div class="log">
            <div class="content1">
                <h2>Sign In Form</h2>
                <form>
                    <input type="text" name="userid" value="USERNAME" onfocus="this.value = '';" onblur="if (this.value == '') {
                                    this.value = 'USERNAME';
                                }">
                    <input type="password" name="psw" value="PASSWORD" onfocus="this.value = '';" onblur="if (this.value == '') {
                                    this.value = 'PASSWORD';}">
                    <div class="button-row">
                        <input type="submit" class="sign-in" value="Sign In">
                        <input type="reset" class="reset" value="Reset">
                        <div class="clear"></div>
                    </div>
                </form>
            </div>
            <div class="content2">
                <h2>Register</h2>
                <form action="index.php", name="index.php" method="post" enctype="multipart/form-data">
                    <input type="text" id="name" name="name" value="Nume">
                    <input type="text" id="email" name="email" value="EmailAdress">
                    <br>

                    <script type="text/javascript" src="webcam.js"></script>
                    <script language="JavaScript">
        document.write(webcam.get_html(320, 240));
                    </script>
                    <div class="button-row">
                        <input class="sign-in"  type=button value="Configure" onClick="webcam.configure()" class="shiva">
                        <input class="reset" type="submit" value="Register" id="image" onClick="take_snapshot()" class="shiva">
                    </div>
                </form>    
            </div>
            <div class="clear"></div>
        </div>
    </body>
</html>

这是连接数据库的脚本:

    <?php 

$db_host = "localhost";
// Place the username for the MySQL database here
$db_username = "Ionut"; 
// Place the password for the MySQL database here
$db_pass = "1993"; 
// Place the name for the MySQL database here
$db_name = "users";

// Run the connection here 
$connection=mysqli_connect("$db_host","$db_username","$db_pass") or die (mysqli_connect_error());
mysqli_select_db($connection,"$db_name") or die ("no database");
?>

我在代码中找不到错误,我需要你的建议/帮助!

感谢您对我的问题感兴趣!

1 个答案:

答案 0 :(得分:1)

要解决此类问题,请将问题分解为多个部分。

(1)首先,接收的PHP文件是什么?在PHP文件的顶部,插入:

<?php
    echo '<pre>';
    print_r($_POST);
    echo '</pre>';
    die('-----------------------------------');

(2)如果没有显示问题,下一步是复制PHP文件,在第二个副本中,硬编码你将在顶部提交的信息(替换正常的PHP数据)提交):

<?php
    $_POST['desc'] = 'TEST - Description';
    $_POST['iscorrect'] = 'what it should be';
    $_POST['type'] = 'TEST - Type';
    etc

然后,运行该修改后的文件并查看数据是否已提交。

(3)如果没有显示问题,请继续使用重复的PHP文件,并在各个地方添加echo语句,以查看文件的中断位置。例如:

$name = $_POST['name'];
$email = $_POST['email'];
$name = mysqli_real_escape_string($connection, $name);
echo 'HERE 01';
$name = strip_tags($name);
$email = mysqli_real_escape_string($connection, $email);
$email = strip_tags($email);
echo 'HERE 02';
if (isset($_FILES['image'])) {
    $name = $_FILES['image']['tmp_name'];
    $image = base64_encode(
            file_get_contents(
                    $_FILES['image']['tmp_name']
            )
    );
}
echo 'HERE 03';
$sql = mysqli_query($connection, "INSERT INTO users (name,email,image) VALUES ('$name', '$email','$image')")or die(mysqli_error($connection));
echo 'HERE 04: $sql = ' .$sql;