PDA

View Full Version : Upload image & MYSQL


mbooma
05-18-2010, 04:00 PM
Hi

I wanna create php code ton insert some info in MYSQL BDD, dreamweaver do that but I must upload image too with this, I choose file for this but it juste take name and I wanna upload photo in "images" directory

with PHPMYADMIN, defaut value for image is "nophoto.jpg", when user don't choose image it will take this by default

my code

<?php require_once('../Connections/connexion.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_insert"])) && ($_POST["MM_insert"] == "form1")) {
$insertSQL = sprintf("INSERT INTO livres (titre, auteur, domaine, maison, `description`, cote, `date`, image) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)",
GetSQLValueString($_POST['titre'], "text"),
GetSQLValueString($_POST['auteur'], "text"),
GetSQLValueString($_POST['domaine'], "text"),
GetSQLValueString($_POST['maison'], "text"),
GetSQLValueString($_POST['description'], "text"),
GetSQLValueString($_POST['cote'], "text"),
GetSQLValueString($_POST['date'], "date"),
GetSQLValueString($_POST['image'], "text"));

mysql_select_db($database_connexion, $connexion);
$Result1 = mysql_query($insertSQL, $connexion) or die(mysql_error());
}
?>
<!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>Document sans titre</title>
</head>

<body>
<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
<table align="center">
<tr valign="baseline">
<td nowrap="nowrap" align="right">Titre:</td>
<td><input type="text" name="titre" value="" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Auteur:</td>
<td><input type="text" name="auteur" value="" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Domaine:</td>
<td><input type="text" name="domaine" value="" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Maison:</td>
<td><input type="text" name="maison" value="" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Description:</td>
<td><input type="text" name="description" value="" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Cote:</td>
<td><input type="text" name="cote" value="" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Date:</td>
<td><input type="text" name="date" value="" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Image:</td>
<td><input name="image" type="file" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right"> </td>
<td><input type="submit" value="InsÚrer un enregistrement" /></td>
</tr>
</table>
<input type="hidden" name="MM_insert" value="form1" />
</form>
<p> </p>
</body>
</html>

Corrosive
05-18-2010, 04:16 PM
Have you looked at this tutorial? http://www.dreamweaverclub.com/vtm/uploading-files.php It should cover what you need.

mbooma
05-18-2010, 05:08 PM
try it but have this error

Notice: Undefined index: uploadedfile in C:\wamp\www\monsite\administration\uploader.php on line 7

Notice: Undefined index: uploadedfile in C:\wamp\www\monsite\administration\uploader.php on line 9


these lines :


$target_path = "C:/Documents and Settings/David/My Documents/Local Work/Remote/dwc/upload/".$_FILES['uploadedfile']['name'];

move_uploaded_file($_FILES['uploadedfile']['tmp_name'],$target_path);

Corrosive
05-18-2010, 05:20 PM
I would imagine you will have to change the file paths. The one you have there is pointing to David's hard drive so the index becomes undefined. You'll need to tailor it to suit your needs.

mbooma
05-18-2010, 05:29 PM
Notice: Undefined index: image in C:\wamp\www\monsite\administration\ajout.php on line 3

Notice: Undefined index: image in C:\wamp\www\monsite\administration\ajout.php on line 5


<?php require_once('../Connections/connexion.php'); ?>
<?php
$target_path = "images/".$_FILES['image']['name'];

move_uploaded_file($_FILES['image']['tmp_name'],$target_path);

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 livres (titre, auteur, domaine, maison, `description`, cote, `date`, image) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)",
GetSQLValueString($_POST['titre'], "text"),
GetSQLValueString($_POST['auteur'], "text"),
GetSQLValueString($_POST['domaine'], "text"),
GetSQLValueString($_POST['maison'], "text"),
GetSQLValueString($_POST['description'], "text"),
GetSQLValueString($_POST['cote'], "text"),
GetSQLValueString($_POST['date'], "date"),
GetSQLValueString($_POST['image'], "text"));

mysql_select_db($database_connexion, $connexion);
$Result1 = mysql_query($insertSQL, $connexion) or die(mysql_error());
}
?>
<!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>Document sans titre</title>
</head>

<body>
<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
<table align="center">
<tr valign="baseline">
<td nowrap="nowrap" align="right">Titre:</td>
<td><input type="text" name="titre" value="" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Auteur:</td>
<td><input type="text" name="auteur" value="" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Domaine:</td>
<td><input type="text" name="domaine" value="" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Maison:</td>
<td><input type="text" name="maison" value="" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Description:</td>
<td><input type="text" name="description" value="" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Cote:</td>
<td><input type="text" name="cote" value="" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Date:</td>
<td><input type="text" name="date" value="" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Image:</td>
<td><input name="image" type="file" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">&nbsp;</td>
<td><input type="submit" value="InsÚrer un enregistrement" /></td>
</tr>
</table>
<input type="hidden" name="MM_insert" value="form1" />
</form>
<p>&nbsp;</p>
</body>
</html>

mbooma
05-19-2010, 02:43 PM
UP....need help please ^^

Corrosive
05-19-2010, 04:00 PM
David J is probably the best person to help you and he won't be on until tomorrow I'd have thought. Can you hang on for a bit?

domedia
05-19-2010, 08:55 PM
Moved to PHP forum

mbooma
05-22-2010, 12:33 AM
Upp............

edbr
05-22-2010, 02:00 AM
as the error is showing the file is in administration try changing the target path to this. stab in the dark but it could be this
$target_path = "../images/".$_FILES['image']['name'];

mbooma
05-22-2010, 12:46 PM
I have directory "images" in administration...