Jump to content

Fatal error: Call to a member function bind_param() on a non-object


Hacym

Recommended Posts

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.

Link to comment
Share on other sites

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]

 

 

Link to comment
Share on other sites

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?

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.