Jump to content

Warning: mysqli_query() expects parameter 1 to be mysqli


lauren_etherington
Go to solution Solved by Ch0cu3r,

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?

Link to comment
Share on other sites

  • Solution

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();
Edited by Ch0cu3r
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.