Php Experts,
I can’t remember what I changed last night that is making the following code malfunctioning. Can you see if you can spot any typos ?
[code]
$sql_query = “UPDATE users SET first_name = ?, middle_name = ?, surname = ?, gender = ?, marital_status = ?, working_status = ?, agree_to_tos = ? WHERE id = ?”;
$stmt = mysqli_prepare($conn,$sql_query);
if($stmt == False)
{
//Close Connection.
mysqli_close($conn);
die(“<pre>2. Mysqli Prepare Failed!n”.mysqli_stmt_error($stmt).”n$sql_query</pre>”);
}
I get this error echoed:
**Warning: mysqli_stmt_error() expects parameter 1 to be mysqli_stmt, boolean given in C:xampphtdocspower.pagesearch_3.php on line 416
2. Mysqli Prepare Failed!
UPDATE users SET first_name = ?, middle_name = ?, surname = ?, gender = ?, marital_status = ?, working_status = ?, agree_to_tos = ? WHERE id = ?**
How to solve this ?
<i>
</i>$sql_query = "SELECT first_name,middle_name,surname,gender,marital_status,working_status FROM users WHERE id = ?";
$stmt = mysqli_prepare($conn,$sql_query);
mysqli_stmt_bind_param($stmt,'i',$_SESSION["user_id"]);;//DELETE THIS WHOLE LINE AS USING IT AS A TEST!
if($stmt == False)
{
//Close Connection.
mysqli_close($conn);
echo "Line 862<br>";//DELETE THIS
die("<pre>2. Mysqli Prepare Failed!n".mysqli_stmt_error($stmt)."n$sql_query</pre>");
}
else
{
//Attempt to Execute the Prepared Statement.
mysqli_stmt_execute($stmt);
if(!mysqli_stmt_execute($stmt))
{
//Close Connection.
mysqli_close($conn);
echo "Line 915<br>";//DELETE THIS
die("<pre>Statement Execution Failed!n".mysqli_stmt_error($stmt)."n$mysqli_stmt_bind_param</pre>");
}
else
{
die("<pre>Statement Execution Success!</pre>");
}
}
//mysqli_stmt_bind_result($stmt,$email);
$result = mysqli_stmt_get_result($stmt);
//if(mysqli_fetch_array($result_3, MYSQLI_NUM))
//Fetch result row as an associative array. Since the result set contains only one row, we don't need to use the 'While loop'.
//mysqli_stmt_fetch($stmt);//use this if you use 'mysqli_stmt_bind_result($stmt,$email).
if($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) //Use this if you use '$result = mysqli_stmt_get_result($stmt)' instead of 'mysqli_stmt_bind_result($stmt,$email)'.
{
//Retrieve Values.
//$id = $row["id"];//Use this if you use '$result = mysqli_stmt_get_result($stmt)' instead of //'mysqli_stmt_bind_result($stmt,$email_count)';
$username = $row["username"];//Use this if you use '$result = mysqli_stmt_get_result($stmt)' instead of //'mysqli_stmt_bind_result($stmt,$email_count)';
$first_name = $row["first_name"];//Use this if you use '$result = mysqli_stmt_get_result($stmt)' instead of //'mysqli_stmt_bind_result($stmt,$email_count)';
$middle_name = $row["middle_name"];//Use this if you use '$result = mysqli_stmt_get_result($stmt)' instead of //'mysqli_stmt_bind_result($stmt,$email_count)';
$surname = $row["surname"];//Use this if you use '$result = mysqli_stmt_get_result($stmt)' instead of //'mysqli_stmt_bind_result($stmt,$email_count)';
$gender = $row["gender"];//Use this if you use '$result = mysqli_stmt_get_result($stmt)' instead of //'mysqli_stmt_bind_result($stmt,$email_count)';
//$age_range = $row["age_range"];//Use this if you use '$result = mysqli_stmt_get_result($stmt)' instead of //'mysqli_stmt_bind_result($stmt,$email_count)';
$marital_status = $row["marital_status"];//Use this if you use '$result = mysqli_stmt_get_result($stmt)' instead of //'mysqli_stmt_bind_result($stmt,$email_count)';
$working_status = $row["working_status"];//Use this if you use '$result = mysqli_stmt_get_result($stmt)' instead of //'mysqli_stmt_bind_result($stmt,$email_count)';
//$country = $row["country"];//Use this if you use '$result = mysqli_stmt_get_result($stmt)' instead of //'mysqli_stmt_bind_result($stmt,$email_count)';
<i> </i>//echo "Id: $id<br>";
<i> </i>//echo "Username: $username<br>";
<i> </i>echo "First Name: $first_name<br>";
<i> </i>echo "Middle Name: $middle_name<br>";
<i> </i>echo "Surname: $surname<br>";
<i> </i>echo "Gender: $gender<br>";
<i> </i>//echo "Age Range: $age_range<br>";
<i> </i>echo "Marital Status: $marital_status<br>";
<i> </i>echo "Working Status: $working_status<br>";
<i> </i>//echo "Country: $country<br>";
<i> </i>
<i> </i>//Close Statement.
<i> </i>mysqli_stmt_close($stmt);
<i> </i>//Free Result.
<i> </i>mysqli_free_result($result);//Is this line necessary if using mysqli_stmt_get_result() ?
<i> </i>//Close Connection.
<i> </i>mysqli_close($conn);
}
<i>
</i>mysqli_free_result($result);//Is this line necessary if using mysqli_stmt_get_result() ?
>@developer_web#1618992 $result = mysqli_stmt_get_result($stmt);
<i>
</i>$result = mysqli_stmt_get_result($stmt);
mysqli_free_result($result);
<i>
</i><?php
session_start();
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
if(!isset($_POST['email_account']) || !isset($_POST['email_service']))
{
$email_error = "<font color='red'>Input Email Address!</color>";
}
else
{
//Connect to Database. (DB_SERVER, BD_USERNAME, DB_PASSWORD, DB_NAME).
$conn = mysqli_connect("localhost","root","","powerpage");
$conn->set_charset('utf8mb4'); //Always set Charset.
<i> </i> if($conn === false)
<i> </i> {
<i> </i> die("ERROR: Connection Error!. " . mysqli_connect_error());
<i> </i> }
<i> </i> else
<i> </i> {
<i> </i> //Set Parameters.
<i> </i> $email = trim($_POST["email_account"]) . '@' . trim($_POST["email_service"]);
<i> </i> $_SESSION['email'] = trim($_POST["email_account"]) . '@' . trim($_POST["email_service"]);//If this fails on test then replace it with above line
<i> </i> echo "line 25 triggered: $email<br>";
<i> </i>
<i> </i> $sql_query = "SELECT COUNT(personal_email) FROM users WHERE personal_email = ?";
<i> </i> $stmt = mysqli_prepare($conn,$sql_query);
<i> </i> if($stmt == False)
<i> </i> {
<i> </i> //Close Connection.
<i> </i> mysqli_close($conn);
<i> </i> echo "Line 33<br>";//DELETE THIS
<i> </i> die("<pre>Mysqli Prepare Failed!n".mysqli_stmt_error($stmt)."n$sql_query</pre>");
<i> </i> }
<i> </i> else
<i> </i> {
<i> </i> mysqli_stmt_bind_param($stmt,'s',$email);
<i> </i>
<i> </i> if(!mysqli_stmt_execute($stmt))
<i> </i> {
<i> </i> //Close Connection.
<i> </i> mysqli_close($conn);
<i> </i> die("Could not mysqli_stmt_execute! Please try again later!");
<i> </i> }
<i> </i>
<i> </i> //$result = mysqli_stmt_bind_result($stmt,$email);
<i> </i> //$result = mysqli_stmt_store_result($stmt);
<i> </i> $result = mysqli_stmt_get_result($stmt);
<i> </i>
<i> </i> if(mysqli_fetch_array($result, MYSQLI_NUM)[0])
<i> </i> //($result->fetch_array()[0]) //DELETE THIS. THIS ALSO NOT RETYPED.
<i> </i> //if(mysqli_fetch_array($result, MYSQLI_NUM)) //WHY THIS NOT WORK UNLESS NUM ARRAY GIVEN ?
<i> </i> {
<i> </i> echo "Line 57 triggered: Function login() will trigger!<br>"; //DELETE THIS
<i> </i> $_SESSION['session_type'] = 'login';
<i> </i> login();
<i> </i>
<i> </i> }
<i> </i> else
<i> </i> {
<i> </i> echo "Line 61 triggered: Function register() will trigger!<br>"; //DELETE THIS
<i> </i> $_SESSION['session_type'] = 'register';
<i> </i> register();
<i> </i> }
<i> </i> }
<i> </i> }
<i> </i>}
}
function login()
{
if(!isset($_SESSION['session_type']) || $_SESSION['session_type'] != 'login')
{
//Close Statement.
mysqli_stmt_close($stmt);
//Close Connection.
mysqli_close($conn);
<i> </i> die("Could not check email! Please try again later!");
<i> </i>}
<i> </i>
<i> </i>//$email = trim($_POST["email_account"]) . '@' . trim($_POST["email_service"]);
<i> </i>$email = $_SESSION['email'];//If this fails on test then replace it with above line
<i> </i>
<i> </i>//Connect to Database. (DB_SERVER, BD_USERNAME, DB_PASSWORD, DB_NAME).
<i> </i>$conn = mysqli_connect("localhost","root","","powerpage");
<i> </i>
<i> </i>//Prepare a Select Statement.
<i> </i>$sql_query_3 = "SELECT id,username,first_name,middle_name,surname,gender,age_range FROM users WHERE personal_email = ?";
<i> </i>if(!$stmt_3 = mysqli_prepare($conn,$sql_query_3))
<i> </i>{
<i> </i> //Close Statement.
<i> </i> mysqli_stmt_close($stmt_3);
<i> </i> //Close Connection.
<i> </i> mysqli_close($conn);
<i> </i>
<i> </i> die("Could not check email! Please try again later!");
<i> </i>}
<i> </i>else
<i> </i>{
<i> </i> //Bind Variables to the Prepared Statement as parameters.
<i> </i> mysqli_stmt_bind_param($stmt_3,'s',$email);
<i> </i>
<i> </i> //Attempt to execute the Prepared Statement.
<i> </i> if(!mysqli_stmt_execute($stmt_3))
<i> </i> {
<i> </i> //Close Statement.
<i> </i> mysqli_stmt_close($stmt_3);
<i> </i> //Close Connection.
<i> </i> mysqli_close($conn);
<i> </i>
<i> </i> die("Could not check email! Please try again later!");
<i> </i> }
<i> </i> //mysqli_stmt_bind_result($stmt,$email);
<i> </i>
<i> </i> $result_3 = mysqli_stmt_get_result($stmt_3);
<i> </i>
<i> </i> //if(mysqli_fetch_array($result_3, MYSQLI_NUM))
<i> </i>
<i> </i> //Fetch result row as an associative array. Since the result set contains only one row, we don't need to use the 'While loop'.
<i> </i> //mysqli_stmt_fetch($stmt);//use this if you use 'mysqli_stmt_bind_result($stmt,$email).
<i> </i> if($row = mysqli_fetch_array($result_3,MYSQLI_ASSOC)) //Use this if you use '$result = mysqli_stmt_get_result($stmt)' instead of 'mysqli_stmt_bind_result($stmt,$email)'.
<i> </i> {
<i> </i> //Retrieve Values.
<i> </i> $id = $row["id"];//Use this if you use '$result = mysqli_stmt_get_result($stmt)' instead of //'mysqli_stmt_bind_result($stmt,$email_count)';
<i> </i> $username = $row["username"];//Use this if you use '$result = mysqli_stmt_get_result($stmt)' instead of //'mysqli_stmt_bind_result($stmt,$email_count)';
<i> </i> $first_name = $row["first_name"];//Use this if you use '$result = mysqli_stmt_get_result($stmt)' instead of //'mysqli_stmt_bind_result($stmt,$email_count)';
<i> </i> $middle_name = $row["middle_name"];//Use this if you use '$result = mysqli_stmt_get_result($stmt)' instead of //'mysqli_stmt_bind_result($stmt,$email_count)';
<i> </i> $surname = $row["surname"];//Use this if you use '$result = mysqli_stmt_get_result($stmt)' instead of //'mysqli_stmt_bind_result($stmt,$email_count)';
<i> </i> $gender = $row["gender"];//Use this if you use '$result = mysqli_stmt_get_result($stmt)' instead of //'mysqli_stmt_bind_result($stmt,$email_count)';
<i> </i> $age_range = $row["age_range"];//Use this if you use '$result = mysqli_stmt_get_result($stmt)' instead of //'mysqli_stmt_bind_result($stmt,$email_count)';
<i> </i>
<i> </i> echo "Id: $id<br>";
<i> </i> echo "Username: $username<br>";
<i> </i> echo "First Name: $first_name<br>";
<i> </i> echo "Middle Name: $middle_name<br>";
<i> </i> echo "Surname: $surname<br>";
<i> </i> echo "Gender: $gender<br>";
<i> </i> echo "Age Range: $age_range<br>";
<i> </i>
<i> </i> //Close Statement.
<i> </i> mysqli_stmt_close($stmt_3);
<i> </i> //Free Result.
<i> </i> mysqli_free_result($result_3);
<i> </i> //Close Connection.
<i> </i> mysqli_close($conn);
<i> </i> }
<i> </i>}
}
<i>
</i>$result_3 = mysqli_stmt_get_result($stmt_3);
mysqli_free_result($result_3);
<i>
</i>$sql_query = "SELECT username,first_name,middle_name,surname,gender,marital_status,working_status FROM users WHERE id = ?";
$stmt = mysqli_stmt_init($conn);
mysqli_stmt_prepare($stmt,$sql_query);
mysqli_stmt_bind_param($stmt,'i',$_SESSION["user_id"]);//DELETE THIS WHOLE LINE AS USING IT AS A TEST!
if($stmt == False)
{
//Close Connection.
mysqli_close($conn);
echo "Line 862<br>";//DELETE THIS
die("<pre>2. Mysqli Prepare Failed!n".mysqli_stmt_error($stmt)."n$sql_query</pre>");
}
//Attempt to Execute the Prepared Statement.
mysqli_stmt_execute($stmt);
if(!mysqli_stmt_execute($stmt))
{
//Close Connection.
mysqli_close($conn);
echo "Line 915<br>";//DELETE THIS
die("<pre>Statement Execution Failed!n".mysqli_stmt_error($stmt)."nmysqli_stmt_bind_param</pre>");
}
else
{
echo "<pre>Statement Execution Success!</pre>";
}
//mysqli_stmt_bind_result($stmt,$email);
$result = mysqli_stmt_get_result($stmt);
//if(mysqli_fetch_array($result_3, MYSQLI_NUM))
//Fetch result row as an associative array. Since the result set contains only one row, we don't need to use the 'While loop'.
//mysqli_stmt_fetch($stmt);//use this if you use 'mysqli_stmt_bind_result($stmt,$email).
if($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) //Use this if you use '$result = mysqli_stmt_get_result($stmt)' instead of 'mysqli_stmt_bind_result($stmt,$email)'.
{
//Retrieve Values.
//$id = $row["id"];//Use this if you use '$result = mysqli_stmt_get_result($stmt)' instead of //'mysqli_stmt_bind_result($stmt,$email_count)';
$username = $row["username"];//Use this if you use '$result = mysqli_stmt_get_result($stmt)' instead of //'mysqli_stmt_bind_result($stmt,$email_count)';
$first_name = $row["first_name"];//Use this if you use '$result = mysqli_stmt_get_result($stmt)' instead of //'mysqli_stmt_bind_result($stmt,$email_count)';
$middle_name = $row["middle_name"];//Use this if you use '$result = mysqli_stmt_get_result($stmt)' instead of //'mysqli_stmt_bind_result($stmt,$email_count)';
$surname = $row["surname"];//Use this if you use '$result = mysqli_stmt_get_result($stmt)' instead of //'mysqli_stmt_bind_result($stmt,$email_count)';
$gender = $row["gender"];//Use this if you use '$result = mysqli_stmt_get_result($stmt)' instead of //'mysqli_stmt_bind_result($stmt,$email_count)';
//$age_range = $row["age_range"];//Use this if you use '$result = mysqli_stmt_get_result($stmt)' instead of //'mysqli_stmt_bind_result($stmt,$email_count)';
$marital_status = $row["marital_status"];//Use this if you use '$result = mysqli_stmt_get_result($stmt)' instead of //'mysqli_stmt_bind_result($stmt,$email_count)';
$working_status = $row["working_status"];//Use this if you use '$result = mysqli_stmt_get_result($stmt)' instead of //'mysqli_stmt_bind_result($stmt,$email_count)';
//$country = $row["country"];//Use this if you use '$result = mysqli_stmt_get_result($stmt)' instead of //'mysqli_stmt_bind_result($stmt,$email_count)';
//echo "Id: $id<br>";
//echo "Username: $username<br>";
echo "First Name: $first_name<br>";
echo "Middle Name: $middle_name<br>";
echo "Surname: $surname<br>";
echo "Gender: $gender<br>";
//echo "Age Range: $age_range<br>";
echo "Marital Status: $marital_status<br>";
echo "Working Status: $working_status<br>";
//echo "Country: $country<br>";
//Close Statement.
mysqli_stmt_close($stmt);
//Free Result.
//mysqli_free_result($result);//Is this line necessary if using mysqli_stmt_get_result() ?
//Close Connection.
mysqli_close($conn);
}
//mail();
mysqli_prepare()
mysqli_init() / mysqli_stmt_prepare()
>@developer_web#1619191 I am confused about this mydsqli_stmt_init() unless someone intends to shed some light here.
<i>
</i>$sql_query = "SELECT username,first_name,middle_name,surname,gender,marital_status,working_status FROM users WHERE username = ? ORDER by id LIMIT $row_start,$row_end";//Offset,Max Result.
$stmt = mysqli_prepare($conn,$sql_query);
<i>
</i>$sql_query = "SELECT username,first_name,middle_name,surname,gender,marital_status,working_status FROM users WHERE username = ? ORDER by id LIMIT $row_start,$row_end";//Offset,Max Result.
$stmt = mysqli_stmt_init($conn);
mysqli_stmt_prepare($stmt,$sql_query);
0.1.9 — BETA 4.20