PHP Image upload stored as url
Web Technologies
Web Development
2 years ago
5
Star Rating
1
Rating
_x000D_
_x000D_
For a project I need to build some kind of auction platform using PHP for the backend. I am currently trying to upload images using PHP and then storing the url in the "Item" table in my sql database. I followed this tutorial for image uploading, however, I am not sure now how to connect it to my database now. My code for the image uploading looks as follows:
server > images > upload.php:
// Tutorial: https://www.w3schools.com/php/php_file_upload.asp
500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats (JPG, JPEG, PNG, and GIF)
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
As mentioned I would like to retrieve the image url and store it in the "Item" table. I connected the database by building an API using this tutorial. So basically my "Item" model looks as follows: server > model > Item.php
conn = $db;
}
// read products
function read()
{
return p_Item_sel_all($this->conn);
}
// search products
function search($search)
{
return p_Item_search($this->conn,$search);
}
function create()
{
// TODO: Check that start date is in the future, and before the end date
// check reserver price is positive
// sanitize
$this->Name = htmlspecialchars(strip_tags($this->Name));
$this->Description = htmlspecialchars(strip_tags($this->Description));
$this->AuctionStart = htmlspecialchars(strip_tags($this->AuctionStart));
$this->AuctionEnd = htmlspecialchars(strip_tags($this->AuctionEnd));
$this->StartingPrice = htmlspecialchars(strip_tags($this->StartingPrice));
$this->ReservePrice = htmlspecialchars(strip_tags($this->ReservePrice));
$this->PhotoURL = htmlspecialchars(strip_tags($this->PhotoURL));
$this->SellerID = htmlspecialchars(strip_tags($this->SellerID));
// $this->created=htmlspecialchars(strip_tags($this->created));
if (p_Item_ins($this->conn, $this->Name, $this->Description, $this->AuctionStart, $this->AuctionEnd, $this->StartingPrice, $this->ReservePrice, $this->PhotoURL, $this->SellerID)) {
return true;
};
return false;
}
// used when filling up the update product form
function readOne()
{
$stmt = p_Item_sel_id($this->conn, $this->ID);
// get retrieved row
$row = $stmt->fetch(PDO::FETCH_ASSOC);
// set values to object properties
$this->ID = $row['ID '];
$this->Name = $row['Name '];
$this->Description = $row['Description '];
$this->AuctionStart = $row['AuctionStart '];
$this->AuctionEnd = $row['AuctionEnd '];
$this->AuctionFinished = $row['AuctionFinished '];
$this->StartingPrice = $row['StartingPrice '];
$this->ReservePrice = $row['ReservePrice '];
$this->FinalPrice = $row['FinalPrice '];
$this->PhotoURL = $row['PhotoURL '];
$this->SellerID = $row['SellerID '];
}
function increment_views()
{
return p_Item_incr_views($this->conn, $this->ID);
}
function update(){
// execute the query
if (p_Item_upd($this->conn, $this->ID, $this->Name, $this->Description, $this->AuctionStart, $this->AuctionEnd, $this->AuctionFinished, $this->StartingPrice, $this->ReservePrice, $this->FinalPrice, $this->PhotoURL, $this->SellerID)) {
return true;
}
return false;
}
// delete the Item
function delete(){
// execute query
if(p_Item_del_id($this->conn, $this->ID)){
return true;
}
return false;
}
}
The functions (create, delete, read_one, read, search, update) are all in different files. The SQL queries are stored in sproc files. If needed, I can of course also include the code here.
This is probably quite a simple connection from upload to the database but as I am completely new to PHP with a tight project deadline, I would very much appreciate any help!
Posted on 16 Aug 2022, this text provides information on Web Development related to Web Technologies. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.