viperjts10 Posted July 5, 2010 Share Posted July 5, 2010 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; } Quote Link to comment https://forums.phpfreaks.com/topic/206794-database-insertion-mysqli_query-expects-parameter-1-to-be-mysqli-null-given/ Share on other sites More sharing options...
phpSensei Posted July 5, 2010 Share Posted July 5, 2010 Can you post the full code, also the line where the connection is established? edit: Reading the post below, I was wondering where the $conn was coming from. You have none... Quote Link to comment https://forums.phpfreaks.com/topic/206794-database-insertion-mysqli_query-expects-parameter-1-to-be-mysqli-null-given/#findComment-1081480 Share on other sites More sharing options...
PFMaBiSmAd Posted July 5, 2010 Share Posted July 5, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/206794-database-insertion-mysqli_query-expects-parameter-1-to-be-mysqli-null-given/#findComment-1081483 Share on other sites More sharing options...
viperjts10 Posted July 7, 2010 Author Share Posted July 7, 2010 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; } Quote Link to comment https://forums.phpfreaks.com/topic/206794-database-insertion-mysqli_query-expects-parameter-1-to-be-mysqli-null-given/#findComment-1082582 Share on other sites More sharing options...
Mchl Posted July 7, 2010 Share Posted July 7, 2010 I don't see $conn being created in this function. Pleas read : http://php.net/manual/en/language.variables.scope.php Quote Link to comment https://forums.phpfreaks.com/topic/206794-database-insertion-mysqli_query-expects-parameter-1-to-be-mysqli-null-given/#findComment-1082585 Share on other sites More sharing options...
viperjts10 Posted July 7, 2010 Author Share Posted July 7, 2010 Thank you very very much for the above comments. I did indeed have to pass my connection variable to the function. I didn't realize this was necessary, thanks. Quote Link to comment https://forums.phpfreaks.com/topic/206794-database-insertion-mysqli_query-expects-parameter-1-to-be-mysqli-null-given/#findComment-1082617 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.