lauren_etherington Posted January 22, 2014 Share Posted January 22, 2014 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 https://forums.phpfreaks.com/topic/285583-warning-mysqli_query-expects-parameter-1-to-be-mysqli/ Share on other sites More sharing options...
Ch0cu3r Posted January 22, 2014 Share Posted January 22, 2014 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(); Link to comment https://forums.phpfreaks.com/topic/285583-warning-mysqli_query-expects-parameter-1-to-be-mysqli/#findComment-1466161 Share on other sites More sharing options...
lauren_etherington Posted January 22, 2014 Author Share Posted January 22, 2014 Cheeers! Got it working now Link to comment https://forums.phpfreaks.com/topic/285583-warning-mysqli_query-expects-parameter-1-to-be-mysqli/#findComment-1466163 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.