Hacym Posted September 7, 2012 Share Posted September 7, 2012 Hello, I'm getting Fatal error: Call to a member function bind_param() on a non-object on the following code: if ($_GET['link'] == "create") { if(!isset($_GET['id'])) { echo "<div style=\"position:absolute; top:20px; left:300px;\">"; echo "<form method=\"post\" action=\"index.php?link=create\">"; date_default_timezone_set('America/New_York'); $time = date('g:i A'); $date = date('F j, Y'); echo "Article title: <br />"; echo "<input type=\"text\" name=\"title\" value=\"Article Title\" onblur=\"if (this.value == '') {this.value = 'Article Title';}\" onfocus=\"if (this.value == 'Article Title') {this.value = '';}\"><br />"; echo "Posted at <input type=\"text\" name=\"time\" value=\"$time\"> on <input type=\"text\" name =\"date\" value=\"$date\"> by $username <br />"; echo "Content:<br />"; echo "<textarea name=\"content\" rows=30 cols=80></textarea> <br />"; echo "Publish <input type=\"radio\" name=\"publish\" value=\"1\" checked> now or <input type=\"radio\" name=\"publish\" value=\"0\"> later in the category named "; $mysqli->select_db("categories"); $cats = $mysqli->query("SELECT name FROM categories"); echo "<select name=\"catdropdown\" value=''>"; while ($catdropdown = mysqli_fetch_assoc($cats)) { echo '<option value="'.$catdropdown['name'].'">' . $catdropdown['name'] . '</option>'; } echo "</select>"; echo "<input type=\"hidden\" name=\"addarticle\"> <br />"; echo "<input type=\"submit\" value=\"Add Article\">"; echo "</form>"; echo "</div>"; } if (isset ($_POST['addarticle'])) { $title = $_POST['title']; $time = $_POST['time']; $date = $_POST['date']; $content = $_POST['content']; $publish = $_POST['publish']; $category = $_POST['catdropdown']; $mysqli->select_db("articles"); $articleadd = $mysqli->prepare("INSERT INTO articles (title, author, time, date, content, published, category) values (?, ?, ?, ?, ?, ?, ?)"); $articleadd->bind_param("sssssis", $title, $username, $time, $date, $content, $publish, $category); $articleadd->execute(); $added = "1"; $articleadd->close(); echo "<div style=\"position:relative; top:640px; left:280px;\">"; echo "Article has been added!"; if ($publish == 0) { echo "Remember to come back and set this to published when you're ready!"; } echo "</div>"; } if(isset($_GET['id'])) { $id = $_GET['id']; $mysqli->select_db("articles"); $query = "SELECT * FROM `articles` WHERE id=$id"; if($articleedit = $mysqli->query($query)) while ($articleeditinfo = $articleedit->fetch_assoc()) { echo "<div style=\"position:absolute; top:20px; left:300px;\">"; echo "<form method=\"post\" action=\"index.php?link=create&id=".$id."\">"; date_default_timezone_set('America/New_York'); $time = date('g:i A'); $date = date('F j, Y'); echo "Article title: <br />"; echo "<input type=\"text\" name=\"title\" value=\"".$articleeditinfo['title']."\"> <br />"; echo "Posted at <input type=\"text\" name=\"time\" value=\"".$articleeditinfo['time']."\"> on <input type=\"text\" name =\"date\" value=\"".$articleeditinfo['date']."\"> by" .$articleeditinfo['username']. "<br />"; echo "Content:<br />"; echo "<textarea name=\"content\" rows=30 cols=80>".$articleeditinfo['content']."</textarea> <br />"; echo "Publish"; if ($articleeditinfo['published'] == 1) { echo "<input type=\"radio\" name=\"publish\" value=\"1\" checked> now or <input type=\"radio\" name=\"publish\" value=\"0\"> later in the category named "; } else { echo "<input type=\"radio\" name=\"publish\" value=\"1\"> now or <input type=\"radio\" name=\"publish\" value=\"0\" checked> later in the category named "; } $mysqli->select_db("categories"); $cats = $mysqli->query("SELECT name FROM categories"); echo "<select name=\"catdropdown\" value=''>"; while ($catdropdown = mysqli_fetch_assoc($cats)) { echo '<option value="'.$catdropdown['name'].'">' . $catdropdown['name'] . '</option>'; } echo "</select>"; echo "<input type=\"hidden\" name=\"updatearticle\"> <br />"; echo "<input type=\"submit\" value=\"Update Article\">"; echo "</form>"; echo "</div>"; } if(isset($_POST['updatearticle'])) { $title = $_POST['title']; $time = $_POST['time']; $date = $_POST['date']; $content = $_POST['content']; $publish = $_POST['publish']; $category = $_POST['catdropdown']; $mysqli->select_db("articles"); $articleupdate = $mysqli->prepare("UPDATE articles (title, author, time, date, content, published, category) values (?, ?, ?, ?, ?, ?, ?)"); $articleupdate->bind_param("sssssis", $title, $username, $time, $date, $content, $publish, $category); $articleupdate->execute(); $added = "1"; $articleupdate->close(); echo "<div style=\"position:relative; top:640px; left:280px;\">"; echo "Article has been updated!"; if ($publish == 0) { echo "Remember to come back and set this to published when you're ready!"; } echo "</div>"; } } } This specific error is happening on the update of the article, so with $articleupdate. I'm fiddled with everything I can think of and cannot see where the error is occurring. Quote Link to comment Share on other sites More sharing options...
Christian F. Posted September 7, 2012 Share Posted September 7, 2012 Have you tried running var_dump ($articleupdate) just before the bind_param () call? Quote Link to comment Share on other sites More sharing options...
Hacym Posted September 7, 2012 Author Share Posted September 7, 2012 Yes, it returns false. Quote Link to comment Share on other sites More sharing options...
Gustek Posted September 7, 2012 Share Posted September 7, 2012 Use $mysqli->prepare(...) or die($mysqli->error); Why do You have separate databases for articles and categories? Quote Link to comment Share on other sites More sharing options...
Christian F. Posted September 7, 2012 Share Posted September 7, 2012 And the next question is..? You have a lead on the problem now, all you need to do is to follow it to the source. The PHP manual is a great source to help you do it. This is basic debugging after all, a skill which any developer must know. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted September 7, 2012 Share Posted September 7, 2012 The syntax of your UPDATE query is not correct. The following is the syntax definition for an update query with required and most commonly used parts in red - UPDATE [LOW_PRIORITY] [iGNORE] table_reference SET col_name1={expr1|DEFAULT} [, col_name2={expr2|DEFAULT}] ... [WHERE where_condition] [ORDER BY ...] [LIMIT row_count] Quote Link to comment Share on other sites More sharing options...
Hacym Posted September 7, 2012 Author Share Posted September 7, 2012 I fixed the syntax of the UPDATE and it worked. Thank you for the help. Use $mysqli->prepare(...) or die($mysqli->error); Why do You have separate databases for articles and categories? What do you mean? How else can I list info about categories? Quote Link to comment Share on other sites More sharing options...
Gustek Posted September 7, 2012 Share Posted September 7, 2012 What I can see in Your code is that You have each table in separate database? You should have one database with many tables so You don't have to use select_db before each query. Quote Link to comment Share on other sites More sharing options...
Hacym Posted September 7, 2012 Author Share Posted September 7, 2012 I have one database with "articles," "categories," and "users" as tables. Quote Link to comment Share on other sites More sharing options...
Gustek Posted September 7, 2012 Share Posted September 7, 2012 Then why You have in Your code before each query to categories table $mysqli->select_db("categories"); and articles $mysqli->select_db("articles"); ? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.