2015-12-16 29 views
1

我有一個表格插入並更新編輯和插入表單元素使用PHP

<form name="image_form" enctype="multipart/form-data" action="image-upload.php" method="POST" id="image_form" role="form"> 
<div class="form-group"> 
    <label>Image</label> 
    <input name="picture" type="file" id="pics" value=""> 
</div> 
<div class="form-group"> 
    <label>Image Name</label> 
    <input name="picture_name" type="text" required class="form-control" value="<?php echo $row_images['img_name']; ?>"> 
</div> 
<div class="form-group"> 
    <label>Image Description</label> 
    <input name="picture_descrip" type="text" required class="form-control" value="<?php echo $row_images['img_descrip']; ?>"> 
</div> 
<button type="submit" class="btn btn-default">Save</button> 

形式動作圖像upload.php的

<?php 
    require_once ('connections/dbconnect.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; 
    } 
} 

$target_dir = "imgs/"; 
$target_file = $target_dir . basename($_FILES["picture"]["name"]); 
$uploadOk = 1; 
$imageFileType = pathinfo($target_file, PATHINFO_EXTENSION); 

// Check for image originality 
if (isset($_POST["save"])) { 
    $check = getimagesize($_FILES["picture"]["tmp_name"]); 
    if ($check !== false) { 
     echo "File is an image - " . $check["mime"] . "."; 
     $uploadOk = 1; 
    } 
    else { 
     echo "File is not an image."; 
     $uploadOk = 0; 
    } 
} 

// Check if file already exists 
if (file_exists($target_file)) { 
    echo "Sorry, file already exists."; 
    $uploadOk = 0; 
} 

// image size limit 
if ($_FILES["picture"]["size"] > 600000) { 
    echo "Sorry, your file is too large."; 
    $uploadOk = 0; 
} 

// Allow certain file formats 
if ($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif") { 
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed."; 
    $uploadOk = 0; 
} 

//update query 
if ((isset($_GET['edit'])) && ($_GET['edit'] != "")) { 
    $updateSQL = sprintf("UPDATE `image` SET image=%s, img_name=%s, img_descrip=%s WHERE img_id=%s", GetSQLValueString($_FILES["picture"]["name"], "text"), GetSQLValueString($_POST['picture_name'], "text"), GetSQLValueString($_POST['picture_descrip'], "text"), GetSQLValueString(date("y-m-d"), "date"), GetSQLValueString($_POST['img_id'], "int")); 

    mysql_select_db($database_dbconnect, $dbconnect); 
    $Result1 = mysql_query($updateSQL, $dbconnect) or die(mysql_error()); 

    $updateGoTo = "image-list.php?msg=Image updated successfullly"; 
    if (isset($_SERVER['QUERY_STRING'])) { 
     $updateGoTo.= (strpos($updateGoTo, '?')) ? "&" : "?"; 
     $updateGoTo.= $_SERVER['QUERY_STRING']; 
    } 
    header(sprintf("Location: %s", $updateGoTo)); 
} 
else { 

    //insert query 
    $insertSQL = sprintf("INSERT INTO `images` (image, img_name, img_descrip, img_date) VALUES (%s, %s, %s, %s)", GetSQLValueString($_FILES["picture"]["name"], "text"), GetSQLValueString($_POST['picture_name'], "text"), GetSQLValueString($_POST['picture_descrip'], "text"), GetSQLValueString(date("y-m-d"), "date")); 

    mysql_select_db($database_dbconnect, $dbconnect); 
    $Result1 = mysql_query($insertSQL, $dbconnect) or die(mysql_error()); 

    $insertGoTo = "image-list.php?msg=Image successfully Posted!!!"; 
    if (isset($_SERVER['QUERY_STRING'])) { 
     $insertGoTo.= (strpos($insertGoTo, '?')) ? "&" : "?"; 
     $insertGoTo.= $_SERVER['QUERY_STRING']; 
    } 
    header(sprintf("Location: %s", $insertGoTo)); 
} 

// Check if $uploadOk is set to 0 by an error 
if ($uploadOk == 0) { 
    echo "Sorry, your file was not uploaded."; 

    // if everything is ok, try to upload file 


} 
else { 
    if (move_uploaded_file($_FILES["picture"]["tmp_name"], $target_file)) { 
     echo "The file " . basename($_FILES["picture"]["name"]) . " has been uploaded."; 
    } 
    else { 
     echo "Sorry, there was an error uploading your file."; 
    } 
} 
?> 

現在參數「編輯」就是從這裏開始

<a href="image-edit.php?edit=<?php echo $row_images['img_id']?>" title="Edit User"> 
<button type="button" class="btn btn-primary btn-circle"> 
<i class="fa fa-pencil"></i></button> 
</a> 

但是,當我點擊應該更新它的編輯按鈕不斷插入,我做錯了什麼?

+0

忘記關閉 – devpro

回答

1

在您的SQL查詢的問題,在變量奧德:

$updateSQL = sprintf("UPDATE `image` SET 
     image=%s, 
     img_name=%s, 
     img_descrip=%s 
     WHERE img_id=%s", 
     GetSQLValueString($_FILES["picture"]["name"], "text"), 
     GetSQLValueString($_POST['picture_name'], "text"), 
     GetSQLValueString($_POST['picture_descrip'], "text"), 
     GetSQLValueString(date("y-m-d"), "date"), 
     GetSQLValueString($_POST['img_id'], "int")); 

所以,img_id設置GetSQLValueString(date("y-m-d"), "date") 我認爲應該有img_date在查詢中,這樣的事情:

$updateSQL = sprintf("UPDATE `image` SET 
      image=%s, 
      img_name=%s, 
      img_descrip=%s 
      img_date=%s 
      WHERE img_id=%s", 
+0

好吧我只是嘗試過,但它不斷插入 – Tabytha

+0

添加到您的問題表結構:寫入「SHOW CREATE TABLE tbl_na我「在你的DB – sergio