dpafea04148 2016-01-23 04:03
浏览 138
已采纳

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

Here is the code php for insert

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

And here is my insert form code

<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>

That's all, if youre wondering where is the connection code, i use this form using include. so the connection function is in the main page.

  • 写回答

1条回答 默认 最新

  • dongxing6802 2016-01-23 04:55
    关注

    Hi you can try this code, please make sure you will understand commented instruction to upload files. 1: when ever using file uploader first we mast use enctype="multipart/form-data" in form tag.

    2: Files are retrieved after post using $_FILES array not $_POST

    3: We can save file directly in DB or a directory

    i: If you are saving files directly in db then you should use blob datatype in db column name.

    ii: If you are saving file in a directory and save its name in db. [I explain same thing in your code]

    <?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());
    
    }
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 汇编语言除法溢出问题
  • ¥50 C++实现删除N个数据列表共有的元素
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗