PHP, AJAX, and Mysql

PHP, AJAX, and Mysql

本文关键字:Mysql and AJAX PHP      更新时间:2023-09-26

我是一个新手,我想问我的项目,我创建了一个文件来上传图像并保存到数据库MySql,它的文件将被分为3个文件

  1. 文件HTML,这是我的代码:

        <form class="form-horizontal" id="inputForm" enctype="multipart/form-data"><input type="hidden" id="id">
              <div class="form-group">
                <label for="name" class="col-sm-2 control-label">Video Name</label>
                <div class="col-sm-6">
                  <input type="text" class="form-control" id="name" placeholder="Video Name">
                </div>
              </div>
              <div class="form-group">
                <label for="nama" class="col-sm-2 control-label">Video URL</label>
                <div class="col-sm-6">
                  <input type="text" class="form-control" id="url" placeholder="https://www.youtube.com/watch?v=ki_dQd8hJP4">
                </div>
              </div>
              <div class="form-group">
                <label for="date" class="col-sm-2 control-label">Date Created</label>
                <div class="col-sm-3">
                  <input type="date" class="form-control" id="date">
                </div>
              </div>
              <div class="form-group">
                <label for="nama" class="col-sm-2 control-label">Image</label>
                <div class="col-sm-3">
                    <input type="file" id="image" class="form-control">
                </div>
              </div>
              <div class="form-group">
                <div class="col-sm-offset-2 col-sm-10">
                  <button type="button" class="btn btn-primary glyphicon glyphicon-save" id="btnSave">Save</button>
                  <button type="button" class="btn btn-danger glyphicon glyphicon-remove" id="btnReset">Reset</button>
                </div>
              </div>
        </form>
    

  • File JavaScript:
  •  $(document).ready(function(){
        Modul.find("button#btnSave").click(function(){
            var name_gr=Modul.find('input#name').val();
            var url_gr=Modul.find('input#url').val();
            var date_gr=Modul.find('input#date').val();
            var image_gr=Modul.find('input#image').val();
            var id_gr=Modul.find('input#id').val();
            $.ajax({
                type    :"POST",
                dataType    : 'json',
                url :"page/video/process_video.php",
                data    :{action:'commit', name:name_gr ,url:url_gr, date:date_gr , image:image_gr ,id:id_gr},
                          success:function(json){
                          alert(json);
                         },
                         complete:function(){
                            selectVideo();
                            Modul.find("div#list").fadeIn();
                            Modul.find("div#form").hide();              
                         }
            });
        }); });


  • 文件PHP
  •     <?php error_reporting(0);    include "../../config/conn.php"; <br> $ACTION=$_POST['action'];
    switch($ACTION){      
        case "commit":
            $ID=$_POST['id'];
            $NAME=$_POST['name'];
            $URL=$_POST['url'];
            $DATE=$_POST['date'];
            $IMAGE=$_POST['image'];
            if($ID==""){
                $query="insert into tb_video (videoname, urlembed, datecreated, gambar) values('$NAME','$URL', '$DATE', '$IMAGE')";
            }else{
                $query="update tb_video set videoname='$NAME',urlembed='$URL',datecreated='$DATE', gambar='$GAMBAR' where idvideo='$ID' ";
            }
            $result=mysql_query($query);
            if($result){
                $msg="Data successfully saved / updated";
                header("refresh:0; url=?page=video");
            }else{
                $msg="Data failed stored / updated";
                header("refresh:0; url=?page=video");
            }
            echo json_encode($msg);
        break;
        default:
            echo json_encode("error");
        break;
        }
    ?>
    

    那么,我如何使用ajax获取输入文件类型的值,然后用ajax上传图像文件并将其保存到MySQL数据库中。非常感谢您的阅读,如果有更少的抱歉。:)

    Wahyudi Andrian你好,

    如果你需要保存图像,我建议你这样做:Php代码:

    <?php
    error_reporting(0);
    include "../../config/conn.php";
    $ACTION=$_POST['action'];
     switch($ACTION){      
     case "commit":
     $ID=$_POST['id'];
     $NAME=$_POST['name'];
     $DATE=$_POST['date'];
     $IMAGE=$_POST['image'];
     // Image Details...
     $IMAGE = $_FILES['image']['name'];
     $IMAGE_SIZE =$_FILES['image']['size'];
     $IMAGE_TMP =$_FILES['image']['tmp_name'];
     $IMAGE_TYPE=$_FILES['image']['type']; 
     $chk_ext = explode(".",$IMAGE);      
     $expensions= array("jpeg","jpg","png"); // Extension of the files
     if( $IMAGE != null ){
      if( in_array(strtolower(end($chk_ext)),$expensions)=== false ){
       $err[]= '[Error] Extension not allowed';
      }
     }
     if( count($err) == 0 ){
      $file_name = $sku.".".strtolower(end($chk_ext));
      $url_imagen = "url_of_the_images/".$file_name;
      move_uploaded_file("url_of_the_images/".$file_name);
      if($ID==""){
       $query="insert into tb_video (videoname, urlembed, datecreated, gambar)values('$NAME','$url_imagen', '$DATE', '$IMAGE')";
      }else{
       $query="update tb_video set videoname='$NAME',urlembed='$url_imagen',datecreated='$DATE', gambar='$GAMBAR' where idvideo='$ID' ";
      }
      $result=mysql_query($query);
     }
     if($result){
      $msg="Data successfully saved / updated";
      header("refresh:0; url=?page=video");
     }else{
      $msg="Data failed stored / updated";
      header("refresh:0; url=?page=video");
     }
     echo json_encode($msg);
     break;
     default:
     echo json_encode("error");
     break;
    }
    

    在这个例子中,你可以看到我使用一个文件夹来保存图像,并将url添加到表中。我希望这能帮到你。