我是MySql的新手,请原谅我任何愚蠢的错误但基本上我正在创建一个博客,我有一个包含4列的表:post_id,post_text,post_user和post_date。我无法获取将代码插入MySql的代码,如果你能看看我会很高兴,让我知道我哪里出错:
<?php
$host = "localhost";
$user="user";
$password="*********";
$db="myData";
$user_id = $_SESSION['user_id'];
ini_set('display_errors', 1); error_reporting(-1);
//can't find the user_id
$con=mysql_connect($host,$user,$password,"myData","posts");
if( $con === FALSE ) {
die('mysql connection error: '.mysql_error());
}
$post_text = mysqli_real_escape_string($con,$_POST["blog_entrance"]);
$post_date = mysqli_real_escape_string($con,date("y.m.d"));
$query = "INSERT INTO posts (post_text, post_user) VALUES ('$post_text','$user_id','$post_date')";
mysql_query($query,$con) or die('Can\'t post data atm: ' . mysql_error());
mysql_close($host,$user,$password,"myData","posts");
header('Location: ../index.php');
?>
错误信息是这样的 “警告:mysql_connect()期望参数5为long,第15行的/media/sdc1/Documents/Coding/Blog/www/core/submit.php中给出字符串警告:mysqli_real_escape_string()期望参数1为mysqli,null在第21行的/media/sdc1/Documents/Coding/Blog/www/core/submit.php中给出警告:mysqli_real_escape_string()期望参数1为mysqli,在/ media / sdc1 / Documents / Coding / Blog / www中给出null第22行上的/core/submit.php注意:未定义的变量:第25行的/media/sdc1/Documents/Coding/Blog/www/core/submit.php中的user_id警告:mysql_query()期望参数2是资源,null在第26行的/media/sdc1/Documents/Coding/Blog/www/core/submit.php中给出无法在atm发布数据:“
我可以看到所有这些错误似乎源于mysql_connect函数,请你告诉我我的方法错误?
另外我不确定是使用mysql还是mysqli ????
答案 0 :(得分:0)
答案 1 :(得分:0)
我以为我发布了最终的代码,以便将来的用户可以看一下:
$con= new mysqli($host,$user,$password,$db);
if ($con->connect_errno) {
die("Failed to connect to MySQL: (" . $con->connect_errno . ") " . $con->connect_error);
}
$post_text = mysqli_real_escape_string($con,$_POST["blog_entrance"]);
$post_date = mysqli_real_escape_string($con,date("y.m.d"));
if (!$con->query("INSERT INTO posts(post_text, post_user, post_date) VALUES ('$post_text',$user_id','$post_date')")) {
die("Multi-INSERT failed: (" . $con->errno . ") " . $con->error);
}