Page 1 of 1

upload images....

Posted: Mon Sep 27, 2010 4:20 pm
by Alidad
Hi, I use dreamweaver insert code to insert value into mysql database, and then the user need to upload imaegs, and save file path name in mysql and images file name into the folder name "images.
The problem i just can't figure out myself what to change to insert images file path name into the database at the same time store images file in the folder name "images" in same directory.

Code: Select all

<?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_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO movie (title, `state`, location, `date`, trailer, photo) VALUES (%s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['title'], "text"),
                       GetSQLValueString($_POST['state'], "text"),
                       GetSQLValueString($_POST['location'], "text"),
                       GetSQLValueString($_POST['date'], "text"),
                       GetSQLValueString($_POST['trailer'], "text"),
                       GetSQLValueString($_POST['images'], "text"));

  mysql_select_db($database_moviedata, $moviedata);
  $Result1 = mysql_query($insertSQL, $moviedata) or die(mysql_error());
}

$colname_movie_database = "-1";
if (isset($_GET['id'])) {
  $colname_movie_database = $_GET['id'];
}
mysql_select_db($database_moviedata, $moviedata);
$query_movie_database = sprintf("SELECT * FROM movie WHERE id = %s", GetSQLValueString($colname_movie_database, "int"));
$movie_database = mysql_query($query_movie_database, $moviedata) or die(mysql_error());
$row_movie_database = mysql_fetch_assoc($movie_database);
$totalRows_movie_database = mysql_num_rows($movie_database);
?>
This is forms code.
<form action="<?php echo $editFormAction; ?>" method="POST" enctype="multipart/form-data" name="form1" id="form1">
<table width="500" border="1" align="center">
<tr>
<td width="90">Title</td>
<td width="229"><label for="title"></label>
<input type="text" name="title" id="title" /></td>
<td colspan="2">&nbsp;</td>
</tr>
<tr>
<td>State:</td>
<td><label for="state"></label>
<input type="text" name="state" id="state" /></td>
<td colspan="2">&nbsp;</td>
</tr>
<tr>
<td>location:</td>
<td><label for="location"></label>
<input type="text" name="location" id="location" /></td>
<td colspan="2">&nbsp;</td>
</tr>
<tr>
<td>Date:</td>
<td><label for="date"></label>
<input type="text" name="date" id="date" /></td>
<td colspan="2">&nbsp;</td>
</tr>
<tr>
<td>Trailer:</td>
<td><label for="trailer"></label>
<input type="text" name="trailer" id="trailer" /></td>
<td colspan="2">&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td colspan="2">&nbsp;</td>
</tr>
<tr>
<td>Images:</td>
<td><label for="images"></label>
<input type="file" name="images" id="images" /></td>
<td colspan="2">&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td colspan="2">&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td width="65"><input type="submit" name="submit" id="submit" value="Submit" /></td>
<td width="88"><input type="submit" name="cancel" id="cancel" value="Cancel" /></td>
</tr>
</table>
<input type="hidden" name="MM_insert" value="form1" />
</form>

Re: upload images....

Posted: Mon Sep 27, 2010 5:44 pm
by Jonah Bron
I opinion is that Dreamweaver inserted snippets are often bad. I suggest you check this out:

http://www.google.com/search?q=php+uplo ... e+tutorial

Re: upload images....

Posted: Mon Sep 27, 2010 7:20 pm
by Alidad
thank you so much for your response, i just found basic tutorial for myself to learn i still have trouble of how to create that insert data into the database along with images files path. please take look at the code and see the line where it said "$pic=($_FILES['photo']['name'])";", is this correct code where to put for insert images file path name! if not , how can i correct, of what code to write!

Code: Select all

<?php 
//connect to database. Username and password need to be changed 
mysql_connect("localhost", "root", ""); 

//Select database, database_name needs to be changed 
mysql_select_db("events"); 


  $target = "images/"; $target = $target . basename( $_FILES['photo']['name']);

$sql="INSERT INTO movie (title, state, location, date, trailer, photo)
VALUES
('$_POST[title]','$_POST[state]','$_POST[location]', '$_POST[ldate]','$_POST[trailer]',$pic=($_FILES['photo']['name'])";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";

mysql_close($con)
?> 

Re: upload images....

Posted: Mon Sep 27, 2010 9:45 pm
by Jonah Bron

Code: Select all

<?php 
//connect to database. Username and password need to be changed 
mysql_connect("localhost", "root", ""); 

//Select database, database_name needs to be changed 
mysql_select_db("events"); 


  $target = "images/"; $target = $target . basename( $_FILES['photo']['name']);

$sql='
INSERT
INTO movie (
    title,
    state,
    location,
    date,
    trailer,
    photo
)
VALUES (
    "' . $_POST[title] . '",
    "' . $_POST[state] . '",
    "' . $_POST[location] . '",
    "' . $_POST[ldate] . '",
    "' . $_POST[trailer] . '",
    "' . $_FILES['photo']['name'] . '"
)';

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";

mysql_close($con)
?> 
That looks good.