Jump to content

Mysqli Insert Problem


Naps

Recommended Posts

Hello,

 

I am still quite new to php and am currently working with mysqli to update a database.

 

When passing data from one script to another in order to update the database I am having the following error printed occur:

 

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in add_news.php on line 32

 

the code on line 32 is:

$num = mysqli_num_rows($r);

 

The database however does update fine with content I provide. I just can't figure out why I am getting this message every time even though the database updates ok. The reason I am running the code $num = mysqli_num_rows($r); is to enable to php code to verify that the database has been updated via the returned row.

 

Could anyone explain why this is happening?

 

Please find my code below:

 

<?
require_once ('../mysqli_connect.php');
include ('header.html');

if (isset ($_POST['title']) && (isset ($_POST['content']) && (isset ($_POST['date'])))) {

	$title = mysqli_real_escape_string($dbc, $_POST['title']);
	$content = mysqli_real_escape_string($dbc, $_POST['content']);
	$date = mysqli_real_escape_string($dbc, $_POST['date']);
} else {

echo "You have accessed this in error";
include ('footer.html');
exit;

}

        	

$q = "INSERT INTO content VALUES (NULL,'$title', '$content', '$date')";

$r = mysqli_query($dbc, $q);

$num = mysqli_num_rows($r);

if ($num > 0) {



		echo '<h2>News Added<h2>';

} else { 

		echo '<h2>Error</h2>';

		echo '<p>' . mysqli_error($dbc) . '<br /><br />Query: ' . $q . '</p>';

	}

include ('footer.html');
?>

 

Edit:

I should add that the database consists of the following 4 columns:

 

1. id (auto increment)

2. title

3. content

4. date

Link to comment
Share on other sites

from the manual:

 

mysqli_query()

Return Values

 

Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE.

 

 

and the error message that you are getting just confirm exactly that

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in add_news.php on line 32

 

mysqli_num_rows()

Return Values

Returns number of rows in the result set.

 

 

most likely you want to use mysqli_affected_rows()

http://www.php.net/manual/en/mysqli.affected-rows.php

Link to comment
Share on other sites

Before query statemet ( $q = "INSERT INTO content VALUES (NULL,'$title', '$content', '$date')" ), add the code below and give us a result.

 

<?php
$x = array($title, $content, $date);
echo '<pre>'.var_dump($x, true). '</pre>';

Link to comment
Share on other sites

Hi all, thank you for the replies.

 

@mikosiko many thanks for your info and suggestion! I have successfully amended the code as per your link and advice.

 

Line 32 has now been amended to:

$num = mysqli_affected_rows($dbc);

 

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.