Jump to content
Larry Ullman's Book Forums

Ch. 19 - Getting Error(S) In Script 19.1

Recommended Posts

Hi all,


Getting errors which I think are related to the connection to the DB, but not sure.  I double checked all the code again just now and it should be perfect.  I went through the entire chapter's scripts, now going back through making corrections - I have 19.2 correct but I can't proceed because 19.1 is required since you have to upload artist name in order to populate the dropdown in 19.2.  I did some searching online for these errors and most were related to normal parenthesis / quotes etc which, again, I believe the code to be 100% correct.  Any help here is certainly appreciated - hoping to wrap up this chapter this week, then re-review the entire book, and then move on to the recently purchased effortless ecommerce v2!



Warning: mysqli_stmt_bind_param() expects parameter 1 to be mysqli_stmt, boolean given in /Applications/MAMP/htdocs/PHP_MYSQL/add_artist(19.1).php on line 25

Warning: mysqli_stmt_execute() expects parameter 1 to be mysqli_stmt, boolean given in /Applications/MAMP/htdocs/PHP_MYSQL/add_artist(19.1).php on line 26

Warning: mysqli_stmt_affected_rows() expects parameter 1 to be mysqli_stmt, boolean given in /Applications/MAMP/htdocs/PHP_MYSQL/add_artist(19.1).php on line 29

Warning: mysqli_stmt_close() expects parameter 1 to be mysqli_stmt, boolean given in /Applications/MAMP/htdocs/PHP_MYSQL/add_artist(19.1).php on line 37

<!DOCTYPE html>
	<meta http-equiv="content-type"content="text/html; charset=utf-8" />
	<title>Add an Artist</title>
<?php # Script 19.1 - add_artist(19.1).php   ************THIS ENTIRE PAGE IS CORRECT - CHECKED ON 3-30-14****************
// This page allows the admin to add a new artist name to the database

if ($_SERVER['REQUEST_METHOD'] == 'POST') { // Handle the form
	// Validate the first and middle names (neither required):
	$fn = (!empty($_POST['first_name'])) ? trim($_POST['first_name']) : NULL;
	$mn = (!empty($_POST['middle_name'])) ? trim($_POST['middle_name']) : NULL;
	// Check for a last_name...
	if (!empty($_POST['last_name'])) {
		$ln = trim($_POST['last_name']);
		// Add the artist to the database:
		require ('../mysqli_connect(ch19).php');   ////////////////////////////////////////////
		$q = 'INSERT INTO artists (first_name, middle_name, last_name) VALUES (?,?,?)';
		$stmt = mysqli_prepare($dbc, $q);
		mysqli_stmt_bind_param($stmt, 'sss', $fn, $mn, $ln);
		// Check the results...
		if (mysqli_stmt_affected_rows($stmt) == 1) {
			echo '<p>The artist has been added.</p>';
			$_POST = array();
		} else {  // Error!    
			$error = 'The new artist could not be added to the database!';
		// Close this prepared statement:
		mysqli_close($dbc); // Close the database connection
	} else { // No last name value.
		$error = 'Please enter the artist\'s name!';
}  // End of the submission IF.

// Check for an error and print it:
if (isset($error)) {
	echo '<h1>Error</h1>
	<p style="font-weight: bold; color: #C00">' . $error . ' Please try again.</p>';

// Display the form...
<h1>Add a Print</h1>
<form action="add_artist(19.1).php" method="post">
	<fieldset><legend>Fill out the form to add an artist:</legend>
		<p><b>First Name:</b> <input type="text" name="first_name" size="10" maxlength="20" value="<?php if (isset($_POST['first_name'])) echo $_POST['first_name']; ?>" /></p>
		<p><b>Middle Name:</b> <input type="text" name="middle_name" size="10" maxlength="20" value="<?php if (isset($_POST['middle_name'])) echo $_POST['middle_name']; ?>" /></p>
		<p><b>Last Name:</b> <input type="text" name="last_name" size="10" maxlength="40" value="<?php if (isset($_POST['last_name'])) echo $_POST['last_name']; ?>" /></p>


	<div align="center"><input type="submit" name="submit" value="Submit" /></div>


Link to comment
Share on other sites

Well if you check php.net http://php.net/manual/en/mysqli.prepare.php


mysqli_prepare() returns a statement object or FALSE if an error occurred.


So mysqli_prepare that you have is returning FALSE which is boolean to $stmt which will cause all the functions/methods using it to fail. So its a problem with your database connection, you simply need to check the required file is being received.


require ('../mysqli_connect(ch19).php');

  • Upvote 2
Link to comment
Share on other sites

How would I check that the required file is being received?


Is the below correct?


I basically copy/pasted from the link you provided from php.net above.


I ran this in my browser and just got a white screen - does that mean I don't have an error since it didn't print an error message?

<?php # test_database_connection(ch19).php 
// This page tests for a connection to the database

require ('../mysqli_connect(ch19).php'); 

// check connection -  **TAKEN DIRECTLY FROM THE PHP.net SITE
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());

Link to comment
Share on other sites

Yes, mysqli_connect_errno returns 0 on a successful connection, meaning that a blank screen means your connection is okay.

With that said, Edward is right that the mysqli_prepare function is returning false and storing that in $stmt, which is your problem.


This happens whenever your query is invalid. Try running the query directly on the DB with static values to better debug the issue.

Link to comment
Share on other sites


  • Create New...