PHP的Sql语句没有插入数据库但不会出错

时间:2016-02-02 07:52:19

标签: php mysql

感谢这个网站,我可以设法解决我的问题,但我的声明并没有在我的数据库上进行,但是当我复制它并将其直接粘贴到我的数据库时,它插入没有任何问题。这是我的代码:

<?php
$ip   = "***"; //MySQL Server IP
$user = "***"; //MySQL user
$pw   = "***"; //MySQL password
$db   = "***"; //Database  
$sql_filter = "";


$con = mysqli_connect($ip, $user, $pw, $db);    
if (mysqli_connect_errno()) 
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    exit();
}   

function register()
{
    $username = $_POST[username];
    $vorname = $_POST[vorname];
    $nachname = $_POST[nachname];
    $geschlecht = $_POST[geschlecht];
    $geburtsdate = $_POST[geburtsdatum];
    $password = $_POST[password];
    $email = $_POST[email];

    if($email!="" and $password!="" and $username!="" and $password==$_POST["password_confirm"])
    {           

        $sql_filter = "INSERT INTO `tblUser`(`UserID`, `UserName`, `Vorname`, `Nachname`, `EMail`, `Geschlecht`,`Password`) VALUES ('','$username','$vorname','$nachname','$email','$geschlecht','$password')";

        $_SESSION['filter'] = $sql_filter;
        $page_query = mysqli_query($con, $_SESSION['filter']);
        $page_nums = mysqli_num_rows($page_query);

        //header('Location: index.php');

        echo $sql_filter;
        echo $_SESSION['filter'];
    }
    else
    {
        header('Location: 404.html');
    }
}   

if(isset($_POST['submit']))
{
    register();
}

mysqli_close($con);
?>  

1 个答案:

答案 0 :(得分:0)

我认为问题是你的$con在函数寄存器()中是未定义的。所以在函数的开头添加它:

function register()
{
     global $con;
     ... // the rest of your function
}