在PHP中将数据插入db

时间:2014-04-21 22:57:22

标签: php mysql insert

我试图将somethig插入到db中,在我编写此代码后,我在php中只得到一个空白页面。你帮帮我吗?如果我删除所有//我得到空白页,如果它现在我得到imput容器

    <?php
function create()
{
    if(isset($_POST["submit"]))
    {       
        $db = new mysqli('localhost', 'root', 'root', 'idoctor_db');

        $username = $db->real_escape_string($_POST['username']);
        $password = $db->real_escape_string($_POST['password']);
        $password_conf = $db->real_escape_string($_POST['password_conf']);
        $nick = $db->real_escape_string($_POST['nick']);

        //create_new_user($username, $password, $password_conf, $nick);
        //$db->query
        //('
            //INSERT INTO  `idoctor_db`.`users` (`ID` ,`Login` ,`Password` ,`Name` ,`Level`)
            //VALUES ('5 ' , 'kev5', 'roo5', 'kevkev5', ' 3 ' );
        //');


    }
}

?>

<!DOCTYPE html>
<html>
<head>
    <title>Login</title>
    <link rel="stylesheet" href="css/login.css" />
</head>
<body>
    <div class="container">

        <form action="<?php create(); ?>" method="POST">
            <input type="text" name="username" placeholder="Username..." />
            <input type="password" name="password" placeholder="Password" />
            <input type="password" name="password_conf" placeholder="Confirm password" />
            <input type="text" name="nick" placeholder="Nick" />
            <input type="submit" value="Create" name="submit"/>
        </form>
    </div>
</body>
</html>

2 个答案:

答案 0 :(得分:1)

如果运行此查询时没有错误,您将看到一个空白页。

所有工作都在后台进行,查看你的mysql表以确认插件是否有效。

如果您想检查查询的结果......只需执行以下操作

$outcome = $db->query
('

    INSERT INTO  `idoctor_db`.`users` (
    `ID` ,
    `Login` ,
    `Password` ,
    `Name` ,
    `Level`
    )
    VALUES 
    (' 4 ', ' kev4 ', ' root ', ' kevkev4 ', ' 3 ');
');

if($outcome) {
    echo 'success';
} else {
    echo 'failed';
}

答案 1 :(得分:0)

编辑完成后......我在您的代码中看到了另一个问题:

<form action="<?php create(); ?>" method="POST">
         <!--     ^ This is not how you submit form data to php... 
             action is supposed to be a URL.. however if left blank 
             it will take you to the same page

请改为尝试:

<?php
    if(isset($_POST["submit"]))
    {       
        $db = new mysqli('localhost', 'root', 'root', 'idoctor_db');

        $username = $db->real_escape_string($_POST['username']);
        $password = $db->real_escape_string($_POST['password']);
        $password_conf = $db->real_escape_string($_POST['password_conf']);
        $nick = $db->real_escape_string($_POST['nick']);

        //create_new_user($username, $password, $password_conf, $nick);
        //$db->query
        //('
        //INSERT INTO  `idoctor_db`.`users` (`ID` ,`Login` ,`Password` ,`Name` ,`Level`)
        //VALUES ('5 ' , 'kev5', 'roo5', 'kevkev5', ' 3 ' );
        //');
    }

?>

<!DOCTYPE html>
<html>
<head>
    <title>Login</title>
    <link rel="stylesheet" href="css/login.css" />
</head>
<body>
    <div class="container">

        <form method="POST">
            <input type="text" name="username" placeholder="Username..." />
            <input type="password" name="password" placeholder="Password" />
            <input type="password" name="password_conf" placeholder="Confirm password" />
            <input type="text" name="nick" placeholder="Nick" />
            <input type="submit" value="Create" name="submit"/>
        </form>
    </div>
</body>
</html>