Can anyone help its the last few lines of code I am strugglign with, I cant seem to return a value from mysqli_num_rows

Excuse the spelling not had chance to tidy it up!

// if the reply hqs been inserted countthe numberof rows and insert it the table
if (mysqli_affected_rows($dbc) == 1) {

$q="SELECT * FROM forum_reply WHERE topic_id='$topic_id'";
$r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
 $rows = mysqli_num_rows($dbc, $q);


// If added new answer, add value +1 in reply column
echo $q3="UPDATE forum_topic SET reply='$rows' WHERE topic_id='$topic_id'";
$r3 = mysqli_query ($dbc, $q3) or trigger_error("Query: $q3\n<br />MySQL Error: " . mysqli_error($dbc));

Full code below for context

<?php // allow the user to add a topic in the forum
//set the page title
$page_title = 'Forum - View Topic';

// include the configuation file
require_once ('../includes/config.inc.php');

// include the header
include ('../includes/header.html');

if (isset($_POST['submitted']));

// if no session variable is present (i.e the user is not logged in re direct to the homepage

if (!isset($_SESSION['user_id'])) {

	// session variable isnt set so set the re direct url

	// free all memory assoctaed with the query
	mysqli_free_result($r);

	//close the $dbc connection
	mysqli_close($dbc);

	// set the url using the defult host in the config settings
	$url = BASE_URL . 'index.php';

	// use the header commend to re direct to the URl crated above
	header("Location: $url");

	//terminate the current script
	exit();

}

// now we have estblished the user is logged in

// require the mydql connection from the config file
require_once (MYSQL);

// Trim any arrys that have been posted for security:
$trimmed = array_map('trim', $_POST);


// get value of hidden id frm the form
$topic_id = mysqli_real_escape_string ($dbc, trim($_POST['id']));



// Find highest answer number.


// get values that sent from form

$reply = mysqli_real_escape_string ($dbc, trim($_POST['reply']));

// Insert answer
$q2="INSERT INTO forum_reply (user_id, topic_id, reply, datetime)VALUES({$_SESSION['user_id']},'$topic_id', '$reply', NOW())";
$r2 = mysqli_query ($dbc, $q2) or trigger_error("Query: $q2\n<br />MySQL Error: " . mysqli_error($dbc));

// if the reply hqs been inserted countthe numberof rows and insert it the table
if (mysqli_affected_rows($dbc) == 1) {

$q="SELECT * FROM forum_reply WHERE topic_id='$topic_id'";
$r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
 $rows = mysqli_num_rows($dbc, $q);


// If added new answer, add value +1 in reply column
echo $q3="UPDATE forum_topic SET reply='$rows' WHERE topic_id='$topic_id'";
$r3 = mysqli_query ($dbc, $q3) or trigger_error("Query: $q3\n<br />MySQL Error: " . mysqli_error($dbc));

echo $rows;


} else {
echo "ERROR";


mysqli_close($dbc);
}

?>

    Can you clarify what the purpose of the 'reply' column is? If it's just to represent the number of replies, why duplicate that information at all? Why not just perform a 'SELECT COUNT(*)' query instead?

      as per the manual, $r should be the only parameter

        Can you clarify what the purpose of the 'reply' column is? If it's just to represent the number of replies, why duplicate that information at all? Why not just perform a 'SELECT COUNT(*)' query instead?

        Can you give me an example that i can try and implement it please?

        Just has a play but getting a load of errors

          dagon;10958425 wrote:

          as per the manual, $r should be the only parameter

          I have also removed the $dbc from the string but I still getthe same error.

          Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, string given in C:\xampp\htdocs\123ac.info\forum_add_reply.php on line 66
          UPDATE forum_topic SET reply='' WHERE topic_id='20'

            Thanks very much, the soloution is always so simple

            really appreciate your help!

              Write a Reply...