数据未插入数据库

时间:2017-05-07 04:17:50

标签: php mysql

我尝试将html表单值插入数据库但它没有执行。我无法确切地知道问题可能是什么。我已经运行了mysql错误测试来检查可能导致它的原因,但它没有显示任何内容。

这是插入语法:

<?php

    require_once('../includes/config.php'); 

    if (isset($_POST['add_user'])) {

        $file=$_FILES['image']['tmp_name'];
        $image= addslashes(file_get_contents($_FILES['image']['tmp_name']));
        $image_name= addslashes($_FILES['image']['name']);

                $location="uploads/" . $_FILES["image"]["name"];
                $name = strip_tags(mysql_real_escape_string($_POST['name']));
                $gender=strip_tags(mysql_real_escape_string($_POST['gender']));
                $uname=strip_tags(mysql_real_escape_string($_POST['username']));
                $ut=strip_tags(mysql_real_escape_string($_POST['user_type']));
                $pass=strip_tags(mysql_real_escape_string($_POST['password']));
                $salt = sha1('_wchs2242%..father%/**...mygreenparrot_password&username\--\__/heelo"@@@@@@.');
                $password = md5($pass.$salt);
                $status=strip_tags(mysql_real_escape_string($_POST['status']));

        if(empty($name) || empty($uname) || empty($pass)){
        $_SESSION['error'] = '<div class="alert-error"><span class="glyphicon glyphicon-info"></span> Please check your inputs for missing values.</div>';  
        header('Location: add_member');

        }
        elseif(!empty($uname)){
        $query = "SELECT username FROM panel_users WHERE username = '$uname'";  
            $chk = mysql_query($query);
            if(mysql_num_rows($chk) > 0){
                $_SESSION['error']  = '<div class="alert-error"><span class="glyphicon glyphicon-info"></span> The username you entered already exist in the database. please input another one</div>'; 
                header('Location: add_member');
            }
        }

        elseif(!empty($name) && !empty($uname) && !empty($pass)){
                move_uploaded_file($_FILES["image"]["tmp_name"],"uploads/" . $_FILES["image"]["name"]);

                $save="INSERT INTO panel_users (name, gender, user_type, username, password, status, pic) VALUES('$name','$gender', '$ut','$uname','$password','$status','$location')";
                $sate = mysql_query($save);

                if($sate == true){
                $_SESSION['success']  = '<div class="alert-success"><span class="glyphicon glyphicon-checked"></span> Saved!</div>';    
                header('Location: add_member');
                exit;
                }
                else{
                echo $_SESSION['error'] = '<div class="alert-error"><span class="glyphicon glyphicon-info"></span> Failed! Please try again.</div>';    
                header('Location: add_member');
                exit;
                }

        }
    }
    ?>

1 个答案:

答案 0 :(得分:0)

1-您正在使用旧的mysql,所以请将您的工作转换为mysqli,因为从php 7开始,mysql已完全停止。

2-使用此检查来捕获错误并查看:

$query = mysql_query($sql) or die("Error<br />".mysql_error());

3- if条件没有按逻辑排列。更好的设计,然后开始编码。请不要这么快,所以你大部分时间都会遗忘许多碎片并在你的代码中留下许多漏洞。

  • 首先确定允许执行的条件并将其作为第一个。
  • 然后保持其他条件。这将清除您的代码以供日后维护,让您真正感到满意和更高的表现。

祝你好运。