-2

I'm new to MySql so forgive me for any stupid errors but Basically i'm making a blog and i have a table which has 4 columns: post_id, post_text, post_user and post_date. I am unable to get my code to insert data into MySql, I'd be greatfull if you could have a look and let me know where I'm going wrong:

 <?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');


?>

the error message is this "Warning: mysql_connect() expects parameter 5 to be long, string given in /media/sdc1/Documents/Coding/Blog/www/core/submit.php on line 15 Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, null given in /media/sdc1/Documents/Coding/Blog/www/core/submit.php on line 21 Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, null given in /media/sdc1/Documents/Coding/Blog/www/core/submit.php on line 22 Notice: Undefined variable: user_id in /media/sdc1/Documents/Coding/Blog/www/core/submit.php on line 25 Warning: mysql_query() expects parameter 2 to be resource, null given in /media/sdc1/Documents/Coding/Blog/www/core/submit.php on line 26 Can't post data atm:"

I can see that all these errors seem to stem from the mysql_connect function, please could you tell me the error of my ways?

Also i'm unsure as to whether to use mysql or mysqli????

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
TomJ
  • 424
  • 3
  • 14
  • You're mixing up `mysql` & `mysqli` statements - they are not compatible. As for which one to use: `mysqli` - the other one is deprecated, is not supported, and will be removed from PHP in the near future. Google "mysqli tutorial" - there are a million of them out there. – Kryten May 09 '14 at 22:17
  • One of the better features of php is its [excellent documentation online](http://www.php.net/docs.php) where all functions, including [mysql_connect](http://php.net/manual/en/function.mysql-connect.php) are described in detail, and many examples are given. Use it. By the way, on the same page they explain why you should not use mysql_ anymore, and why. – fvu May 09 '14 at 22:18
  • 2
    This question appears to be off-topic because all that's needed to solve the problem can be found in the [official documentation](http://php.net/manual/en/) – fvu May 09 '14 at 22:19
  • `session_start();` is loaded? 9 times out of 10 the OP responds with "*yes, it is.*" - it's not shown but it's there. – Funk Forty Niner May 09 '14 at 22:27
  • Add error reporting to the top of your file(s) `error_reporting(E_ALL); ini_set('display_errors', 1);` – Funk Forty Niner May 09 '14 at 22:28

2 Answers2

0

Read about mysql_connect() function: here

You must use:

$con = mysql_connect($host,$user,$password);

Anyway, mysql_* functions is old, monstrous and deprecated. Use PDO or mysqli

Sharikov Vladislav
  • 7,049
  • 9
  • 50
  • 87
0

I thought i'd post my final code so future users can have a look at it:

$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);
}
TomJ
  • 424
  • 3
  • 14