Home > OS >  How do I create a variable from database inquiry to change photo name
How do I create a variable from database inquiry to change photo name

Time:08-29

I Hope someone can help me solve this problem I have. I have been working on it for days and so many similar questions and answers are over my head. I'm trying my best to figure it out. So far, I am getting close. I am totally new to some of this. I am trying to build a recipe site where people upload a photo along with their recipe. With each recipe submission a random number is assigned to their recipe. This is the number from the database I am trying to change their photo submitted photo name to so that it is associated with the recipe. I hope that is understandable. I'm trying to give as much info as I can so you can understand what I am trying to do. Thank You in advance for any help you can give me. I am 63 years old who is becoming hooked on PHP and MySQL. I have previously been working with HTML and CSS but love the things you can do with PHP and MySQL. This is what I have so far:

$select = "SELECT * FROM VaRecipes ORDER BY id DESC LIMIT 1;";
$result = $conn->query($select);

Doing this to show the image works great.

<img src="../images/<?php while ($row = $result->fetch_object()){ echo $row->extra03; } ?>.jpg" />

The photo shows on the web page exactly as I want. I'm sure it's got to be a better way of doing it. I'm just a beginner with mySQL & PHP. But here's where I need a way to set a variable that I can use to rename and move the photo. What I have so far is:

<?php rename("uploadedphotos/uploadedphotos.jpg","../images/new_name.jpg"); ?>

This example works partly. It renames the image and moves it to the correct folder but the renamed image is named:

<?php while( = ()){ echo ; } ?>'.jpg

instead of the name it is supposed to be named which is a number from the database.

This is my first question here so Hi everyone! ~~Mark

CodePudding user response:

this is all wrong...

When you query a database you get an array/object of results...

$select = "SELECT * FROM VaRecipes ORDER BY id DESC LIMIT 1;";
$result = $conn->query($select);

Now you have your results that you can iterate...

<?php while ($row = $result->fetch_object()): ?>

// While in the level loop context we can utilize the current result..
<img src="../images/<?php echo {$row->imgPath}; ?>.jpg" /> // This prints the img html that will load the image

// Then we can do other stuff while int he loop...

<?php rename("uploadedphotos/uploadedphotos.jpg","../images/{$row->imgName}.jpg"); ?>

<? endwhile ?>;

To be honest i dont understand the concept... TBH you should not be moving the picture in a view context but regardless...

You should be moving a variable pic from one dir to another..

rename("uploadedphotos/{$row->imgName}.jpg","../images/{$row->imgName}.jpg")

For whatever reason you are doing this you migth want to to move the image bofore rendering in

The resulting $row array data only you can know since you havnt shared it with us..

CodePudding user response:

if(isset($_FILES)) {
    $name = $_FILES['name']; // The name provided by your form
    $type = $_FILES['type']; // type of file uploaded
    $path = $_FILES['tmp_name']; // the temp location it is stored in
    $size = $_FILES['size']; // the size of it.
    $error = $_FILES['error']; // any errors occured

    $randomKey = bin2hex(random_bytes(16));

    rename($path,"../images/$randomKey.jpg");

    // save to database path => ../images/$randomKey.jpg
}

  • Related