PHP MYSQLI Call to a member function bind_param() [duplicate] - php

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 6 years ago.
my code is
$query1 = "SELECT `user_id` FROM `it_user` WHERE (user_email_address = ? AND user_mobile_number_verified = NULL)";
$stmt1 = $mysqli->prepare($query1);
$stmt1->bind_param("s",$email);
$stmt1->execute();
if ($stmt1->affected_rows == 1) {
//set registration flag to 1 stating that the users mobile number is already verified
echo '<meta http-equiv="refresh" content="0; URL=\'../signin/\'"/>';
}
else
{
$registration_flag = 2;
//redirect user to error page
//echo '<meta http-equiv="refresh" content="0; URL=\'../error/\'"/>';
}
i am getting this error ::
Call to a member function bind_param() on a non-object in ***** on line 62
where as my email variable is working corrrect and also the query.

Use NULL safe operator and write code as below:-
$mobile = NULL; // NOTE: no quotes - using php NULL
$query1 = "SELECT `user_id` FROM `it_user` WHERE user_email_address = ? AND user_mobile_number_verified <=> ?";
$stmt1 = $mysqli->prepare($query1);
$stmt1->bind_param("s",$email);
$stmt1->bind_param($mobile);
$stmt1->execute();
Hope it will help you :-)

Related

php database warning bool [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Why does this PDO statement silently fail?
(2 answers)
Closed 11 months ago.
This post was edited and submitted for review 11 months ago and failed to reopen the post:
Duplicate This question has been answered, is not unique, and doesn’t differentiate itself from another question.
I have this warning, how do I fix it?
Notice: Trying to access array offset on value of type bool in C:\Program Files\Ampps\www\NodeMCU\read tag user data.php on line 17
Code
require 'database.php';
$id = null;
if ( !empty($_GET['id'])) {
$id = $_REQUEST['id'];
}
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT * FROM tb_nodemcu where id = ?";
$q = $pdo->prepare($sql);
$q->execute(array($id));
$data = $q->fetch(PDO::FETCH_ASSOC);
Database::disconnect();
$msg = null;
if (null==$data['name']) {
$msg = "The ID of your Card / KeyChain is not registered !!!";
$data['id']=$id;
$data['name']="--------";
$data['gender']="--------";
$data['email']="--------";
$data['mobile']="--------";
} else {
$msg = null;
}

mysqli_query() expects parameter 1 to be mysqli, boolean given [duplicate]

This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
How do I display a MySQL error in PHP for a long query that depends on the user input? [duplicate]
(6 answers)
Reference - What does this error mean in PHP?
(38 answers)
Closed 4 years ago.
Hey can you guys help me with this because I can't figure it out here are the errors.
[04-Nov-2018 15:21:52 UTC] PHP Warning: mysqli_connect(): (HY000/1045):
Access denied for user 'freeload_retain'#'vps28004.inmotionhosting.com'
(using password: NO) in /home/freeloadboard/public_html/insert.php on
line 7
[04-Nov-2018 15:21:52 UTC] PHP Warning: mysqli_select_db() expects
parameter 1
to be mysqli, boolean given in /home/freeloadboard/public_html/insert.php
on line 21
[04-Nov-2018 15:21:52 UTC] PHP Warning: mysqli_query() expects parameter
1 to be mysqli, boolean given in
/home/freeloadboard/public_html/insert.php on line 45
Program:
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$con = mysqli_connect('##.##.###.##','freeload_retain','');
if(!$con)
{
echo "Not Connected to Server. ";
}
if(!mysqli_select_db($con, 'freeload_retain'))
{
echo "Database Not Selected. ";
}
$Companyname = $_POST['companyname'];
$Username = $_POST['username'];
$Password = $_POST['password'];
$Email = $_POST['email'];
$sql = "INSERT INTO clients (companyname, username, password, email)
VALUES ('$Companyname', '$Username', '$Password', '$Email')";
if(!mysqli_query($con, $sql))
{
echo "Not Inserted. ";
}
else
{
echo "Inserted. ";
}
?>
Hope you guys find out the answer soon!
Also I'm reusing this question because I can't wait another day to make another question but thanks for helping me out!
To answer your question: It's not working because you're wrapping the column names in brackets, remove these and it should work. You also have a typo. ($comapnyname = $_POST['companyname'];), should be $companyname.
However, there's a few other, bigger issues with your code. You're using the mysql functions, which are deprecated and completely removed from PHP7.
Next to that, you should use prepared statements and bind_param to prevent SQL injections, escaping strings will not accomplish this.
This what it would look like using prepared statements.
// ... Set your database connection variables
/*
* Create the database connection, notice the
* usage of mysqli instead of mysql
*/
$connect = new mysqli($host, $user, $password, $database);
/*
* The query, notice the usage of a question mark
* on the place of the variables to be inserted
*/
$sql = "INSERT INTO client (cname, tname, pname, ename) VALUES (?, ?, ?, ?)";
// Prepare the query using $connect->prepare()
$stmt = $connect->prepare($sql);
// Check if the query was prepared
if(!$stmt) {
// ... Handle your error
}
// Get $_POST variables
$companyname = $_POST['companyname'];
$username = $_POST['username'];
$password = $_POST['password'];
$email = $_POST['email'];
if(!$stmt->bind_param('ssss', $companyname, $username, $password, $email)) {
// ... Handle your error
}
if(!$stmt->execute()) {
// ... Handle your error
} else {
echo 'Record inserted.';
}
It also seems that you're inserting the passwords into your database as clear-text, this is a big issue. You should hash them. Write two functions, one to hash the password and one to verify it when users log in.
The first function will return the password hash and the second one will return TRUE or FALSE if the password is correct or incorrect.
function hashPassword($password) {
return password_hash($password, PASSWORD_DEFAULT);
}
function verifyPassword($password, $hash) {
return password_verify($password, $hash);
}

Mysqli_close() expects parameter 1 to be mysqli [duplicate]

This question already has answers here:
php warning: mysqli_close() expects parameter 1 to be mysqli
(2 answers)
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
(3 answers)
Closed 5 years ago.
I am working on android app project and in this project, i need to connect my android app with PHP and MySQL everything is fine but I am getting an error in my user registration .php file error like this
Warning: mysqli_close() expects parameter 1 to be myself, null given
in C:\xampp\htdocs\panel\UserRegistration.php on line 35
I have tried many things but the error is still there.
Here is what I have tried:
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
include 'DatabaseConfig.php';
$con = mysqli_connect($HostName,$HostUser,$HostPass,$DatabaseName);
$id_name = $_POST['id'];
$U_name = $_POST['username'];
$password = $_POST['password'];
$cnic = $_POST['cnic'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$CheckSQL = "SELECT * FROM Users WHERE email='$email'";
$check = mysqli_fetch_array(mysqli_query($con,$CheckSQL));
if(isset($check)){
echo 'Email Already Exist';
}
else{
$Sql_Query = "INSERT INTO Users (id,username,email,password_cnic,phone) values ('$id','$U_name','$email','$password','$cnic','$phone')";
if(mysqli_query($con,$Sql_Query))
{
echo 'Registration Successfully';
}
}
?>
https://secure.php.net/manual/fr/mysqli.close.php
You must do : mysqli_close($con);
:)
Pass Connection object in mysqli_close() function.
Like :
mysqli_close($con);

Strict standards: Only variables should be passed by reference on line 14 [duplicate]

This question already has answers here:
PDO pass by reference notice?
(4 answers)
Closed 5 years ago.
I am getting the error Strict standards: Only variables should be passed by reference on line 14
<?php
require 'database.php';
$messaqage = '';
if(!empty($_POST['email']) && !empty($_POST['password'])):
// Enter the new user in the database
$sql = "INSERT INTO users (email, password) VALUES (:email, :password)";
$stmt = $conn->prepare($sql);
$stmt->bindParam(':email',$_POST['email']);
$stmt->bindParam(':password',password_hash($_POST['password'],PASSWORD_BCRYPT));
if( $stmt->execute() ):
$message = 'Successfully created new user';
else:
$message = 'Sorry there must be an issue creating your account';
endif;
endif;
?>
I am stuck in resolving this error. I am not able to debug the issue.
Thanks in advance
bindParam() second argument is a reference. You pass a function result. change like this .
$password = password_hash($_POST['password'],PASSWORD_BCRYPT);
$stmt->bindParam(':password',$password);

PHP Notice: Undefined offset: 0 [duplicate]

This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 8 years ago.
I am trying redirect user when login successfully but I am getting error on entering wrong username and password and also redirection not working. If I insert valid username and password works great.
Error:
Notice: Undefined offset: 0 in /var/sites/l/example.com/public_html/demo/sitename/application/models/loginmodel.php on line 70
Notice: Trying to get property of non-object in /var/sites/l/example.com/public_html/demo/sitename/application/models/loginmodel.php on line 70
Warning: Cannot modify header information - headers already sent by (output started at /var/sites/l/example.com/public_html/demo/sitename/application/models/loginmodel.php:70) in /var/sites/l/example.com/public_html/demo/sitename/application/models/loginmodel.php on line 82
My Code:
session_start();
$username = strip_tags($username);
$password = strip_tags($password);
$sql = "SELECT id FROM users WHERE name='$username' and password='$password'";
$query = $this->db->prepare($sql);
$query->execute();
$records = $query->fetchAll();
$ck_userID = $records[0]->id;
//$active=$row['active'];
// echo "<pre>";
// print_r($ck_userID);
// echo "</pre>";
// die;
if ( count($ck_userID) > 0 ){
$_SESSION['login_user']=$username;
header('location: ' . URL . 'admin');
}else{
header('location: ' . URL . 'login?invalid');
}
Change
$ck_userID = $records[0]->id;
to
$ck_userID = isset($records[0]) && is_object($records[0]) ? $records[0]->id : null;
Explanation:
You have to check whethe $records[0] does exist and you should check whether it's an object (so it's not false or null).
Also care for SQL injection. You are using some kind of prepare method, but you don't prepare anything.
Another bad practice is header sending location change but no exit following.
Header start with a capital letter, location: -> Location:.
change
$records = $query->fetchAll();
$ck_userID = $records[0]->id;
if ( count($ck_userID) > 0 ){
to
$records = $query->fetchAll();
if ( count($records) > 0 ){
NOTE: your code is vulnerable to sql injections

Categories