Skip to main content
Known Participant
October 7, 2011
Answered

Save image path to MySQL and store file

  • October 7, 2011
  • 1 reply
  • 46199 views

Hi there,

I have a form that saves data to a MySQL db. In this form there are four input type file to upload images.

Everything is made with the Dreamweaver wizard.

What I need to do is to store the file path into MySQL and the file into a folder of the website.

I assume that I need to change in GetSQLValueString($_POST['variable'], "text"), but I don't know how to do it.

Anyone can help???

Thank you VERY mcuh

Jaume

This topic has been closed for replies.
Correct answer MurraySummers

Gentelmen, thanks a lot again!!

I haven't realized about what Lon says. And it is ABSOLUTELY true.

I will need to resize the big images to a certain size.

Can this be done with dreamweaver????

Any ideas?

Thanks

Jaume


Can this be done with dreamweaver????

That is not the question.  You should ask "Can this be done with PHP", and the answer is yes, sure.  Google "PHP image resize" - you'll find lots....

1 reply

Participating Frequently
October 10, 2011

Hi Jaume,

I can't claim credit for this - I got it from here

So want you want to do is upload an image, saving the image itself within your site and the image name in your database.

I haven't figured a way to do this in Dreamweaver wizards, without amending the code, but it's quite straightforward.

Assuming you have a form for people to upload an image, and a new folder within your 'images' folder named 'uploads'...

Using the Dreamweaver server behaviours will have added loads of hidden code to the top of your page, so you need to use the 'code' view to see this (or 'split' if you'd prefer to see both code and design views).

On your form page (mine's called add.php), within the code at the very top of your page which will begin with <?php

and end with

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

Add in the following code:

$target = "../images/uploads/";  //This is the directory where images will be saved//

$target = $target . basename( $_FILES['image']['name']);   //change the image and name to whatever your database fields are called//


Your Dreamweaver wizard will have created the $_POST details needed for each field.  You need to change the one related to your image to a $_FILES rather than $_POST - look for the line that says something like this (depending on your fields):

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {

  $insertSQL = sprintf("INSERT INTO database_name (name, Product_No, image) VALUES (%s, %s, %s)",

                       GetSQLValueString($_POST['Name'], "text"),

                       GetSQLValueString($_POST['Product_No'], "text"),

                       GetSQLValueString($_POST['image'], "text"));

And change the last line to this:

GetSQLValueString($_FILES['image']['name'], "text"));

That tells the form to insert the file to a specified location, and to insert the image's name - as text - into the database.  Now you need to specifiy the location:

  //This code writes the photo to the server//

if(move_uploaded_file($_FILES['image']['tmp_name'], $target))

{

//And confirms it has worked//

echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";

}

else {

//Gives error if not correct//

echo "Sorry, there was a problem uploading your file.";

}

So your amended php will now look like this:

$target = "../images/uploads/";  //This is the directory where images will be saved//

$target = $target . basename( $_FILES['image']['name']);   //change the image and name to whatever your database fields are called//

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {

  $insertSQL = sprintf("INSERT INTO database_name (name, Product_No, image) VALUES (%s, %s, %s)",

                       GetSQLValueString($_POST['Name'], "text"),

                       GetSQLValueString($_POST['Product_No'], "text"),

                         GetSQLValueString($_FILES['image']['name'], "text"));


  //This code writes the photo to the server//

if(move_uploaded_file($_FILES['image']['tmp_name'], $target))

{

//And confirms it has worked//

echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";

}

else {

//Gives error if not correct//

echo "Sorry, there was a problem uploading your file.";

}

Now in your HTML within the form, your code should look something like this:

<label for="image">Image</label>
      <input name="image" type="file" id="image" value="<?php echo $row_add['image']; ?>" />
    </p>
    <p>
      <input type="Submit" name="Add" id="add" value="Add" />
     
      <input type="hidden" name="MM_insert" value="form1" />
  </form>

To display the uploaded images on your website, just put in the location and echo the image field, remembering your ALT tag:

<img src="images/uploads/<?php echo $row_get['image']; ?>" alt="<?php echo $row_get['name']; ?>" /></a>

Good luck!

jaumeHAuthor
Known Participant
October 11, 2011

Becca, thank you very much!!!

I have some problems though, maybe you can help me ;-)

I don't know where to place the

$target = "../images/uploads/";  //This is the directory where images will be saved// $target = $target . basename( $_FILES['image']['name']);   //change the image and name to whatever your database fields are called// 

I think everything else I understand quite well.

I copy the code here so maybe you can have al look to it.

Thank you once again

Jaume.

<?php require_once('Connections/Conexion1.php'); ?>

<?php require_once('Connections/Conexion1.php'); ?>

<?php

if (!function_exists("GetSQLValueString")) {

function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")

{

  if (PHP_VERSION < 6) {

    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {

    case "text":

      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";

      break;   

    case "long":

    case "int":

      $theValue = ($theValue != "") ? intval($theValue) : "NULL";

      break;

    case "double":

      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";

      break;

    case "date":

      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";

      break;

    case "defined":

      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;

      break;

  }

  return $theValue;

}

}

$editFormAction = $_SERVER['PHP_SELF'];

if (isset($_SERVER['QUERY_STRING'])) {

  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);

}

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "UpdateForm")) {

  $updateSQL = sprintf("UPDATE detailsmachines SET Hours=%s, StateCons=%s, Accesory1=%s, Accesory2=%s, Accesory3=%s, Image1=%s, Image2=%s, Image3=%s, Image4=%s WHERE ID=%s",

                       GetSQLValueString($_POST['HoursEdit'], "int"),

                       GetSQLValueString($_POST['State'], "text"),

                       GetSQLValueString($_POST['Accesory1'], "text"),

                       GetSQLValueString($_POST['Accesory2'], "text"),

                       GetSQLValueString($_POST['Accesory3'], "text"),

                       GetSQLValueString($_POST['Image1'], "text"),

                       GetSQLValueString($_POST['Image2'], "text"),

                       GetSQLValueString($_POST['Image3'], "text"),

                       GetSQLValueString($_POST['Image4'], "text"),

                       GetSQLValueString($_POST['id'], "int"));

  mysql_select_db($database_Conexion1, $Conexion1);

  $Result1 = mysql_query($updateSQL, $Conexion1) or die(mysql_error());

  $updateGoTo = "Message Pages/Thanks.html";

  if (isset($_SERVER['QUERY_STRING'])) {

    $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";

    $updateGoTo .= $_SERVER['QUERY_STRING'];

  }

  header(sprintf("Location: %s", $updateGoTo));

}

$colname_DetailRS1 = "-1";

if (isset($_GET['recordID'])) {

  $colname_DetailRS1 = $_GET['recordID'];

}

mysql_select_db($database_Conexion1, $Conexion1);

$query_DetailRS1 = sprintf("SELECT * FROM detailsmachines WHERE ID = %s", GetSQLValueString($colname_DetailRS1, "int"));

$DetailRS1 = mysql_query($query_DetailRS1, $Conexion1) or die(mysql_error());

$row_DetailRS1 = mysql_fetch_assoc($DetailRS1);

$totalRows_DetailRS1 = mysql_num_rows($DetailRS1);

?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Untitled Document</title>

</head>

<body>

<form action="<?php echo $editFormAction; ?>" method="POST" enctype="multipart/form-data" name="UpdateForm" id="UpdateForm">

  <p>

    <label for="id"></label>

    <input name="id" type="hidden" id="id" value="<?php echo $row_DetailRS1['ID']; ?>" />

  </p>

  <table border="1" align="center">

    <tr>

      <td>Family</td>

      <td><?php echo $row_DetailRS1['Family']; ?></td>

    </tr>

    <tr>

      <td>Chasis</td>

      <td><?php echo $row_DetailRS1['Chasis']; ?></td>

    </tr>

    <tr>

      <td>Model</td>

      <td><?php echo $row_DetailRS1['Model']; ?></td>

    </tr>

    <tr>

      <td>Price</td>

      <td><?php echo $row_DetailRS1['Price']; ?></td>

    </tr>

    <tr>

      <td>Hours</td>

      <td><label for="HoursEdit"></label>

        <label for="HoursEdit2"></label>

      <input name="HoursEdit" type="text" id="HoursEdit2" value="<?php echo $row_DetailRS1['Hours']; ?>" /></td>

    </tr>

    <tr>

      <td>Warehouse</td>

      <td><?php echo $row_DetailRS1['Warehouse']; ?></td>

    </tr>

    <tr>

      <td>StateCons</td>

      <td><label for="State"></label>

      <input name="State" type="text" id="State" value="<?php echo $row_DetailRS1['StateCons']; ?>" /></td>

    </tr>

    <tr>

      <td>Accesory1</td>

      <td><label for="Accesory1"></label>

      <input name="Accesory1" type="text" id="Accesory1" value="<?php echo $row_DetailRS1['Accesory1']; ?>" /></td>

    </tr>

    <tr>

      <td>Accesory2</td>

      <td><input name="Accesory2" type="text" id="Accesory2" value="<?php echo $row_DetailRS1['Accesory2']; ?>" /></td>

    </tr>

    <tr>

      <td>Accesory3</td>

      <td><input name="Accesory3" type="text" id="Accesory3" value="<?php echo $row_DetailRS1['Accesory3']; ?>" /></td>

    </tr>

    <tr>

      <td>Accesory4</td>

      <td><input name="Accesory4" type="text" id="Accesory4" value="<?php echo $row_DetailRS1['Accesory4']; ?>" /></td>

    </tr>

    <tr>

      <td>Image1</td>

      <td><label for="Image1"></label><input name="Image1" type="file" id="Image1" value="<?php $row_DetailRS1['Image1']->file_src_pathname; ?>"/>

      </td>

    </tr>

    <tr>

      <td>Image2</td>

      <td><label for="Image2"></label>

      <input name="Image2" type="file" id="Image2" value="<?php echo $row_DetailRS1['Image2']; ?>" /></td>

    </tr>

    <tr>

      <td>Image3</td>

      <td><label for="Image3"></label>

      <input name="Image3" type="file" id="Image3" value="<?php echo $row_DetailRS1['Image3']; ?>" /></td>

    </tr>

    <tr>

      <td>Image4</td>

      <td><label for="Image4"></label>

      <input name="Image4" type="file" id="Image4" value="<?php echo $row_DetailRS1['Image4']; ?>" /></td>

    </tr>

  </table>

  <p align="center">

    <input type="submit" name="Submit" id="Submit" value="Update" />

  </p>

  <input type="hidden" name="MM_update" value="UpdateForm" />

</form>

<p> </p>

</body>

</html><?php

mysql_free_result($DetailRS1);

?>

MurraySummers
Inspiring
October 12, 2011

Just for something to think about, I've always been divided/uncertain if saving the path with the filename is the best way to go. If anything, I think I would at least store the filename in a separate column. The reason being that often, you don't always know where the page will be that will eventually link to the file and document relative links then may not work.

The path can be added to the image source string at any time - either as a static or dynamic value.


I would never store the pathname like that.  Just add it in the static HTML on the page.