Upload af billede sammen med andre felter til MySQL-tabel
Hej,Jeg har lavet nedenstående kode i Dreamweaver ud fra en "Lav dit eget CMS-system"-bog. Jeg har nu tage to funktioner fra bogen og lagt sammen - jeg har tre felter (name, age, email) jeg gerne vil indsætte i en MySQL-tabel sammen med et billede, der samtidig skal loades op og hvortil en sti skal skrives i databasen. Ind til videre virker koden sådan at de tre øverste felter skrives i en række og billedet i en anden. kan I hjælpe?
Hoden for hele siden følger her:
<?php require_once('../Connections/tvduno.php'); ?>
<?php
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $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"] == "upload")) {
$insertSQL = sprintf("INSERT INTO hotornot_billeder (name, age, email) VALUES (%s, %s, %s)",
GetSQLValueString($_POST['name'], "text"),
GetSQLValueString($_POST['age'], "text"),
GetSQLValueString($_POST['email'], "text"));
mysql_select_db($database_tvduno, $tvduno);
$Result1 = mysql_query($insertSQL, $tvduno) or die(mysql_error());
$insertGoTo = "index.php";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="../css/styles.css" rel="stylesheet" type="text/css">
</head>
<body bgproperties="fixed">
<table width="100%" border="0" cellpadding="0" cellspacing="2" class="layout">
<tr>
<td colspan="2" class="top"> </td>
</tr>
<tr>
<td height="83" colspan="2" class="menuroom">
<table width="200" border="0" align="center" cellpadding="0" cellspacing="2" class="menu">
<tr>
<td width="20%" class="menu1"><div align="center"><a href="../index.php?id=1&mainid=1">Nyheder</a></div></td>
<td width="20%" class="menu2"><div align="center"><a href="../index.php?id=2&mainid=2">Internet-TV</a></div></td>
<td width="20%" class="menu3"><div align="center"><a href="../index.php?id=3&mainid=3">Mobil</a></div></td>
<td width="20%" class="menu4"><div align="center"><a href="../index.php?id=4&mainid=4">Services</a></div></td>
<td width="20%" class="menu5"><div align="center"><a href="../index.php?id=5&mainid=5">DUNO</a> </div></td>
</tr>
</table></td>
</tr>
<tr>
<td class="left"> </td>
<td class="middle"><table width="600" border="0" cellspacing="0" cellpadding="2">
<tr>
<td><h1>Upload billede til HOT-or-NOT: </h1></td>
</tr>
<tr>
<td><?
if (isset($_FILES['billede'])) {
print "Fil, der er uploadet: {$_FILES['billede']['name']}<p>\n";
$query = "INSERT INTO hotornot_billeder SET picfile='".$_FILES['billede']['name']."'";
$Result1 = mysql_query($query, $tvduno) or die(mysql_error());
}
{
$tempfile = $_FILES['billede']['tmp_name'];
$destination = "../billeder/hotornot/{$_FILES['billede']['name']}";
copy($tempfile, $destination);
}
?></td>
</tr>
<tr>
<td><form action="<?php echo $editFormAction; ?>" method="POST" enctype="multipart/form-data" name="upload" id="upload">
<table width="100%" border="0" cellspacing="0" cellpadding="2">
<tr>
<td width="22%">Navn:</td>
<td width="78%"><input name="name" type="text" id="navn2" size="50"></td>
</tr>
<tr>
<td>Alder:</td>
<td><input name="age" type="text" id="age2" size="5"></td>
</tr>
<tr>
<td>E-mail:</td>
<td><input name="email" type="text" id="email" size="50"></td>
</tr>
<tr>
<td>Vælg billede: </td>
<td><input name="billede" type="file" id="billede" size="50"></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="Submit" value="Upload"></td>
</tr>
</table>
<input type="hidden" name="MM_insert" value="upload">
</form></td>
</tr>
</table></td>
</tr>
</table>
</body>
</html>
