PHP : User update method inside insert Method - php

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 :)

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.

Fatal Error in my php script

I am using Opencart v2.2.0 and I am having problem with my php script. The line in question is:
$stmt2->bind_param('sssssisi', $name, $description, $meta_description, $meta_keyword, $tag, $modified_by, $product_id, $language_id);
The whole function including the line above is as follows:
function restoreBackup()
{
global $mysqli;
$i = 0;
$getpic = "SELECT * FROM oc_product_backup LIMIT 0, 100000";
$backup = $mysqli->query($getpic);
$mysqli->autocommit(FALSE);
$updateproduct_sql = "UPDATE oc_product SET image = ?, ean = ?, model = ?, status = ?, price_sync = ?, modified_by = ?, date_modified = ? WHERE product_id= ?";
$updatedescription_sql = "UPDATE oc_product_description SET name = ?, description = ?, meta_description = ?, meta_keyword = ?, tag = ?, modified_by = ? WHERE product_id = ? AND language_id = ?";
$stmt = $mysqli->prepare($updateproduct_sql);
$stmt->bind_param('sssiiiss', $image, $ean, $model, $status, $price_sync, $modified_by, $date_modified, $product_id);
$stmt2 = $mysqli->prepare($updatedescription_sql);
$stmt2->bind_param('sssssisi', $name, $description, $meta_description, $meta_keyword, $tag, $modified_by, $product_id, $language_id);
while($row = $backup->fetch_array(MYSQLI_ASSOC))
{
//$name = removeslashes($row['name']);
$name = $row['name'];
//$description = removeslashes($row['description']);
$description = $row['description'];
$meta_description = $row['meta_description'];
$meta_keyword = $row['meta_keyword'];
$tag = $row['tag'];
$product_id = $row['product_id'];
$modified_by = $row['modified_by'];
$language_id = $row['language_id'];
if($row['language_id'] == 4)
{
$image = $row['image'];
$ean = $row['ean'];
$name = $row['name'];
$model = $row['model'];
$status = $row['status'];
$price_sync = $row['price_sync'];
$date_modified = $row['date_modified'];
if(!$stmt->execute())
return false;
}
if(!$stmt2->execute())
return false;
$i++;
if(($i % 500) === 0) $mysqli->commit();
}
$mysqli->commit();
$backup->close();
return true;
}
function removeslashes($string)
{
$string=implode("",explode("\\",$string));
return stripslashes(trim($string));
}
The error I get is
Fatal error: Call to a member function bind_param() on boolean in
Any suggestions, please? I do not see what am I doing wrong. Thank you all in advance.
The prepare() method can return false and you should check for that. As for why it returns false, perhaps the table name or column names (in SELECT, UPDATE or WHERE clause) are not correct?
Also, consider use of something like $query->error_list to examine errors that occurred parsing the SQL. (I'll occasionally echo the actual SQL statement strings and paste into phpMyAdmin to test, too, but there's definitely something failing there.)

mysql php: How to bind result with the same recurring variable [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'];

Missing argument 2 for {closure}();

I am in the process of working my RESTful Web Service with Slim Framework, and I got this error when I try to get data from 3 tables.
this is the function in my class:
public function getCard($card_id, $card_type_id, $user_id) {
$stmt = $this->conn->prepare("SELECT uc.id, c.card_name, ct.category, c.card_desc, c.card_picture ,c.status, c.created_at from cards c, user_cards uc, cards_type ct WHERE c.card_type_id = ct.card_type_id AND c.card_id = uc.card_id AND uc.user_id = ?");
if ($stmt == FALSE) {
die($this->conn->error);
} else {
$stmt->bind_param("iii", $card_id, $card_type_id, $user_id);
if ($stmt->execute()) {
$res = array();
$stmt->bind_result($id, $card_name, $category, $card_desc, $card_picture, $status, $created_at);
$stmt->fetch();
$res["id"] = $id;
$res["card_name"] = $card_name;
$res["category"] = $category;
$res["card_desc"] = $card_desc;
$res["card_picture"] = $card_picture;
$res["status"] = $status;
$res["created_at"] = $created_at;
$stmt->close();
return $res;
} else {
return NULL;
}
}
}
And this is the code in index.php:
$app->get('/cards/users/:card_id', 'authenticate', function($card_id, $card_type_id) {
global $user_id;
$response = array();
$db = new Card();
// fetch card
$result = $db->getCard($card_id, $card_type_id, $user_id);
if ($result != NULL) {
$response["error"] = false;
$response["id"] = $result["id"];
$response["card_name"] = $result["card_name"];
$response["category"] = $result["category"];
$response["card_desc"] = $result["card_desc"];
$response["card_picture"] = $result["card_picture"];
$response["status"] = $result["status"];
$response["createdAt"] = $result["created_at"];
echoRespnse(200, $response);
} else {
$response["error"] = true;
$response["message"] = "The requested resource doesn't exists";
echoRespnse(404, $response);
}
});
Did i write something wrong ?
Check out this line:
$stmt = $this->conn->prepare("SELECT uc.id, c.card_name, ct.category, c.card_desc, c.card_picture ,c.status, c.created_at from cards c, user_cards uc, cards_type ct WHERE c.card_type_id = ct.card_type_id AND c.card_id = uc.card_id AND uc.user_id = ?");
The query might be hard to see, so I'll format just the query:
SELECT uc.id,
c.card_name,
ct.category,
c.card_desc,
c.card_picture,
c.status,
c.created_at
FROM cards c,
user_cards uc,
cards_type ct
WHERE c.card_type_id = ct.card_type_id
AND c.card_id = uc.card_id
AND uc.user_id = ?
Notice that it only has one question mark, indicating that you should only bind one parameter to your prepared statement.
So you should change this line:
$stmt->bind_param("i", $user_id); //have only one argument

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