blogo blogo - 3 months ago 12
PHP Question

MySQLi: mysqli_real_escape_string() expects parameter 1 to be mysqli, null given

I realize that similar questions have been asked multiple times, but none of the solutions offered there have worked for me. I've also studied php.net and MySQLi tutorials, but can't figure this one out.

$dbconnect = mysqli_connect("localhost", "***", "***", "***");
$today = date('Y-m-d');
$age = mysqli_real_escape_string($dbconnect2, $_POST['age']);
$country = mysqli_real_escape_string($dbconnect2, $_POST['country']);
$poll = "poll-s1";
$result = mysqli_query($dbconnect, "INSERT INTO tblpoll (poll, quizdate, age, country) VALUES($poll, $today, $age, $country)");


Produced the error:
mysqli_real_escape_string() expects parameter 1 to be mysqli, null given

(On both the lines for $age and $country.)

The values for $age and $country come from the previous page, e.g.:

<form id="form1" name="form1" method="post" action="show.php">
<select name="age" class="formadmin" id="age">
<option value="no" selected="selected">Select</option>
<option value="under10">Under 10</option>
<option value="11-20">11-20</option>
<option value="21-30">21-30</option>
</select>
<select name="country" class="formadmin" id="country">
<option value="none" selected="selected">Select</option>
<option value="Afghanistan">Afghanistan</option>
<option value="Albania">Albania</option>
<option value="Algeria">Algeria</option>
</select>
</form>


(truncated lists)

I've not done MySQLi INSERT before so I'm probably making a basic error, but I can't spot it.

What am I missing?

Answer

You are passing $db_connect2 instead of $db_connect

Comments