Jump to content

Warning: mysqli_query() expects parameter 1 to be mysqli


lauren_etherington

Recommended Posts

I am having a minor problem with my code if any one can help.

I have a form where the user can add a new article however on submit this error is displayed:

Warning: mysqli_query() expects parameter 1 to be mysqli, object given in //public_html/admin/includes/addnews.php on line 12.

 

This is the code:

$auth = $_POST['auth'];
$tit = $_POST['tit'];
$stat = $_POST['stat'];
$shrt = $_POST['short'];
$art = $_POST['art'];


$conn = mysqli_connect("localhost","db","password","db") or die ("Could not connect to database");

$query = $conn->prepare ("INSERT INTO newsitem (author,title,shortdesc,article,newsdate,status) VALUES ('$auth','$tit','$shrt','$art',CURDATE(),'$stat')");
$result = mysqli_query($query,$conn);
	
if($result){
	
	echo "successful";
	echo "<BR>";
	echo "<a href='news.php'>Back to News </a>";
	
		}

else {
	echo "error could not upload article";
}

mysqli_close($conn);
?>

I have been looking at this for the last two hours with no hope, is there anyone who can point me in the right direction please?

Your arguments for mysqli_query are in the wrong order. The argument order is connection, query. Not query, connection

 

Also you don't use mysqli_query when using mysqli_prepare before hand. If you are using prepared statements you need to use mysqli_stmt_execute

 

Correct code for a prepared statement

// procedural
$query = $conn->prepare ("INSERT INTO newsitem (author,title,shortdesc,article,newsdate,status) VALUES (?, ?, ?, ?, CURDATE(), ?)");
mysqli_stmt_bind_param($query, 'sssss', $auth, $tit, $shrt, $art, $stat);
mysqli_stmt_execute($query);.

// OR as OOP
$query = $conn->prepare ("INSERT INTO newsitem (author,title,shortdesc,article,newsdate,status) VALUES (?, ?, ?, ?, CURDATE(), ?)");
$query->bind_param('sssss', $auth,$tit,$shrt,$art,$stat);
$query->execute();

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.