我试图将表单的输入值作为数据库连接的触发器;谁能告诉我我做错了什么?
我以为我可以在提交后发布值并在页面加载时使用发布的值作为变量来完成我的连接字符串。
代码:
<form name="form" action="Test.php" method="post">
<B>Please Enter Password</B> <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);
?>
答案 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">
<B>Please Enter Password</B> <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);
}
?>