如何使弹出窗体向数据库插入数据?

时间:2016-02-04 07:14:10

标签: php jquery html

我尝试制作一个弹出窗体并使用此窗体将数据插入数据库。 此弹出窗体运行良好,但它不会将信息插入数据库。我不知道它有什么问题。请帮帮我。

database structure

这是我的代码:

HTML code:

<a href="#popupLogin" data-rel="popup" data-position-to="window" data-transition="pop">
<p align = 'center'><img src='image/add.png'></p>
</a>
<div data-role="popup" id="popupLogin" data-theme="a" class="ui-corner-all">
<form action="popup.php" method="post">
    <div style="padding:10px 20px;">
        <h3>ADD INFO</h3>
        <label for='mail' class='ui-hidden-accessible'>Mail:</label>
        <input name='mail' id='mail' value='' placeholder='Mail' data-theme='a' type='text'>

        <label for='username' class='ui-hidden-accessible'>Username:</label>
        <input name='username' id='username' value='' placeholder='Username' data-theme='a' type='text'>

        <label for='password' class='ui-hidden-accessible'>Password:</label>
        <input name='password' id='password' value='' placeholder='Password' data-theme='a' type='text'>

        <button type="submit" name="Submit">ADD INFO</button>
    </div>
</form>

PHP代码:

 $db= mysql_connect("localhost","root","");
 if(!$db)
{
    echo "CAN'T CONNECT DATABASE";
    exit;
}
$db_selected = mysql_select_db("sctv_data2",$db);
if(!$db_selected)
{
    die("CAN'T USE THIS DATABASE: ".mysql_error());
}
if($_POST["mail"] !="" && $_POST["username"] != "" && $_POST["password"] !="" )
{
    $mail = $_POST["mail"];
    $username = $_POST["username"];
    $password = $_POST["password"];
    $sql = "INSERT INTO `login` VALUES ( '$mail','$username','$password')";
    $result = mysql_query($sql,$db);
}

3 个答案:

答案 0 :(得分:1)

请像这样使用:

 $db= mysql_connect("localhost","root","");
 if(!$db)
{
    echo "CAN'T CONNECT DATABASE";
    exit;
}
$db_selected = mysql_select_db("sctv_data2",$db);
if(!$db_selected)
{
    die("CAN'T USE THIS DATABASE: ".mysql_error());
}
if($_POST["mail"] !="" && $_POST["username"] != "" && $_POST["password"] !="" )
{
    $mail = $_POST["mail"];
    $username = $_POST["username"];
    $password = $_POST["password"];
    $sql = "INSERT INTO 'login' (email, username, password) VALUES ( '$mail','$username','$password')";   // Use you own column name from login table
    $result = mysql_query($sql,$db);
}

答案 1 :(得分:0)

试试这个...... :) 我认为它100%工作......

&#13;
&#13;
<a href="#popupLogin" data-rel="popup" data-position-to="window" data-transition="pop">
<p align = 'center'><img src='image/add.png'></p>
</a>
<div data-role="popup" id="popupLogin" data-theme="a" class="ui-corner-all">
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
    <div style="padding:10px 20px;">
        <h3>ADD INFO</h3>
        <label for='mail' class='ui-hidden-accessible'>Mail:</label>
        <input name='mail' id='mail' value='' placeholder='Mail' data-theme='a' type='text'>

        <label for='username' class='ui-hidden-accessible'>Username:</label>
        <input name='username' id='username' value='' placeholder='Username' data-theme='a' type='text'>

        <label for='password' class='ui-hidden-accessible'>Password:</label>
        <input name='password' id='password' value='' placeholder='Password' data-theme='a' type='text'>

        <button type="submit" name="Submit">ADD INFO</button>
    </div>
</form>

<?php
$db= mysql_connect("localhost","root","root");
 if(!$db)
{
    echo "CAN'T CONNECT DATABASE";
    exit;
}
$db_selected = mysql_select_db("sctv_data2",$db);
if(!$db_selected)
{
    die("CAN'T USE THIS DATABASE: ".mysql_error());
}
if($_POST["mail"] !="" && $_POST["username"] != "" && $_POST["password"] !="" )
{
    $mail = $_POST["mail"];
    $username = $_POST["username"];
    $password = $_POST["password"];
    $sql = "INSERT INTO `login` VALUES ( '$mail','$username','$password')";
    $result = mysql_query($sql,$db);
}?>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

首先是您的HTML代码:

<a href="#popupLogin" data-rel="popup" data-position-to="window" data-transition="pop">
<p align = 'center'><img src='image/add.png'></p>
</a>
<div data-role="popup" id="popupLogin" data-theme="a" class="ui-corner-all">
<form action="popup.php" method="post">
    <div style="padding:10px 20px;">
        <h3>ADD INFO</h3>
        <label for='mail' class='ui-hidden-accessible'>Mail:</label>
        <input name='mail' id='mail' value='' placeholder='Mail' data-theme='a' type='text'>

        <label for='username' class='ui-hidden-accessible'>Username:</label>
        <input name='username' id='username' value='' placeholder='Username' data-theme='a' type='text'>

        <label for='password' class='ui-hidden-accessible'>Password:</label>
        <input name='password' id='password' value='' placeholder='Password' data-theme='a' type='text'>

        <button type="submit" name="Submit">ADD INFO</button>
    </div>
</form>

我假设html代码在弹出窗口中工作正常。

然后你的PHP代码。 将代码写在popup.php文件的顶部。 (好多了) 也可以使用PDO或Mysqli,因为不推荐使用Mysql。

if (isset($_Post['username'])) {

    try {
        $db = array(
            'host' => '127.0.0.1',
            'dbname' => 'sctv_data2',
            'user' => 'root',
            'pass' => ''
        );

        $db = new PDO('mysql:host=' . $db['host'] . ';dbname=' . $db['dbname'], $db['user'], $db['pass']);

    } catch (Exception $e) {
        echo "\n" . $e->getMessage();
    }

    if ($_POST["mail"] != "" && $_POST["username"] != "" && $_POST["password"] != "") {
        $mail = $_POST["mail"];
        $username = $_POST["username"];
        $password = md5($_POST["password"]);
        $sql = 'INSERT INTO login SET mail = :mail, username = :username, password = :password';

            $stm = $db->prepare('$sql');
            $stm->bindparam('mail' , $mail);
            $stm->bindparam('username' , $username);
            $stm->bindparam('password' , $password);
        $stm->execute();
    }
}

现在这也不是正确的方法。但它会让你更进一步。 尝试OOP并将其包装在另一页的类中。这将更好。 我也使用MD5哈希作为密码,但我建议使用dcrypt insted。 检查dbname,因为我不确定它是否是正确的数据库名称。希望这会对你有所帮助。