Jump to content


Photo

referencing images in the database

php mysql mysqli

  • Please log in to reply
11 replies to this topic

#1 Drumlegend

Drumlegend

    Member

  • Members
  • PipPip
  • 26 posts

Posted 07 February 2013 - 04:45 PM

I am still really new to php so I am sorry if I don't make much sense but what I want to be able to achieve is to retrieve images from a file directory that will be referenced in the database.

Here is my code for submitting the images

<?php
$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000)
&& in_array($extension, $allowedExts))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br>";
    echo "Type: " . $_FILES["file"]["type"] . "<br>";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";

    if (file_exists("upload/" . $_FILES["file"]["name"]))
	  {
	  echo $_FILES["file"]["name"] . " already exists. ";
	  }
    else
	  {
	  move_uploaded_file($_FILES["file"]["tmp_name"],
	  "upload/" . $_FILES["file"]["name"]);
	  echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
	  }
    }
  }
else
  {
  echo "Invalid file";
  }
?>

Here is my sql for the created table

CREATE TABLE `recipes` (
    `recipeid` INT(11) UNSIGNED ZEROFILL PRIMARY KEY AUTO_INCREMENT,
    `recipename` VARCHAR(50) NOT NULL,
    `ingredients` VARCHAR(50) NOT NULL,
    `instructions` VARCHAR(50) NOT NULL,
    `imagename` VARCHAR(50) NOT NULL,
    
    
    `created` DATETIME NOT NULL
)

Thank you in advance.

#2 mindblown

mindblown

    Member

  • Members
  • PipPip
  • 16 posts

Posted 07 February 2013 - 04:49 PM

I would love to hear the answer to this one too.

#3 shlumph

shlumph

    Advanced Member

  • Members
  • PipPipPip
  • 576 posts
  • LocationNew Hampshire

Posted 07 February 2013 - 04:50 PM

Are you asking for help in regards to inserting the image's reference in the database? That looks like an upload script.
whether you think you can, or can't, you're right -HF
Visit my website or github

#4 Drumlegend

Drumlegend

    Member

  • Members
  • PipPip
  • 26 posts

Posted 07 February 2013 - 04:53 PM

That is the upload script, so I have managed to upload the photo to the server but I want to retrieve it.

This code might make more sense.

<!DOCTYPE html>
<html>
    <head>
	    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <head><link rel="stylesheet" type="text/css" href="../style/user.css"></head>
	    <title>Users</title>
    </head>
<?php
include('common.php');
    
    $sqlget ="SELECT * FROM recipes";
    
    $sqldata= mysqli_query($dbcon, $sqlget) or die('error getting data');
?>
    
    <body>
	   <section id="mainContainer">
       <h1>test</h1>
            <section id="photo">
            <h1>test</h1>
            <a href="#">Edit Photo</a>
            </section>
            <section id="ad1">
            <h1>test</h1>
            </section>
            <section id="leftContainer">
            <h1>Saved Recipe</h1>
            <input type="Submit" Value="View more recipes"/>
            </section>
            <section id="ad2">
            <h1>test</h1>
            </section>
            <section id="middleContainer">
                <?php
    
    echo "<table>";
    echo "<tr><th>ID</th><th>Recipe Name</th><th>Ingredients</th><th>Instructions</th></tr>";
    
    while($row = mysqli_fetch_array($sqldata, MYSQLI_ASSOC)) {    
        echo "<tr><td>";
        echo $row['recipeid'];
        echo "</td><td>";
        echo $row['recipename'];
        echo "</td><td>";
        echo $row['ingredients'];
        echo "</td><td>";
        echo $row['instructions'];
        echo "</td></tr>";
        }
        echo "</table>";
    ?>
            <h1>About Me</h1>
            </section>
            
       
       </section>
    </body>
</html>

I basically want each recipe to display an image

#5 shlumph

shlumph

    Advanced Member

  • Members
  • PipPipPip
  • 576 posts
  • LocationNew Hampshire

Posted 07 February 2013 - 04:59 PM

Your upload script does not insert the images file location into the recipes table. Unless you left that out?
whether you think you can, or can't, you're right -HF
Visit my website or github

#6 Drumlegend

Drumlegend

    Member

  • Members
  • PipPip
  • 26 posts

Posted 07 February 2013 - 05:02 PM

I did leave that out as I'm not sure how to do that.

#7 shlumph

shlumph

    Advanced Member

  • Members
  • PipPipPip
  • 576 posts
  • LocationNew Hampshire

Posted 08 February 2013 - 10:03 AM

Oh ok. Yes, you'll need them in the database first, before you display them :happy-04:

Depending on if the recipe is already in the database or not, you'll need to either UPDATE or INSERT. Is the recipe already in the database when submitting the image?
whether you think you can, or can't, you're right -HF
Visit my website or github

#8 Drumlegend

Drumlegend

    Member

  • Members
  • PipPip
  • 26 posts

Posted 11 February 2013 - 04:07 PM

I don't want store the images in the database, I know you can do that but it is not practical for what I'm doing as I am storing multiple recipes. The recipe is already stored in the database.

#9 Jessica

Jessica

    This is not my name.

  • Gurus
  • 8,982 posts
  • LocationDallas, TX
  • Age:26

Posted 11 February 2013 - 07:08 PM

How do you know what recipe the image goes with? UPDATE that recipe's row.
My goal in replying to posts is to help you become a better programmer, including learning how to debug your own code and research problems. For that reason, rather than posting the solution, I reply with tips and hints on how to find the solution yourself. See below for useful links when you get stuck.

How to Get Good Help: How to Ask Questions | Don't be a help vampire
Debugging Your Code: Debugging your SQL | What does a php function do? | What does a term mean? | Don't see any errors?
Things You Should Do: Normalize Your Data | use print_r() or var_dump()
Lulz: "Functions should not have side effects." - trq

Please take a look at my new PHP/Web Dev blog: The Web Mason - Thanks!!

#10 Drumlegend

Drumlegend

    Member

  • Members
  • PipPip
  • 26 posts

Posted 12 February 2013 - 06:14 AM

How do you know what recipe the image goes with? UPDATE that recipe's row.


That's the question I am after, I'm not sure how to do that.

#11 shlumph

shlumph

    Advanced Member

  • Members
  • PipPipPip
  • 576 posts
  • LocationNew Hampshire

Posted 12 February 2013 - 09:37 AM

Hopefully this helps:



$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000)
&& in_array($extension, $allowedExts))
{
    if ($_FILES["file"]["error"] > 0)
    {
        echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
    }
    else
    {
        echo "Upload: " . $_FILES["file"]["name"] . "<br>";
        echo "Type: " . $_FILES["file"]["type"] . "<br>";
        echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
        echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";

        if (file_exists("upload/" . $_FILES["file"]["name"]))
        {
            echo $_FILES["file"]["name"] . " already exists. ";
        }
        else
        {
            move_uploaded_file($_FILES["file"]["tmp_name"],
            "upload/" . $_FILES["file"]["name"]);

            // Connect to database
            include('common.php'); // Assuming it's here? ($dbcon)

            $sql = "UPDATE recipes SET imagename = '{$_FILES['file']['name']}' WHERE recipeid= '{$_GET['recipeid']}'"; // Assuming $_GET is set like this?
            mysqli_query($dbcon, $sql) or trigger_error(mysql_error());

            echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
        }
    }
}
else
{
    echo "Invalid file";
}

whether you think you can, or can't, you're right -HF
Visit my website or github

#12 Jessica

Jessica

    This is not my name.

  • Gurus
  • 8,982 posts
  • LocationDallas, TX
  • Age:26

Posted 12 February 2013 - 09:50 AM

How do YOU the person using this system, know what image goes with what recipe?
My goal in replying to posts is to help you become a better programmer, including learning how to debug your own code and research problems. For that reason, rather than posting the solution, I reply with tips and hints on how to find the solution yourself. See below for useful links when you get stuck.

How to Get Good Help: How to Ask Questions | Don't be a help vampire
Debugging Your Code: Debugging your SQL | What does a php function do? | What does a term mean? | Don't see any errors?
Things You Should Do: Normalize Your Data | use print_r() or var_dump()
Lulz: "Functions should not have side effects." - trq

Please take a look at my new PHP/Web Dev blog: The Web Mason - Thanks!!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com