เพิ่ม E-CATALOGS

prepare($sql)) { $stmt->bind_param(\"sssssss\", $_POST['shop_name'], $_POST['level'], $_POST['branch'], $_POST['time_open'], $_POST['time_close'], $_POST['tel'], $_POST['content_on']); // execute the insert query if ($stmt->execute()) { // echo \"User was saved.
\";// $stmt->close(); } else { die(\"Unable to save.\"); } } else { die(\"Unable to prepare statement.\"); } // close the database $mysqli->close(); $shop_name = $_POST['shop_name']; $time_open =…","dateCreated":"2016-04-28T19:06:01+07:00","upvoteCount":1,"url":"https://programmerthailand.com/forum/view/form-input-file-submit-%E0%B9%84%E0%B8%A1%E0%B9%88%E0%B9%84%E0%B8%9B/1032#answer-1449","author":{"@type":"Person","name":"admin","url":"https://programmerthailand.com/space/admin"}},{"@type":"Answer","text":"ไม่ทราบว่าแก้ไขได้หรือยังครับ","dateCreated":"2016-04-29T18:40:19+07:00","upvoteCount":0,"url":"https://programmerthailand.com/forum/view/form-input-file-submit-%E0%B9%84%E0%B8%A1%E0%B9%88%E0%B9%84%E0%B8%9B/1032#answer-1450","author":{"@type":"Person","name":"admin","url":"https://programmerthailand.com/space/admin"}},{"@type":"Answer","text":"ได้แล้วครับ ขอบคุณ คุณมานพ มากๆๆๆๆครับ","dateCreated":"2016-05-06T11:21:32+07:00","upvoteCount":0,"url":"https://programmerthailand.com/forum/view/form-input-file-submit-%E0%B9%84%E0%B8%A1%E0%B9%88%E0%B9%84%E0%B8%9B/1032#answer-1455","author":{"@type":"Person","name":"Supat Pooresirichot","url":"https://programmerthailand.com/space/user/view?user=ton.supat"}}]}}]} ข้ามไปยังเนื้อหาหลัก
ProgrammerThailand

สังคมนักพัฒนาระบบ (Community) php

Form input File submit ไม่ไป

ผมสร้าง form ขึ้นมา มันมีปัญหาอยู่ตรงส่วนที่เป็น input file (เลือกรูป) ถ้าผมไม่เลือกรูปมันจะ submit ไม่ไป

หรือถ้ากรณีเราไม่เลือกรูปทำให้มันแทรกภาพ default แทนได้ไหมครับ มันจะได้ submit ผ่าน

Form input File  submit ไม่ไป

<!DOCTYPE HTML>
<html>
<head>
<title>Complete</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="css/admin-css.css" rel="stylesheet" type="text/css">
<link href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.3/styles/github.min.css" rel="stylesheet" >
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet">
<!--link href="css/bootstrap-toggle.css" rel="stylesheet"--->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<!--script src="https://code.jquery.com/jquery-2.1.1.min.js"></script-->


<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css" />
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>


</head>
<body>
<div class="admin-topic-container">
  <h1>เพิ่ม E-CATALOGS</h1>
</div>
<?php
// connect to database
include 'libs/db_connect.php';
// sql query

$bt_disabled ='';


function GetImageExtension($imagetype){
if(empty($imagetype)) return false;
switch($imagetype){
case 'image/bmp': return '.bmp';
case 'image/gif': return '.gif';
case 'image/jpeg': return '.jpg';
case 'image/png': return '.png';
default: return false;
} 
}


if (!empty($_FILES["uploadedimage"]["name"])) {

$file_name=$_FILES["uploadedimage"]["name"];
$temp_name=$_FILES["uploadedimage"]["tmp_name"];
$imgtype=$_FILES["uploadedimage"]["type"];

$ext= GetImageExtension($imgtype);
$imagename=date("d-m-Y")."-".time().$ext;
$target_path = "images/".$imagename;

if(move_uploaded_file($temp_name, $target_path)) {

// if the form was submitted
if($_POST){


$sql = "INSERT INTO detail(shop_name,level, branch, time_open,time_close,tel,content_on,images_path,submission_date)
VALUES(?,?, ?, ?, ?, ?,?,'".$target_path."','".date("Y-m-d")."')";

// if the statement was prepared
if($stmt = $mysqli->prepare($sql) ){

$stmt->bind_param(
"sssssss", 
$_POST['shop_name'],
$_POST['level'],
$_POST['branch'],
$_POST['time_open'],
$_POST['time_close'],
$_POST['tel'],
$_POST['content_on']
);

// execute the insert query
if($stmt->execute()){
//echo "User was saved.<br>";//





$stmt->close();
}else{
die("Unable to save.");
}

}else{
die("Unable to prepare statement.");
}

// close the database
$mysqli->close();
}

}else{

exit("Error While uploading image on the server");
} 




$shop_name = $_POST['shop_name'];
$time_open = $_POST['time_open'];
$time_close = $_POST['time_close'];
$tel = $_POST['tel'];
$level = $_POST['level'];




echo '

<a href="#popupLogin" data-rel="popup" data-position-to="window" class="ui-btn ui-corner-all ui-shadow ui-btn-inline ui-icon-check ui-btn-icon-left ui-btn-a" data-transition="pop">Complete</a>
<div data-role="popup" id="popupLogin" data-theme="a" class="ui-corner-all">

<div class="row">
<div class="col-lg-7 col-md-7 col-sm-7 col-xs-12"><img src="images/'.$imagename.'" style="max-width:300px;"></div>
<div class="col-lg-5 col-md-5 col-sm-5 col-xs-12">


<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12"><p>ชื่อร้าน '.$shop_name.'</p></div>
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12"><p>ชั้น '.$level.'</p></div>
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12"><p>เวลาเปิด '.$time_open.'</p></div>
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12"><p>เวลาปิด '.$time_close.'</p></div>
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12"><p>เบอร์โทร '.$tel.'</p></div>
</div>
</div>

</div>
';

$bt_disabled ='disabled=""';




}

?>

<form action='add.php' method='post' border='0' enctype="multipart/form-data" data-ajax="false">
  <div class="container-fluid">
    <div class="row">
      <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
        <label for="text">ชื่อร้าน</label>
        <input type="text" name="shop_name" id="text-basic" class="form-field" required>
      </div>
        </div>
         <div class="row">
       <div class="col-lg-6 col-md-6 col-sm-6 col-xs-12">
        <label for="textarea">สาขา</label>
        <select name="branch"  class="form-field" >
          <?php include('inc/branch.php'); ?>
        </select>
      </div>
      <div class="col-lg-2 col-md-2 col-sm-2 col-xs-12">
        <label for="textarea">ชั้น</label>
        <input type="text" name="level" id="textarea" class="form-field" required></textarea>
      </div>
     
      <div class="col-lg-4 col-md-4 col-sm-4 col-xs-12">
        <label for="textarea">เบอร์โทร</label>
        <input type="tel" name="tel" id="textarea" class="form-field" >
      </div>
         </div>
           <div class="row">
      <div class="col-lg-4 col-md-4 col-sm-4 col-xs-12">
         <label for="textarea">เวลาเปิด</label>
        <input type="time" data-clear-btn="true" name="time_open" id="date_in"  class="form-field" value="10:00" required>
      </div>
      
      <div class="col-lg-4 col-md-4 col-sm-4 col-xs-12">
          <label for="textarea">เวลาปิด</label>
        <input type="time"  data-clear-btn="true" name="time_close" id="date_out" class="form-field"  value="22:00" required>
      </div>
      
    <div class="col-lg-4 col-md-4 col-sm-4 col-xs-12">

        <label for="file">ภาพร้านค้า</label>
        <input type="file" name="uploadedimage" id="uploadedimage" autofocus accept="image/*" />

       
        
        
        
        
      
      </div> </div>
      
      
        <div class="row">
 <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
      
      <label for="slider2">ON/OFF</label>
<select name="content_on" id="slider2" data-role="slider">
    <option value="off">Off</option>
    <option value="on" selected="selected">On</option>
</select>
      
      </div>   </div>
      
      
        <div class="row">
      <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
        <input type="submit" value="Save"  id="bt-submit" <?php echo $bt_disabled ?>>
      </div>
      
      
       <div class="row">
      <div class="col-lg-6 col-md-6 col-sm-6 col-xs-12">
      <a href="add.php" target="_self" class="ui-btn">เพิ่มข้อมูลใหม่</a>
  </div>
  <div class="col-lg-6 col-md-6 col-sm-6 col-xs-12">
   <a href="index.php" target="_self" class="ui-btn">Back to index</a>
    </div>
   
    </div>
    
  </div>
    </div>
</form>
<script src="js/bootstrap-toggle.js"></script>



</body>
</html>

 

0 คะแนน

3 คำตอบ

1
admin Lv.1 Hello World ตอบ

แก้ไขส่วน PHP หน่อยครับ

1. ตรวจสอบก่อนว่า POST ข้อมูลมายัง
2. ถ้าโพสมาแล้ว มีการ upload รูปป่าว ถ้า upload มาก็ใช้ sql1 ถ้าไม่ upload ก็ใช้ sql2
3. สั่งบันทึก

<?php
// connect to database
include 'libs/db_connect.php';
// sql query

$bt_disabled = '';

function GetImageExtension($imagetype)
{
    if (empty($imagetype))
        return false;
    switch ($imagetype) {
        case 'image/bmp':
            return '.bmp';
        case 'image/gif':
            return '.gif';
        case 'image/jpeg':
            return '.jpg';
        case 'image/png':
            return '.png';
        default:
            return false;
    }
}

if ($_POST) { //ถ้ามีการส่งข้อมูลมา
    
    

    if (! empty($_FILES["uploadedimage"]["name"])) {//ถ้ามีการ upload รูป
        $file_name = $_FILES["uploadedimage"]["name"];
        $temp_name = $_FILES["uploadedimage"]["tmp_name"];
        $imgtype = $_FILES["uploadedimage"]["type"];
        
        $ext = GetImageExtension($imgtype);
        $imagename = date("d-m-Y") . "-" . time() . $ext;
        $target_path = "images/" . $imagename;
        
        if (move_uploaded_file($temp_name, $target_path)) {
            $sql = "INSERT INTO detail(shop_name,level, branch, time_open,time_close,tel,content_on,images_path,submission_date)
VALUES(?,?, ?, ?, ?, ?,?,'" . $target_path . "','" . date("Y-m-d") . "')";
        }else{//upload รูปไม่ได้
            exit("Error While uploading image on the server");
        }
    } else {//ถ้าไม่มีการ upload รูป ให้ใช้ชื่อ default.jpg
        
        $sql = "INSERT INTO detail(shop_name,level, branch, time_open,time_close,tel,content_on,images_path,submission_date)
VALUES(?,?, ?, ?, ?, ?,?,'" . $target_path.'/default.jpg' . "','" . date("Y-m-d") . "')";
    }
    
    // if the statement was prepared
    if ($stmt = $mysqli->prepare($sql)) {
    
        $stmt->bind_param("sssssss", $_POST['shop_name'], $_POST['level'], $_POST['branch'], $_POST['time_open'], $_POST['time_close'], $_POST['tel'], $_POST['content_on']);
    
        // execute the insert query
        if ($stmt->execute()) {
            // echo "User was saved.<br>";//
    
            $stmt->close();
        } else {
            die("Unable to save.");
        }
    } else {
        die("Unable to prepare statement.");
    }
    
    // close the database
    $mysqli->close();
    
    $shop_name = $_POST['shop_name'];
    $time_open = $_POST['time_open'];
    $time_close = $_POST['time_close'];
    $tel = $_POST['tel'];
    $level = $_POST['level'];
    
    echo '

<a href="#popupLogin" data-rel="popup" data-position-to="window" class="ui-btn ui-corner-all ui-shadow ui-btn-inline ui-icon-check ui-btn-icon-left ui-btn-a" data-transition="pop">Complete</a>
<div data-role="popup" id="popupLogin" data-theme="a" class="ui-corner-all">

<div class="row">
<div class="col-lg-7 col-md-7 col-sm-7 col-xs-12"><img src="images/' . $imagename . '" style="max-width:300px;"></div>
<div class="col-lg-5 col-md-5 col-sm-5 col-xs-12">


<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12"><p>ชื่อร้าน ' . $shop_name . '</p></div>
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12"><p>ชั้น ' . $level . '</p></div>
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12"><p>เวลาเปิด ' . $time_open . '</p></div>
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12"><p>เวลาปิด ' . $time_close . '</p></div>
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12"><p>เบอร์โทร ' . $tel . '</p></div>
</div>
</div>

</div>
';
    
    $bt_disabled = 'disabled=""';
}

?>

 

0
admin Lv.1 Hello World ตอบ

ไม่ทราบว่าแก้ไขได้หรือยังครับ

0
Supat Pooresirichot Lv.1 Hello World ตอบ

ได้แล้วครับ ขอบคุณ คุณมานพ มากๆๆๆๆครับ

ตอบคำถามนี้

เข้าสู่ระบบเพื่อตอบคำถามและโหวต

เข้าสู่ระบบ สมัครสมาชิก