Jump to content



Photo

Chapter_12 Post_Message.Php


  • Please log in to reply
4 replies to this topic

#1 lou22

lou22

    Newbie

  • Members
  • Pip
  • 8 posts

Posted 3 February 2012 - 7:26 AM

Hi,
I wonder if anyone can help spot what I've done wrong. I keep getting the following error messages when I submit a comment on the post_message.php form:

Warning: mysqli_stmt_bind_param() expects parameter 1 to be mysqli_stmt, boolean given in D:\xampp\htdocs\PHPMYSQL\Chapter 12\post_message.php on line 25

Warning: mysqli_stmt_execute() expects parameter 1 to be mysqli_stmt, boolean given in D:\xampp\htdocs\PHPMYSQL\Chapter 12\post_message.php on line 35

Warning: mysqli_stmt_affected_rows() expects parameter 1 to be mysqli_stmt, boolean given in D:\xampp\htdocs\PHPMYSQL\Chapter 12\post_message.php on line 38
Your message could not be posted.


Warning: mysqli_stmt_error() expects parameter 1 to be mysqli_stmt, boolean given in D:\xampp\htdocs\PHPMYSQL\Chapter 12\post_message.php on line 45

Warning: mysqli_stmt_close() expects parameter 1 to be mysqli_stmt, boolean given in D:\xampp\htdocs\PHPMYSQL\Chapter 12\post_message.php on line 50

All these warnings refer to lines where I refer to the $stmt. I have written the script twice but get same result. I've compared it line for line with the script from the book and the downloaded post_message.php file. I can't find the error.


My code is:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Post a Message</title>
</head>
<body>
<?php #Script 12.4 -post_message.php
if(isset($_POST['submitted'])){

//validate the data omitted!

//connect to database:
$dbc = mysqli_connect('localhost','username','password','forum');  //substituted my username and password

//make the query:
$q = 'INSERT INTO messages(forum_id, parent_id, user_id, subject, body, date_entered) VALUES(?,?,?,?,?, NOW())';

//prepare the statement:
$stmt = mysqli_prepare($dbc, $q);

//bind the variables:
mysqli_stmt_bind_param($stmt, 'iiiss', $forum_id, $parent_id, $user_id, $subject, $body);

//assign the values to the variables:
$forum_id = (int)$_POST['forum_id'];
$parent_id = (int)$_POST['parent_id'];
$user_id = 3;
$subject = strip_tags($_POST['subject']);
$body = strip_tags($_POST['body']);

//execute the query:
mysqli_stmt_execute($stmt);

//Print a message based on the result:
if(mysqli_stmt_affected_rows($stmt)==1){
  echo '<p>Your message has been posted.</p>';
 
}else{
 
  echo '<p style="font-weight:bold; color:#c00">Your message could not be posted.</p>';
 
  echo '<p>'.mysqli_stmt_error($stmt).'</p>';
 
}

//close the statement
mysqli_stmt_close($stmt);

//close the connection:
mysqli_close($dbc);
}//end of submission IF
//display form
?>
<form action="post_message.php" method="post">
<fieldset>
<legend>Post a message:</legend>
<p><b>Subject</b>:<input name="subject" type="text" size="30" maxlength="100"/></p>
<p><b>Body</b>:<textarea name="body" rows="3" cols="40"></textarea>
</fieldset>
<div align="center"><input type="submit" name="submit" value="Submit"/></div>
<input type="hidden" name="submitted" value="TRUE"/>
<input type="hidden" name="forum_id" value="1"/>
<input type="hidden" name="parent_id" value="0"/>
</form>
</body>
</body>
</html>

I'm using Vista home basic,Dreamweaver cs4, xampp v3.02 with Apache 2.0 Handler and phpmyadmin version 3.4.5 . phpinfo gives the configuration path for php.ini as C:\Windows and Loaded configuration file as D:xampp\php\php.ini.

Thanks in advance for any help.
  • 0

#2 HartleySan

HartleySan

    Advanced Member

  • Members
  • PipPipPip
  • 2,694 posts
  • LocationColumbus, OH USA

Posted 3 February 2012 - 8:45 AM

I'm guessing that the DB connection is not being properly established, but it's hard to say. I imagine that one problem is leading to all of the above errors.
  • 1

#3 lou22

lou22

    Newbie

  • Members
  • Pip
  • 8 posts

Posted 11 February 2012 - 7:17 AM

Thanks for your helpful reply - sorry I didn't reply sooner - my kids have been sick. I still can't get it to work although I did notice that I hadn't changed the database name in mysqli_connect.php which I have copied to the same folder as post_message.php. I think that the error is probably coming from:

//prepare the statement:
$stmt = mysqli_prepare($dbc, $q);

and / or possibly:

//connect to database:
$dbc = mysqli_connect('localhost','username','password','forum');  //substituted my username and password

but I still can't see where I've made the error.

I would be grateful for any further help.
  • 0

#4 lou22

lou22

    Newbie

  • Members
  • Pip
  • 8 posts

Posted 12 February 2012 - 7:28 AM

OK, so I found the answer by doing a bit of internet research. For anyone who has a similar problem:

1. I forgot to change the database name when I copied mysqli_connect.php to a new folder.

2. the problem seemed to be in the connection so I added:

if (!$dbc) {
    die('Connect Error: ' . mysqli_connect_error());
}else{
echo '<p>connection seems fine</p>';
}

after defining the db connection to check if it was OK. I got 'connection seems fine' so I could eliminate the problem being in the connection.

3. According to another forum:

//prepare the statement:
$stmt = mysqli_prepare($dbc, $q);

was returning 'false', hence the boolean in the warning, so I tested the prepare statement by adding:

if ( !$stmt ) {
  die('mysqli error: '.mysqli_error($dbc));
}else{
echo '<p>mysqli_prepare seems fine</p>';
}

and this gave the detailed error message:

mysqli error: Unknown column 'parent_id' in 'field list'

on checking my db I found i'd omitted the parent_id column. Whole thing now works. Hope this helps for anyone else with similar problems.
  • 1

#5 Larry

Larry

    Administrator/Writer

  • Administrators
  • 3,538 posts
  • LocationState College, PA (USA)

Posted 13 February 2012 - 8:43 AM

Kudos for figuring it out and thanks for sharing your solution.
  • 0