<form id="newuser" class="fontservizi" method="post" action="file2.php">
NEW USER:<br><br><br>
Name:<input type="text" name="firstname" id="firstname">
Surname: <input type="text" name="lastname" id="lastname"><br><br>
email: <input type="text" name="email" id="email" style="width: 200px"><br><br>
Phone: <input type="integer" name="numtel" style="width: 70px" maxlength="10"><br><br>
Password: <input type="password" name="pwd" id="pwd"><br><br><br><br>
<input type="submit" value="Iscrivimi"></form>
这是我的“file2.php”:`
$sql="INSERT INTO Registration (One, Two, Three, Four) VALUES ('$_POST[firstname]', '$_POST[lastname]', '$_POST[email]'), '$_POST[pwd]'";
mysql_close($db);
?> `
....但我的数据库中没有任何事情......
答案 0 :(得分:2)
正确使用您的代码是这样的:
$sql="INSERT INTO Registration (One, Two, Three, Four) VALUES ('".$_POST['firstname']."', '".$_POST['lastname']."', '".$_POST['email']."', '".$_POST['pwd']."')";
mysql_query($sql);
你做错了什么:
但是,这种查询不安全,我建议你使用预备语句
http://php.net/manual/en/mysqli.prepare.php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
/* create a prepared statement */
if ($stmt = $mysqli->prepare("INSERT INTO Registration (One, Two, Three, Four) VALUES (?,?,?,?)")) {
/* bind parameters for markers */
$stmt->bind_param("ssss", $_POST['firstname'], $_POST['lastname'], $_POST['email'],$_POST['pwd']);
/* execute query */
$stmt->execute();
}
你应该使用MySQLi扩展而不是MySQL扩展的原因很多。
还有其他好处。但主要是,你应该专注于安全性和稳定性 - 而MySQLi就是为你提供的。
答案 1 :(得分:0)
虽然这是新手,但是你的insert语句是错误的:
$sql="INSERT INTO Registration (One, Two, Three, Four) VALUES ('$_POST[firstname]', '$_POST[lastname]', '$_POST[email]'), '$_POST[pwd]'
&#34 ;;
为什么在结束之前你有'$_POST[email]'),
额外的)
?
应该是
$sql="INSERT INTO Registration (One, Two, Three, Four) VALUES ('$_POST[firstname]', '$_POST[lastname]', '$_POST[email]', '$_POST[pwd]')"
说完,don't use mysql_*
functions,他们不再被维护,而且是officially deprecated。请改为了解prepared statements,并使用PDO或MySQLi。您还需要Prevent SQL Injection!