mysql php: How to bind result with the same recurring variable [duplicate] - php

I had a mysql query and I was converting it to mysqli(prepared statement) but I ran in to a problem which throws the below error,
Warning: mysqli_stmt::bind_result(): Number of bind variables doesn't match number of fields in prepared statement
Mysql code
$random_name_generated = vpb_generate_random_name().'.jpg'; //Generated name for uploaded files or images
if (move_uploaded_file($_FILES['file_to_upload']['tmp_name'], $final_uploads_location)) {
$check_empty_field = mysql_query("select * from `vpb_uploads` where `username` = '".mysql_real_escape_string(strip_tags($username))."' and `firstname` = '".mysql_real_escape_string("")."' and `lastname` = '".mysql_real_escape_string("")."'");
if (mysql_num_rows($check_empty_field) < 1) {
mysql_query("insert into `vpb_uploads` values('', '".mysql_real_escape_string($username)."', '', '', '".mysql_real_escape_string($random_name_generated)."', '', '', '', '', '".mysql_real_escape_string(date("d-m-Y"))."')");
$identity = "image_one";
} else {
$get_empty_field = mysql_fetch_array($check_empty_field);
$image_one = strip_tags($get_empty_field["image_one"]);
$image_two = strip_tags($get_empty_field["image_two"]);
$image_three = strip_tags($get_empty_field["image_three"]);
$image_four = strip_tags($get_empty_field["image_four"]);
$image_five = strip_tags($get_empty_field["image_five"]);
global $identity;
The below is what I tried even though it didn't work. I already knew it won't work but I wanted to try it myself before asking a question. And the error is coming from the $get_empty_field = $stmt->bind_result($stmt);
if (move_uploaded_file($_FILES['file_to_upload']['tmp_name'], $final_uploads_location)) {
$firstname = '""';
$lastname = '""';
$stmt = $mysqli->prepare("select * from `vpb_uploads` where `username` = ? and `firstname` = ? and `lastname` = ?");
$stmt->bind_param('sss', $username, $firstname, $lastname);
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows < 1) {
$date = 'date("d-m-Y")';
$image_2 = "''";
$image_3 = "''";
$image_4 = "''";
$image_5 = "''";
$stmt = $mysqli->prepare("insert into `vpb_uploads` (`username`, `firstname`, `lastname`, `image_one`, `image_two`, `image_three`, `image_four`, `image_five`, `date`) values(?,?,?,?,?,?,?,?,?)");
$stmt->bind_param('sssssssss', $username, $firstname, $lastname, $random_name_generated, $image_2, $image_3, $image_4, $image_5, $date);
$stmt->execute();
$stmt->close();
$identity = "image_one";
} else {
$get_empty_field = $stmt->bind_result($stmt);
$image_one = strip_tags($get_empty_field["image_one"]);
$image_two = strip_tags($get_empty_field["image_two"]);
$image_three = strip_tags($get_empty_field["image_three"]);
$image_four = strip_tags($get_empty_field["image_four"]);
$image_five = strip_tags($get_empty_field["image_five"]);
global $identity;

You need to change
$get_empty_field = $stmt->bind_result($stmt);
To
$get_empty_field = $stmt->bind_result($field1, $field2, $field3);
The number of $fieldx variables being equal to the number of fields that are selected. If you don't know how many there are, use this:
// Throw an exception if the result metadata cannot be retrieved
if (!$meta = $stmt->result_metadata())
{
throw new Exception($stmt->error);
}
// The data array
$data = array();
// The references array
$refs = array();
// Iterate over the fields and set a reference
while ($name = $meta->fetch_field())
{
$refs[] =& $data[$name->name];
}
// Free the metadata result
$meta->free_result();
// Throw an exception if the result cannot be bound
if (!call_user_func_array(array($stmt, 'bind_result'), $refs))
{
throw new Exception($stmt->error);
}
And then you access the result, after fetching, with $data['field'];

Related

Ajax JSON without correct content type

I am trying to get data from ajax response as json. But I am getting error response. I had this problem once, and i manage to solve it by adding header('Content-type: application/json'); to my php file. But if I do it now it kind of break my code, because i am including the file to another file that has a lot of html code in it and it loads the code in response. Can i get json response without that header somehow ?
$('#catlist').change(function() {
var opt = $(this).val();
console.log(opt);
$.ajax({
url: 'includes/processproducts.php',
type: 'get',
dataType: "json",
data: {opt: opt},
success: function(options){
console.log(options);
},
error: function(options){
console.log(options);
console.log("not working");
}
});
$('#brandlist').attr('disabled', false);
});
<?php
if(!isset($_SESSION)){
session_start();
}
//
include '../../core/initialize.php';
/*
include 'includes/head.php';
include 'includes/navigation.php';
include 'includes/functions.php';
*/
$sql = "SELECT * FROM product";
$result = $db->query($sql);
if(isset($_REQUEST['submitbtn'])){
if(isset($_REQUEST['edit'])){
?>
<script> alert("EDIT") </script>
<?php
if(!empty($_POST['handleName'])){
$nameEdVar = $_POST['handleName'];
} else {
$nameEdVar = NULL;
}
if(!empty($_POST['handleCat'])){
$catEdVar = $_POST['handleCat'];
} else {
$catEdVar = NULL;
}
if(!empty($_POST['handleBrand'])){
$brandEdVar = $_POST['handleBrand'];
} else {
$brandEdVar = NULL;
}
if(!empty($_POST['handleDesc'])){
$descEdVar = $_POST['handleDesc'];
} else {
$descEdVar = NULL;
}
if(!($_FILES['image']['size'] == 0)){
$path = processImg($_REQUEST['edit']);
} else {
$path = NULL;
}
//$path = NULL;
$edit_id = (int)$_REQUEST['edit'];
$sqled = "UPDATE product SET name='$nameEdVar', cat_id='$catEdVar', brand_id='$brandEdVar', image='$path', description='$descEdVar' WHERE product.id_P = $edit_id";
$db->query($sqled);
//header('Location: products.php');
}else{
if(($_POST['handleName'] =='') || ($_POST['handleCat'] == '')
|| ($_POST['handleBrand'] == '') || ($_POST['handleDesc'] == '')
|| ($_FILES['image']['size'] == 0)){ ?>
<script> alert('ADD časť niečo nevyplnene'); </script>
<?php
}else{
?> <script> alert('ADD časť všetko vyplnene'); </script>
<?php
$nameVar = $_POST['handleName'];
$catVar = $_POST['handleCat'];
$brandVar = $_POST['handleBrand'];
$descVar = $_POST['handleDesc'];
$qVar = 1;
//$path = "https://localhost/shop/imgs/31.jpg";
$sql = "SELECT MAX(id_p) FROM product";
$stmt = $db->prepare($sql);
$stmt->execute();
$stmt->bind_result($maxid);
$stmt->fetch();
$path = processImg(++$maxid);
$stmt->close();
$sql = "INSERT INTO product (id_p, name, cat_id, brand_id, image, description, quantity) VALUES(?, ?, ?, ?, ?, ?, ?)";
$stmt = $db->prepare($sql);
$stmt->bind_param('sssssss', $nullvar = NULL, $nameVar, $catVar, $brandVar, $path, $descVar, $qVar);
$stmt->execute();
$stmt->close();
//header('Location: products.php');
}
}
}
//Delete Product
if(isset($_GET['delete']) && !empty($_GET['delete'])){
$delete_id = (int)$_GET['delete'];
$sqldel = "DELETE FROM product WHERE product.id_p = $delete_id";
$stmt = $db->prepare($sqldel);
$stmt->execute();
$stmt->close();
header('Location: products.php');
}
if(isset($_REQUEST['edit'])){
echo "
<script>
$(document).ready(function() {
$('#addBox').modal('show');
})
</script>";
}
$sqlcat = "SELECT * FROM category";
$stmtcat = $db->prepare($sqlcat);
$stmtcat->execute();
$stmtcat->bind_result($cat_id, $type);
$stmtcat->store_result();
$cat = array();
while($stmtcat->fetch()){
$cat[$cat_id] = array();
$cat[$cat_id]["type"] = $type;
}
$stmtcat->close();
$sqlbrand = "SELECT * FROM brand";
$stmtbrand = $db->prepare($sqlbrand);
$stmtbrand->execute();
$stmtbrand->bind_result($brand_id, $name);
$stmtbrand->store_result();
$brand = array();
while($stmtbrand->fetch()){
$brand[$brand_id] = array();
$brand[$brand_id]["name"] = $name;
}
$stmtbrand->close();
if(isset($_REQUEST['submitcategory'])){
$sql = "INSERT INTO category (cat_id, type) VALUES(?, ?) ON DUPLICATE KEY UPDATE cat_id = cat_id";
$stmt = $db->prepare($sql);
$stmt->bind_param('is', $cat_id = NULL, $_REQUEST['category']);
$stmt->execute();
$stmt->close();
foreach ($_REQUEST as $key=>$value){if($key != "category" && $value != "Submit"){
if($value == ""){
exit();
}
var_dump($value);
$sql = "INSERT INTO brand(brand_id, NAME) VALUES(?, ?) ON DUPLICATE KEY UPDATE brand_id = brand_id";
$stmt = $db->prepare($sql);
$stmt->bind_param('is', $brand_id = NULL, $value);
$stmt->execute();
$stmt->close();
$sql = "SELECT brand_id, cat_id FROM brand,category WHERE brand.name= ? AND category.type = ?";
$stmt = $db->prepare($sql);
$stmt->bind_param('ss', $value, $_REQUEST['category']);
$stmt->execute();
$stmt->bind_result($brand_id, $cat_id);
$stmt->fetch();
$stmt->close();
$sql = "INSERT INTO brand_category(brand_id, cat_id) VALUES(?, ?) ON DUPLICATE KEY UPDATE brand_id = brand_id";
$stmt = $db->prepare($sql);
$stmt->bind_param('ii', $brand_id, $cat_id );
$stmt->execute();
$stmt->close();
}
}
}elseif(isset($_REQUEST['submitbrand'])){
$sql = "INSERT INTO brand (brand_id, name) VALUES(?, ?) ON DUPLICATE KEY UPDATE brand_id = brand_id";
$stmt = $db->prepare($sql);
$stmt->bind_param('is', $brand_id = NULL, $_REQUEST['brand']);
$stmt->execute();
$stmt->close();
foreach ($_REQUEST as $key=>$value){if($key != "brand" && $value != "Submit"){
if($value == ""){
exit();
}
$sql = "INSERT INTO category (cat_id, type) VALUES(?, ?) ON DUPLICATE KEY UPDATE cat_id = cat_id";
$stmt = $db->prepare($sql);
$stmt->bind_param('is', $cat_id = NULL, $value);
$stmt->execute();
$stmt->close();
$sql = "SELECT brand_id, cat_id FROM brand,category WHERE brand.name= ? AND category.type = ?";
$stmt = $db->prepare($sql);
$stmt->bind_param('ss', $_REQUEST['brand'], $value);
$stmt->execute();
$stmt->bind_result($brand_id, $cat_id);
$stmt->fetch();
$stmt->close();
$sql = "INSERT INTO brand_category(brand_id, cat_id) VALUES(?, ?) ON DUPLICATE KEY UPDATE brand_id = brand_id";
$stmt = $db->prepare($sql);
$stmt->bind_param('ii', $brand_id, $cat_id );
$stmt->execute();
$stmt->close();
}
}
}
$sql = "SELECT brand.name, brand.brand_id FROM brand JOIN brand_category ON brand_category.brand_id = brand.brand_id WHERE brand_category.cat_id = ?";
$stmt = $db->prepare($sql);
$stmt->bind_param('s', $_REQUEST['opt']);
$stmt->execute();
$stmt->bind_result($name, $brand_id);
$stmt->store_result();
$options = array();
echo $name;
echo $brand_id;
while($stmt->fetch()){
$options[$brand_id] = array();
$options[$brand_id]["name"] = $name;
}
//header('Content-type: application/json');
echo json_encode($options);
$stmt->close();
?>
Remove the following line from your ajax call:
dataType: "json"
and change the line
success: function(options){
var data = JSON.parse(options); // convert the json to js array and use it
}
Try this, it will work
Solved it by creating file just for ajax call.

Database table is not updating

Hi I'm trying to update my tbl_jadwal, it said success, but database is not updated, anybody can please help me finding where the problem is ? Thankyou
<?php
if (isset($_GET['id'])) {
$ID = $_GET['id'];
} else {
$ID = "";
}
// create array variable to store category data
$category_data = array();
$sql_query = "SELECT Category_ID, Category_name
FROM tbl_category
ORDER BY Category_ID ASC";
$stmt_category = $connect->stmt_init();
if ($stmt_category->prepare($sql_query)) {
// Execute query
$stmt_category->execute();
// store result
$stmt_category->store_result();
$stmt_category->bind_result($category_data['Category_ID'],
$category_data['Category_name']
);
}
$sql_query = "SELECT Menu_image FROM tbl_jadwal WHERE Menu_ID = ?";
$stmt = $connect->stmt_init();
if ($stmt->prepare($sql_query)) {
// Bind your variables to replace the ?s
$stmt->bind_param('s', $ID);
// Execute query
$stmt->execute();
// store result
$stmt->store_result();
$stmt->bind_result($previous_menu_image);
$stmt->fetch();
$stmt->close();
}
$stmt = $connect->stmt_init();
if ($stmt->prepare($sql_query)) {
// Execute query
$stmt->execute();
// store result
$stmt->store_result();
$stmt->fetch();
$stmt->close();
}
if (isset($_POST['btnEdit'])) {
$nama_lokasi = $_POST['nama_lokasi'];
$category_ID = $_POST['category_ID'];
$longitude = $_POST['longitude'];
$latitude = $_POST['latitude'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$description = $_POST['description'];
// get image info
$menu_image = $_FILES['menu_image']['name'];
$image_error = $_FILES['menu_image']['error'];
$image_type = $_FILES['menu_image']['type'];
// create array variable to handle error
$error = array();
// updating all data
$sql_query = "UPDATE tbl_jadwal
SET Nama_Lokasi = ? , Category_ID = ?, Latitude = ?, Longitude = ?, Phone = ?, Email = ?, Menu_image = ?, Description = ?
WHERE Menu_ID = ?";
$upload_image = 'upload/images/' . $menu_image;
$stmt = $connect->stmt_init();
if ($stmt->prepare($sql_query)) {
// Bind your variables to replace the ?s
$stmt->bind_param('ssssssss',
$nama_lokasi,
$category_ID,
$longitude,
$latitude,
$phone,
$email,
$upload_image,
$description,
$ID);
// Execute query
$stmt->execute();
// store result
$update_result = $stmt->store_result();
$stmt->close();
}
} else {
updating all data except image file
$sql_query = "UPDATE tbl_jadwal
SET Nama_Lokasi = ? , Category_ID = ?,
Longitude = ?, Latitude = ?, Phone = ?, Email = ?, Description = ?
WHERE Menu_ID = ?";
$stmt = $connect->stmt_init();
if ($stmt->prepare($sql_query)) {
// Bind your variables to replace the ?s
$stmt->bind_param('sssssss',
$nama_lokasi,
$category_ID,
$longitude,
$latitude,
$phone,
$email,
$description,
$ID);
// Execute query
$stmt->execute();
// store result
$update_result = $stmt->store_result();
$stmt->close();
}
}
check update result
if ($update_result) {
$error['update_data'] = " <span class='label label-primary'>Success update</span>";
} else {
$error['update_data'] = " <span class='label label-danger'>failed update</span>";
}
This my database structure
replace bind_param() with bindParam(':data', $data);
or try $stmt->execute(array(':data' => $data))
Hi Just write the simple query firstly and add EXPLAIN before it.
For example:
EXPLAIN update table set name='test' where id=1;
This statement will show all the possible error. In this way you will be able to resolve the problem.

PHP : User update method inside insert Method

I have a little problem when I try to validating the condition in my insert method.
This is what I want :
if data not exist, do insert data
if data exist in 'isActive' is 'true', send error 'data already exist'
if data exist in 'isActive' is 'false', run update method
But it seems my method can not run smoothly.
this is my insert method :
public function InsertUserCard(UserCard $uc)
{
//get kartu
$user_card = $this->GetUserCardByCardIdByUserId($uc);
if(!$user_card)
{
$stmt = $this->conn->prepare("INSERT INTO ".$this->table_name."
(user_card_id, user_id, card_id, card_number, barcode, barcode_format, created_at, updated_at)
VALUES('', ?, ?, ?, ?, ?, ?, ?)");
if ($stmt == FALSE)
{
die($this->conn->error);
}
else
{
$user_id = NULL;
$card_id = NULL;
$card_number = NULL;
$barcode = NULL;
$barcode_format = NULL;
$created_at = NULL;
$updated_at = NULL;
$stmt->bind_param("iisssss", $user_id, $card_id, $card_number, $barcode, $barcode_format, $created_at, $updated_at);
$user_id = $uc->getUserId();
$card_id = $uc->getCardId();
$card_number = $uc->getCardNumber();
$barcode = $uc->getBarcode();
$barcode_format = $uc->getBarcodeFormat();
$created_at = $uc->getCreatedAt();
$updated_at = $uc->getUpdatedAt();
$stmt->execute();
$result = $this->conn->insert_id;
$stmt->close();
}
// Check for successful insertion
if ($result)
{
// User card successfully inserted
return $result;
}
else
{
// Failed to insert user card
return USER_CARD_INSERT_FAILED;
}
}
else if($user_card->getisActive() == 'true')
{
return USER_CARD_ALREADY_EXISTED;
}
else {
// this is where I try to run the update method
$uc->setUserCardId($user_card->getUserCardId());
$this->UpdateUserCard($uc);
return $uc->getUserCardId();
}
}
in last condition, I try to run the update method if the data is exists but 'isActive' value is 'false'
This is my update method :
public function UpdateUserCard(UserCard $uc)
{
$stmt = $this->conn->prepare("UPDATE ".$this->table_name."
SET user_id = ?,
card_id = ?,
card_number = ?,
barcode = ?,
barcode_format = ?,
isActive = ?,
updated_at = ?
WHERE user_card_id = ?
AND user_id = ?");
if($stmt == FALSE)
{
die($this->conn->error);
}
else
{
$user_id = NULL;
$card_id = NULL;
$card_number = NULL;
$barcode = NULL;
$format = NULL;
$isActive = NULL;
$updated_at = NULL;
$user_card_id = NULL;
$stmt->bind_param("iisssssii", $user_id,
$card_id,
$card_number,
$barcode,
$format,
$isActive,
$updated_at,
$user_card_id,
$user_id);
$user_id = $uc->getUserId();
$card_id = $uc->getCardId();
$card_number = $uc->getCardNumber();
$barcode = $uc->getBarcode();
$format = $uc->getBarcodeFormat();
$isActive = $uc->getisActive();
$updated_at = $uc->getUpdatedAt();
$user_card_id = $uc->getUserCardId();
$stmt->execute();
$num_affected_rows = $stmt->affected_rows;
$stmt->close();
return $num_affected_rows > 0;
}
// Check for successful insertion
if ($stmt->execute())
{
// User card successfully inserted
return USER_CARD_INSERTED_SUCCESSFULLY;
}
else
{
// Failed to insert user card
return USER_CARD_INSERT_FAILED;
}
}
and the last is my GetUserCardByCardIdByUserId Method :
private function GetUserCardByCardIdByUserId(UserCard $card)
{
$stmt = $this->conn->prepare("SELECT user_card_id, user_id, card_id, card_number, barcode, barcode_format, isActive, created_at, updated_at
FROM ". $this->table_name ."
WHERE card_id = ? AND user_id = ?");
if ($stmt == FALSE)
{
die($this->conn->error);
}
else
{
$card_id = NULL;
$user_id = NULL;
$stmt->bind_param("ii", $card_id, $user_id);
$card_id = $card->getCardId();
$user_id = $card->getUserId();
$stmt->execute();
$stmt->bind_result($user_card_id,
$user_id,
$card_id,
$card_number,
$barcode,
$barcode_format,
$isActive,
$created_at,
$updated_at);
$stmt->store_result();
$num_rows = $stmt->num_rows;
if($num_rows != 1)
return NULL;
else {
$card = new UserCard();
$card->setUserCardId($user_card_id);
$card->setUserId($user_id);
$card->setCardId($card_id);
$card->setBarcode($barcode);
$card->setBarcodeFormat($barcode_format);
$card->setisActive($isActive);
$card->setCreatedAt($created_at);
$card->setUpdatedAt($updated_at);
$stmt->close();
return $card;
}
}
}
please check my method if I write wrong structure of code.
thanks :)

bind_result failed

I actually tried mysqli->fetch_array(); for fetching array but that couldn't work so I used bind_result(); and it throws the below error
Warning: mysqli_stmt::bind_result(): Number of bind variables doesn't match number of fields in prepared statement
So I did a error check on bind_result and what I got was bind_result() failed: so what I am really doing wrong?
$wtf = "";
$hello = "";
$check_empty_field = $mysqli - > prepare("select `username`, `firstname`, `lastname` from `vpb_uploads` where `username` = ? and `firstname` = ? and `lastname` = ?");
$check_empty_field - > bind_param('sss', $username, $wtf, $hello);
$check_empty_field - > execute();
$check_empty_field - > store_result();
if($check_empty_field - > num_rows < 1) {
$date = date("d-m-Y");
$i2 = '';
$i3 = '';
$i4 = '';
$i5 = '';
$insert = $mysqli - > prepare("insert into `vpb_uploads` (`username`, `firstname`, `lastname`, image_one, image_two, image_three, image_four, image_five, date)values(?,?,?,?,?,?,?,?,?)");
$insert - > bind_param('sssssssss', $username, $wtf, $hello, $random_name_generated, $i2, $i3, $i4, $i5, $date);
$insert - > execute();
$identity = "image_one";
} else {
$get_empty_field = $check_empty_field - > bind_result($username, $wtf, $hello);
if(false === $get_empty_field) {
die('bind_result() failed: '.htmlspecialchars($mysqli - > error));
}
$image_one = strip_tags($get_empty_field["image_one"]);
$image_two = strip_tags($get_empty_field["image_two"]);
$image_three = strip_tags($get_empty_field["image_three"]);
$image_four = strip_tags($get_empty_field["image_four"]);
$image_five = strip_tags($get_empty_field["image_five"]);
global $identity;
}
This error is thrown because you have not set each of the column names which are returned from the SQL query to a variable.
Example:
If you have 6 columns in your table and you have selected all (*) then you will have to:
$stmt->bind_result($Col_1,$Col_2,$Col_3,$Col_4,$Col_5,$Col_6);
If you have:
$stmt->bind_result($Col_1,$Col_2,$Col_3,$Col_4,$Col_5);
an error will be thrown.
$check_empty_field->data_seek(0);
$get_empty_field = $check_empty_field - > bind_result($username, $wtf, $hello);

Full-text search using mysqli prepared statement errors with unmatched bind variables [duplicate]

I had a mysql query and I was converting it to mysqli(prepared statement) but I ran in to a problem which throws the below error,
Warning: mysqli_stmt::bind_result(): Number of bind variables doesn't match number of fields in prepared statement
Mysql code
$random_name_generated = vpb_generate_random_name().'.jpg'; //Generated name for uploaded files or images
if (move_uploaded_file($_FILES['file_to_upload']['tmp_name'], $final_uploads_location)) {
$check_empty_field = mysql_query("select * from `vpb_uploads` where `username` = '".mysql_real_escape_string(strip_tags($username))."' and `firstname` = '".mysql_real_escape_string("")."' and `lastname` = '".mysql_real_escape_string("")."'");
if (mysql_num_rows($check_empty_field) < 1) {
mysql_query("insert into `vpb_uploads` values('', '".mysql_real_escape_string($username)."', '', '', '".mysql_real_escape_string($random_name_generated)."', '', '', '', '', '".mysql_real_escape_string(date("d-m-Y"))."')");
$identity = "image_one";
} else {
$get_empty_field = mysql_fetch_array($check_empty_field);
$image_one = strip_tags($get_empty_field["image_one"]);
$image_two = strip_tags($get_empty_field["image_two"]);
$image_three = strip_tags($get_empty_field["image_three"]);
$image_four = strip_tags($get_empty_field["image_four"]);
$image_five = strip_tags($get_empty_field["image_five"]);
global $identity;
The below is what I tried even though it didn't work. I already knew it won't work but I wanted to try it myself before asking a question. And the error is coming from the $get_empty_field = $stmt->bind_result($stmt);
if (move_uploaded_file($_FILES['file_to_upload']['tmp_name'], $final_uploads_location)) {
$firstname = '""';
$lastname = '""';
$stmt = $mysqli->prepare("select * from `vpb_uploads` where `username` = ? and `firstname` = ? and `lastname` = ?");
$stmt->bind_param('sss', $username, $firstname, $lastname);
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows < 1) {
$date = 'date("d-m-Y")';
$image_2 = "''";
$image_3 = "''";
$image_4 = "''";
$image_5 = "''";
$stmt = $mysqli->prepare("insert into `vpb_uploads` (`username`, `firstname`, `lastname`, `image_one`, `image_two`, `image_three`, `image_four`, `image_five`, `date`) values(?,?,?,?,?,?,?,?,?)");
$stmt->bind_param('sssssssss', $username, $firstname, $lastname, $random_name_generated, $image_2, $image_3, $image_4, $image_5, $date);
$stmt->execute();
$stmt->close();
$identity = "image_one";
} else {
$get_empty_field = $stmt->bind_result($stmt);
$image_one = strip_tags($get_empty_field["image_one"]);
$image_two = strip_tags($get_empty_field["image_two"]);
$image_three = strip_tags($get_empty_field["image_three"]);
$image_four = strip_tags($get_empty_field["image_four"]);
$image_five = strip_tags($get_empty_field["image_five"]);
global $identity;
You need to change
$get_empty_field = $stmt->bind_result($stmt);
To
$get_empty_field = $stmt->bind_result($field1, $field2, $field3);
The number of $fieldx variables being equal to the number of fields that are selected. If you don't know how many there are, use this:
// Throw an exception if the result metadata cannot be retrieved
if (!$meta = $stmt->result_metadata())
{
throw new Exception($stmt->error);
}
// The data array
$data = array();
// The references array
$refs = array();
// Iterate over the fields and set a reference
while ($name = $meta->fetch_field())
{
$refs[] =& $data[$name->name];
}
// Free the metadata result
$meta->free_result();
// Throw an exception if the result cannot be bound
if (!call_user_func_array(array($stmt, 'bind_result'), $refs))
{
throw new Exception($stmt->error);
}
And then you access the result, after fetching, with $data['field'];

Categories