将表单输入值发布到同一页面以构建SQL连接字符串

时间:2014-11-29 13:15:14

标签: php

我试图将表单的输入值作为数据库连接的触发器;谁能告诉我我做错了什么?

我以为我可以在提交后发布值并在页面加载时使用发布的值作为变量来完成我的连接字符串。

代码:

    <form name="form" action="Test.php" method="post">
&nbsp; <B>Please Enter Password</B>  &nbsp; <input type='password' name='test-' id ='password123';/>
<button id='test'>Submit Year</button>
</form>

    <?php   
$password= $_POST['password123'];


$conn = @mysql_connect('localhost','root','$password');
if (!$conn) {
    die('Could not connect: ' . mysql_error());
}
mysql_select_db('ct', $conn);
?>

2 个答案:

答案 0 :(得分:0)

试试这个

<?php

if ($_POST['do'] == 'something') {
    $conn = mysql_connect('localhost','root','$password') or die(mysql_error());
    mysql_select_db('ct', $conn);

    //Uncomment the below to prevent refresh spam
    //header("Location: {$_SERVER['REQUEST_URI']}");
    //die();
}

?>
<form name="form" action="?process" method="POST">
    <input type='hidden' name='do' value='something'/>
    <strong>Please Enter Password</strong>
    <input type='password' name='test-' id ='password123'/>
    <button id='test'>Submit Year</button>
</form>

考虑使用MySQLi。

答案 1 :(得分:0)

试试这个

<form name="form" action="<?php $_SERVER['PHP_SELF']?>" method="post">
&nbsp; <B>Please Enter Password</B>  &nbsp; <input type='password' name='test-' id ='password123';/>
<button id='test' name="submit">Submit Year</button>
</form>

<?php   
if(isset($_POST['submit'])) {
    $password= $_POST['test-'];
    $conn = @mysql_connect('localhost','root','$password');
    if (!$conn) {
        die('Could not connect: ' . mysql_error());
    }
    mysql_select_db('ct', $conn);
}
?>