尝试从表单将内容发布到数据库时出现以下错误:
Notice: Undefined index: formUser in /Applications/MAMP/htdocs/Crewniverse/index.php on line 49
Warning: mysqli_real_escape_string() expects exactly 2 parameters, 1 given in /Applications/MAMP/htdocs/Crewniverse/index.php on line 49
Notice: Undefined index: formPass in /Applications/MAMP/htdocs/Crewniverse/index.php on line 50
Warning: mysqli_real_escape_string() expects exactly 2 parameters, 1 given in /Applications/MAMP/htdocs/Crewniverse/index.php on line 50
代码是:
<?php
$dbServer = "localhost";
$db = "Crewniverse";
$dbUser = "url_acc";
$dbPassword = "url_pass";
$db_connect = mysqli_connect("localhost", "url_acc", "url_pass", "Crewniverse");
//evaluate the connection
if (mysqli_connect_error()) {
echo mysqli_connect_error();
die("<br>Couldn't connect!");
}
$formUser=mysqli_real_escape_string($_POST['formUser']);
$formPass=mysqli_real_escape_string($_POST['formPass']);
if(isset($_POST['submit']))
{
$sql="INSERT INTO username_pass (formUser, formPass)VALUES('$formUser', '$formPass')";
$result=mysql_query($sql) or die ('error Updating database');
}
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='index.html'>Back to main page</a>";
}
else {
echo "ERROR";
}
错误是不言自明的:
mysqli_real_escape_string() expects exactly 2 parameters
看看 http://www.w3schools.com/php/func_mysqli_real_escape_string.asp
$formUser=mysqli_real_escape_string($db_connect,$_POST['formUser']);
$formPass=mysqli_real_escape_string($db_connect,$_POST['formPass']);
而且是deprecated
使用 isset() 或 empty() 来避免出现如下通知并且您还错过了 mysqli_real_escape_string() 中的参数
$frmuser = (isset($_POST['formUser']) ? $_POST['formUser'] : '');
$formPass= (isset($_POST['formPass']) ? $_POST['formPass'] : '');
$formUser=mysqli_real_escape_string($db_connect,$frmuser);
$formPass=mysqli_real_escape_string($db_connect,$formPass);
并且还使用mysqli_query()
mysql_query()