[英]How to upload file using a record insert wizard? Dreamweaver php
這是用於插入的代碼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 siswa (NISN, Kode_KK, Nama_Siswa, Alamat_Siswa, Tgl_Lahir, Foto_siswa) VALUES (%s, %s, %s, %s, %s, %s)",
GetSQLValueString($_POST['NISN'], "int"),
GetSQLValueString($_POST['Kode_KK'], "int"),
GetSQLValueString($_POST['Nama_Siswa'], "text"),
GetSQLValueString($_POST['Alamat_Siswa'], "text"),
GetSQLValueString($_POST['Tgl_Lahir'], "date"),
GetSQLValueString($_POST['Foto_siswa'], "text"));
mysql_select_db($database_praukkcon, $praukkcon);
$Result1 = mysql_query($insertSQL, $praukkcon) or die(mysql_error());
}
?>
這是我的插入表單代碼
<form method="post" name="form1" action="<?php echo $editFormAction; ?>" enctype="multipart/form-data">
<table width="490" height="308" align="center">
<tr valign="baseline">
<td height="24" align="right" valign="middle" nowrap><div align="center">NISN:</div></td>
<td valign="middle"><input type="text" name="NISN" value="MAKS 10" size="32" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'MAKS 10';}"></td>
</tr>
<tr valign="baseline">
<td height="24" align="right" valign="middle" nowrap><div align="center">Kode_KK:</div></td>
<td valign="middle"><input type="text" name="Kode_KK" value="MAKS 4" size="32" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'MAKS 4';}"></td>
</tr>
<tr valign="baseline">
<td height="24" align="right" valign="middle" nowrap><div align="center">Nama_Siswa:</div></td>
<td valign="middle"><input type="text" name="Nama_Siswa" value="MAKS 50" size="32" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'MAKS 50';}"></td>
</tr>
<tr valign="baseline">
<td height="24" align="right" valign="middle" nowrap><div align="center">Alamat_Siswa:</div></td>
<td valign="middle"><input type="text" name="Alamat_Siswa" value="" size="32"> </td>
</tr>
<tr valign="baseline">
<td height="24" align="right" valign="middle" nowrap><div align="center">Tgl_Lahir:</div></td>
<td valign="middle"><input type="text" name="Tgl_Lahir" value="YYYY-MM-DD" size="32" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'YYYY-MM-DD';}"></td>
</tr>
<tr valign="baseline">
<td height="24" align="right" valign="middle" nowrap><div align="center">Foto_siswa:</div></td>
<td valign="middle"><input type="file" name="Foto_siswa" value="" size="32"> </td>
</tr>
<tr valign="baseline">
<td height="26" align="right" valign="middle" nowrap><div align="center"></div></td>
<td valign="middle"><div align="center">
<input type="submit" value="INSERT DATA">
</div></td>
</tr>
</table>
<input type="hidden" name="MM_insert" value="form1">
</form>
就是這樣,如果您想知道連接代碼在哪里,我會使用包含在內的這種形式。 因此連接功能在主頁中。
嗨,您可以嘗試使用此代碼,請確保您了解上載文件的注釋說明。 1:第一次使用文件上傳器時,我們會在表單標簽中使用enctype =“ multipart / form-data”。
2:使用$ _FILES數組而不是$ _POST在發布后檢索文件
3:我們可以將文件直接保存在數據庫或目錄中
i:如果直接在db中保存文件,則應在db列名中使用blob數據類型。
ii:如果要將文件保存在目錄中,並將其名稱保存在db中。 [我在您的代碼中解釋了同樣的事情]
<?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")) {
//Here First upload a files in physical(drive) location
$_FileNameToSave = ''; //If file is not uploaded then initialize it value balnk
$target_file = '/fileuploader'; //put here your uploaded directory name or create a folder "fileuploader" in your project root directory
if($_FILES['Foto_siswa']['name'] && sizeof($_FILES['Foto_siswa']['name']) > 0) {
if (move_uploaded_file($_FILES["Foto_siswa"]["tmp_name"], $target_file))
{
$_FileNameToSave = $_FILES['Foto_siswa']['name'];
}
}
$insertSQL = sprintf("INSERT INTO siswa (NISN, Kode_KK, Nama_Siswa, Alamat_Siswa, Tgl_Lahir, Foto_siswa)
VALUES (%s, %s, %s, %s, %s, %s)",
GetSQLValueString($_POST['NISN'], "int"),
GetSQLValueString($_POST['Kode_KK'], "int"),
GetSQLValueString($_POST['Nama_Siswa'], "text"),
GetSQLValueString($_POST['Alamat_Siswa'], "text"),
GetSQLValueString($_POST['Tgl_Lahir'], "date"),
GetSQLValueString($_FileNameToSave, "text"));//file name to save in db
mysql_select_db($database_praukkcon, $praukkcon);
$Result1 = mysql_query($insertSQL, $praukkcon) or die(mysql_error());
}
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.