基于复选框检查更新所选图像

时间:2016-06-26 08:41:18

标签: javascript php mysql

我需要上传所选图片,但我不知道如何制作。任何人都可以帮我解决这个问题?包含以下代码。感谢您提前帮助。

test.php的

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script>
function myFunction1(isChecked2){
$('.chckbox1').change( function() {
    var isChecked2 = this.checked;
    if(isChecked2) {
        $(this).parents("tr:eq(0)").find(".product_img").prop("disabled",false); 
    } else {
        $(this).parents("tr:eq(0)").find(".product_img").prop("disabled",true);}
})};
</script>
</head>

<body>
<form method="post" action="UPDATE.php">
<div class="row">
<div class="col-xs-4">
<table id="simple-table" class="table table-striped table-bordered table-hover">
<thead>
   <tr>
   <th class="center">Select</th>
   <th class="center">Product Image</th>
   <th class="center">New Image</th>
   </tr>
</thead>
<tbody>
<?php 

//connect to database
$con = mysql_connect("localhost","root","");
if (!$con)
{
    die('Could not connect: ' . mysql_error());
}
//select table from database
mysql_select_db("MBTR", $con);

$result = mysql_query("SELECT * from maray_product_img WHERE product_id='P100044'");

//query resutl
$countrow = 1;
//read result           
while($row = mysql_fetch_array($result))
{

    $img_id=$row['img_id'];
    $p_id = $row['product_id'];
    $product_img_name = $row['product_img_name'];
    $product_img = $row['product_img'];
    $product_img_path = $row['product_img_path'];
    //$product_quantity = $row['product_quantity'];
?>
     <tr>
     <td class="center" style="vertical-align:middle"> <input name="img_id[]" type="checkbox" id="check[]" class="chckbox1 ace" value="<?php echo $img_id;?>" onclick="myFunction1(this)"/><span class="lbl red"><input type="hidden" name="product_id[<?php echo $img_id;?>]" value="<?php echo $p_id;?>"></span><input type="hidden" name="product_img_name[<?php echo $img_id;?>]" value="<?php echo $product_img_name;?>"></td>
      <td class="center"><img src="<?php echo $row['product_img_path'];?>" width="60" height="60"></td>
      <td class="center" style="vertical-align:middle"><input type="file" name="product_img[<?php echo $img_id;?>]" id="product_img[]" class="product_img  form-control" disabled></td>

      </tr>
<?php           
    $countrow++;
    }
?>
   </tbody>
   </table>
    </div><!-- /.span -->
    </div><!-- /.row -->
    <button type="submit" name="submit">Update</button>
</form>
</body>
</html>

UPDATE.php

<?php
error_reporting(E_ALL & ~E_NOTICE);
include ("dbconfig.php");
require_once('auth.php');

$product_img_name=$_FILES["product_img"]["name"];

if((isset($_POST['img_id'])))
        {
            $rand = rand(5,987897899);

            foreach($_POST["img_id"] as $imgId)
            {
                //$product_img_name= $_POST['product_img_name'][$imgId];
                $tmpName = $_FILES['product_img_name']['tmp_name'][$imgId];

                $imagetmp=addslashes (file_get_contents($tmpName));

                $name=basename($_FILES['product_img_name']['name'][$imgId]);
                $t_name=$_FILES['product_img']['tmp_name'][$imgId];
                $dir='upload';
                $new_name= date('dmY')."_".$product_code."_".$rand."_".$name;
                if(move_uploaded_file($t_name,$dir."/".$new_name))
                {
                    $query = mysql_query("UPDATE maray_product_img set product_img_name='$imagename',
                    product_img_path='upload/$imagename', product_img='$imagetmp' where img_id='$imgId' LIMIT 1");
                    echo "<script>alert ('Successfull! Data has been changed.')</script>";
                    echo "<script>window.location = 'test.php'</script>";

                }
                else
                {
                    echo "<script>alert ('Failed to update. Please try again')</script>";
                }

            }

        }

?>

希望专家可以帮助我。我是PHP的新手。我从其他人那里获得的一些代码。真的需要帮助。提前谢谢。

0 个答案:

没有答案