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

报告相同问题?

悬赏问题

  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?