如何使用记录插入向导上传文件? Dreamweaver php

时间:2016-01-23 04:03:04

标签: php html mysql dreamweaver

这是插入的代码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>

就是这样,如果你想知道连接代码在哪里,我使用include来使用这个表单。所以连接功能在主页面中。

1 个答案:

答案 0 :(得分:0)

您好,您可以尝试此代码,请确保您将理解上传文件的注释说明。 1:首次使用文件上传器时,我们在表单标签中使用enctype =“multipart / form-data”。

2:使用$ _FILES数组而不是$ _POST

在发布后检索文件

3:我们可以直接在DB或目录中保存文件

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());

}

?>