Kabera Mugwaneza Alexis Kabera Mugwaneza Alexis - 2 months ago 10
PHP Question

How to insert image url in mysql database using constructor function

I am beginner in php and i wish to send image url in database using constructors function . before i used ordinary php and it worked fine .

The following file works great :

<?php

if($_SERVER['REQUEST_METHOD']=='POST'){

$image = $_POST['image'];
$name = $_POST['name'];

require_once('dbConnect.php');

$sql ="SELECT id FROM uploadimagestoserver ORDER BY id ASC";

$res = mysqli_query($con,$sql);

$id = 0;

while($row = mysqli_fetch_array($res)){
$id = $row['id'];
}

$path = "uploads/$id.png";

$actualpath = "https://localhost/GoodPriceApi/uploadImage/$path";

$sql = "INSERT INTO uploadimagestoserver(image,name) VALUES ('$actualpath','$name')";

if(mysqli_query($con,$sql)){
file_put_contents($path,base64_decode($image));
echo "Successfully Uploaded";
}

mysqli_close($con);
}else{
echo "Error";
}
?>


But when i try to add my script in the following function i get an error :
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result

Please help me to integrate up mentioned codes in this function createProfile which is file i nemed DbHandler.php and another one i named upload.php

DbHandler.php

class DbHandler {

private $conn;

function __construct() {
require_once dirname(__FILE__) . '/DbConnect.php';
// opening db connection
$db = new DbConnect();
$this->conn = $db->connect();
}

public function createProfile($image, $name) {

$response = array();

$stmt =$this->conn->prepare ("SELECT id FROM uploadimagestoserver ORDER BY id ASC") ;

$id = 0;

while($row = mysqli_fetch_array($response)){
$id = $row['id'];
}

$path = "uploads/$id.png";
$actualpath = "https://localhost/GoodPriceApi/uploadImage/$path";


// insert query
$stmt = $this->conn->prepare("INSERT INTO uploadimagestoserver(image,name) values(?, ?)");
$stmt->bind_param("is", $actualpath, $name);

$result = $stmt->execute();

$new_user_id = $stmt->insert_id;

$stmt->close();

// Check for successful insertion
if ($result) {

file_put_contents($path,base64_decode($image));


// User successfully inserted
return USER_CREATED_SUCCESSFULLY;
} else {
// Failed to create user
return USER_CREATE_FAILED;
}


return $response;



}
}


upload.php

<?php

include './DbHandler.php';
$db = new DbHandler();


$response = array();

if (isset($_POST['name']) && $_POST['image'] != '') {


$name = $_POST['name'];
$image = $_POST['image'];




$res = $db->createProfile($name, $image);

if ($res == USER_CREATED_SUCCESSFULLY) {


$response["error"] = false;
$response["message"] = "access granted";
} else if ($res == USER_CREATE_FAILED) {
$response["error"] = true;
$response["message"] = "Sorry! Error occurred in registration.";
}
} else {
$response["error"] = true;
$response["message"] = "Sorry! phone number is not valid or missing.";
}

echo json_encode($response);

?>

Answer
 $response = array();

$stmt =$this->conn->prepare ("SELECT id FROM uploadimagestoserver ORDER BY       id ASC") ;

 $id = 0;

while($row = mysqli_fetch_array($response)){
        $id = $row['id'];
}

So say its an array and never execute your $stmt