注册表单值未在php中插入数据库。使用mysql_connect时显示错误,当我使用mysqli_connect时,值不会插入到数据库中。
Form.html
<html>
<body>
<form action="signup.php" method="post">
username <input type="text" name="username">
password <input type="password" name="password">
<input type="submit" value="ok" name="submit">
</body>
</html>
signup.php
<?php
$connection=mysqli_connect("localhost","root","","form") or die("not connected");
if(ISSET($_POST['SUBMIT'])){
$username=mysql_real_escape_string($_POST['username']);
$password=mysql_real_escape_string($_POST['password']);
mysql_query("insert into formval('username','password') values('$username','$password')");
}
?>
答案 0 :(得分:1)
这应该有效
<?php
$connection=mysqli_connect("localhost","root","","form") or die("not connected");
if($_POST['username']!="" && $_POST['password']!=""){
$username=mysqli_real_escape_string($connection,$_POST['username']);
$password=mysqli_real_escape_string($connection,$_POST['password']);
mysqli_query($connection,"insert into formval(username,password) values('$username','$password')");
}
?>
答案 1 :(得分:0)
它无效,因为你正在混合mysql
&amp; mysqli
。mysql_
已弃用。您应该使用mysqli_
或PDO
来代替。
<?php
$connection=new mysqli("localhost","root","","form") or die("not connected");
if(isset($_POST['SUBMIT'])){
$username=mysqli_real_escape_string($connection, $_POST['username']);
$password=mysqli_real_escape_string($connection, $_POST['password']);
mysqli_query($connection, "insert into formval(username,password) values('$username','$password')");
}
?>