Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/248.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php mySQL连接无法正常工作_Php_Mysql_Sql_Mysqli - Fatal编程技术网

Php mySQL连接无法正常工作

Php mySQL连接无法正常工作,php,mysql,sql,mysqli,Php,Mysql,Sql,Mysqli,我对SQL相当陌生,我正在尝试编写代码来插入消息表单中的信息。以下是SQL代码: $con = mysqli_connect($hostname,$username,$password,$db); // Check connection if (mysqli_connect_error()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } $name = mysqli_real_escape_string

我对SQL相当陌生,我正在尝试编写代码来插入消息表单中的信息。以下是SQL代码:

$con = mysqli_connect($hostname,$username,$password,$db);

// Check connection
if (mysqli_connect_error()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$name = mysqli_real_escape_string($con, $_POST['name']); 
$email = mysqli_real_escape_string($con, $_POST['email']); 
$message = mysqli_real_escape_string($con, $_POST['message']); 

$sql = "INSERT INTO messages (name, email, message) VALUES ( '$name' , '$email' , '$message' )";


if (!mysqli_query($sql)) {
die ('Error: ' . mysqli_error());
}
else {
echo "<html><script language='JavaScript'> alert('Thank you for your submission.'),window.location = 'home'</script></html>";
}
$con=mysqli\u connect($hostname、$username、$password、$db);
//检查连接
if(mysqli\u connect\u error()){
echo“未能连接到MySQL:”.mysqli_connect_error();
}
$name=mysqli\u real\u escape\u字符串($con,$\u POST['name']);
$email=mysqli\u real\u escape\u字符串($con,$\u POST['email']);
$message=mysqli\u real\u escape\u字符串($con,$\u POST['message']);
$sql=“在消息(名称、电子邮件、消息)中插入值(“$name”、“$email”、“$message”)”;
if(!mysqli_query($sql)){
die('Error:'.mysqli_Error());
}
否则{
echo“警报('感谢您的提交')、window.location='主页';
}
这段代码返回“Error:”我解释为它认为有错误,但没有任何错误。mysqli_connect中的连接变量都是正确的,但是我不确定我是否正确使用了mysqli_real_escape_字符串,甚至$sql语句,因为这段代码也没有向我的数据库中插入任何内容。提前感谢。

根据文档,如果您使用的是过程表示法,则需要包括您的mysqli链接:

混合mysqli_查询(mysqli$link,string$query[,int$resultmode=mysqli_存储结果])

这将建议您需要将
$con
传递到
mysqli_query()
,正如您在其他函数调用中所做的那样,如下所示:

mysqli\u查询($con,$sql)


另外,请查阅并阅读有关参数化的内容,因为您的代码不应在活动站点上使用,因为您容易受到SQL注入的影响。请花点时间阅读并学习如何防止它。

尝试以这种方式运行查询

mysqli_query($con, $sql);

mysqli\u查询需要指向数据库连接的链接,该链接是“$con”

您正在混合使用
mysql\u
mysqli\u
。修复该问题,然后查看准备好的语句。@Alexartan我更改了它,但仍然得到“错误:”您没有将$con传递到mysqli\u query()@PeterFeatherstone我该怎么做?您已经在使用
mysqli\u real\u escape\u string()调用进行此操作了。。。