Jump to content

Database insertion ->mysqli_query() expects parameter 1 to be mysqli, null given


viperjts10

Recommended Posts

I searched the forums and found similar errors, but none of what I found helped solve my problem. I do not understand what is wrong.

 

The error says " Warning: mysqli_query() expects parameter 1 to be mysqli, null given in I:\wamp\www etc... on line 45

This is coming up as a "warning" and not an error, but it still doesn't let me insert items into my database.

 

The problematic line is this:

if($result = mysqli_query($conn, $query))

 

I echoed my "query" as it would look if it was submitted by the form, and it seems to look ok. Here is an example of what it would look like

INSERT INTO news (title, author, image, news) VALUES ('This is Title','Jeremy','','My news entries will be here')

 

Here's the function that the line is in:

 

function store_form($formData, $tableName)
{
	// Check to make sure there are no empty fields
	if(!is_array($formData))
	{	
		return FALSE;
		exit();
	}

	foreach($formData as $field => $value) //$value is user input
	{
		$_POST[$field] = trim($_POST[$field]); // Trim white space
		//$_POST[$field] = strip_tags($_POST[$field]); // Strip special tags/characters

		$field_array[] = $field; // Creates a new array with the $field values in $formData
		$value_array[] = $_POST[$field]; // The array that holds the users' inputted data

	}
	// Separate each word with a comma
	$fields = implode(", ", $field_array); // $fields is now a string equal to "field1, field2, field3, etc.")
	$values = implode('\',\'', $value_array); // Surround each with with single quotes, and separate words with comma

	/* DEBUGGING COMMENTS BELOW */
	//$values = mysqli_real_escape_string($conn, $values);
	//echo $fields. "<br />";
	//echo $values. "<br />";

	$query = "INSERT INTO $tableName ($fields) VALUES ('$values')";	
	echo $query. "<br />";
	if($result = mysqli_query($conn, $query))
		return TRUE;
	else
		return FALSE;
}

Link to comment
Share on other sites

Seriously, did you bother to read the error message. The first parameter ($conn) in your code (mysqli_query() expects parameter 1 to be mysqli) is a null (null given).

 

You must pass the $conn variable (assuming that is what your mysqli connection is in) into your function as a parameter when you call the function.

Link to comment
Share on other sites

This is why I had a problem with this because I read the error and had everything setup as I assume it should be.

 

I have my $conn made in a config file which is included in the main index page. It is setup in mysqli.

 

	/** Connect to the mysql database **/
$conn = mysqli_connect(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_DATABASE) or die("Unable to connect to mysql!");

 

I use the store_form() function here....

 

					if($form_filled)
				{						
					if(store_form($news, 'news'))
						echo "<p>News item added!</p>";
					else
					{	
						echo "<p>Unable to add entry</p>";
						echo mysql_error();
					}
				}

 

And at last, the store_form() function is below as I listed above

 

	function store_form($formData, $tableName)
{
	// Check to make sure there are no empty fields
	if(!is_array($formData))
	{	
		return FALSE;
		exit();
	}

	foreach($formData as $field => $value) //$value is user input
	{
		$_POST[$field] = trim($_POST[$field]); // Trim white space
		//$_POST[$field] = strip_tags($_POST[$field]); // Strip special tags/characters

		$field_array[] = $field; // Creates a new array with the $field values in $formData
		$value_array[] = $_POST[$field]; // The array that holds the users' inputted data

	}
	// Separate each word with a comma
	$fields = implode(", ", $field_array); // $fields is now a string equal to "field1, field2, field3, etc."
	$values = implode('\',\'', $value_array); // Surround each with with single quotes, and separate words with comma

	/* DEBUGGING COMMENTS BELOW */
	//$values = mysqli_real_escape_string($conn, $values);
	//echo $fields. "<br />";
	//echo $values. "<br />";

	$query = "INSERT INTO $tableName ($fields) VALUES ('$values')";	
	echo $query. "<br />";
	if($result = mysqli_query($conn, $query))
		return TRUE;
	else
		return FALSE;
}

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.