dourun2990 2016-07-27 13:47 采纳率: 100%
浏览 16

数据不会多次进入选择部分

when i click update button without any change in editing form..the fields of product category and product brand become empty... and in database the record at that place also become empty.please help me what i should do for this problem.

<?php
include("include/db.php");
if(isset($_GET['edit_pro'])){
    $get_id=$_GET['edit_pro'];
    $get_pro="select * from products where product_id='$get_id'";
    $run_pro=mysqli_query($db, $get_pro);

    $row_pro=mysqli_fetch_array($run_pro);
    $pro_id=$row_pro['product_id'];
    $pro_title=$row_pro['product_title'];
    $pro_image1=$row_pro['product_img1'];
    $pro_image2=$row_pro['product_img2'];
    $pro_image3=$row_pro['product_img3'];
    $pro_price=$row_pro['product_price'];
    $pro_desc=$row_pro['product_desc'];
    $pro_keywords=$row_pro['product_keywords'];
    $pro_cat=$row_pro['cat_id'];
    $pro_brand=$row_pro['brand_id'];

    $get_cat="select * from categories where cat_id='$pro_cat'";
    $run_cat=mysqli_query($db,$get_cat);
    $row_cat=mysqli_fetch_array($run_cat);
    $cat_title=$row_cat['cat_title'];

    $get_brand="select * from brands where brand_id='$pro_brand'";
    $run_brand=mysqli_query($db,$get_brand);
    $row_brand=mysqli_fetch_array($run_brand);
    $brand_title=$row_brand['brand_title'];
}

?>
<!DOCTYPE>
<html>
<head><title>update product</title>
<!--text editor taken from tinymce.com-->
<!--<script src="//cdn.tinymce.com/4/tinymce.min.js"></script>
  <script>tinymce.init({ selector:'textarea' });</script>-->
  <link rel="stylesheet" href="styles/stylesheet.css" media="all">
</head>
<body>
<form method="post" action="" enctype="multipart/form-data" align="center">
<center><table width="700" align="center">
<tr align="center" style="background-color:transparent;">
<td colspan="2"><h2>edit product</h2></td>
</tr>
<tr class="button_row">
<td align="right"><b>product title</b></td>
<td><input type="text" name="product_title" value="<?php echo $pro_title; ?>" size="40" placeholder="enter title" style="background-color:transparent; color:gray; border:1px solid black;"></td>
</tr>
<tr>
<td align="right"><b>product category</b></td>
<td>
<select name="product_cat" style="background-color:transparent; color:gray; border:1px solid black; opacity:0.6;">
<option value=""><?php echo $cat_title; ?></option>
<?php
    $get_cats="select * from categories";
    $run_cats=mysqli_query($db,$get_cats);
    while($row_cats=mysqli_fetch_array($run_cats)){
        $cat_id=$row_cats['cat_id'];
        $cat_title=$row_cats['cat_title'];
    echo "<option value='$cat_id'>$cat_title</option>";
    }
    ?>
    </select>
</td>
</tr>
<tr>
<td align="right"><b>product brand</b></td>
<td>
<select name="product_brand" style="background-color:transparent; color:gray; border:1px solid black; opacity:0.6;">
<option value=""><?php echo $brand_title; ?></option>
<?php
    $get_brands="select * from brands";
    $run_brands=mysqli_query($db,$get_brands);
    while($row_brands=mysqli_fetch_array($run_brands)){
        $brand_id=$row_brands['brand_id'];
        $brand_title=$row_brands['brand_title'];
    echo "<option value='$brand_id'>$brand_title</option>";
    }
    ?>
</select>
</td>
</tr>
<tr>
<td align="right"><b>product image 1</b></td>
<td><input type="file" name="product_img1" style="background-color:transparent; color:gray; opacity:0.7;"><img src="product_images/<?php echo $pro_image1; ?>" width="60" height="40"></td>
</tr>
<tr>
<td align="right"><b>product image 2</b></td>
<td><input type="file" name="product_img2" style="background-color:transparent; color:gray; opacity:0.7;"><img src="product_images/<?php echo $pro_image2; ?>" width="60" height="40"></td>
</tr>
<tr>
<td align="right"><b>product image 3</b></td>
<td><input type="file" name="product_img3" style="background-color:transparent; color:gray; opacity:0.7;"><img src="product_images/<?php echo $pro_image3; ?>" width="60" height="40"></td>
</tr>
<tr>
<td align="right"><b>product price</b></td>
<td><input type="text" name="product_price" value="<?php echo $pro_price; ?>" placeholder="enter price" style="background-color:transparent; color:gray; border:1px solid black;"></td>
</tr>
<tr>
<td align="right"><b>product description</b></td>
<td><textarea name="product_desc" cols="30" rows="1" placeholder="enter description" style="background-color:transparent; color:gray; border:1px solid black; outline: none;"><?php echo $pro_desc; ?></textarea></td>
</tr>
<tr>
<td align="right"><b>product keywords</b></td>
<td><input type="text" name="product_keywords" size="40" value="<?php echo $pro_keywords; ?>" placeholder="enter keywords" style="background-color:transparent; color:gray; border:1px solid black;"></td>
</tr>
<tr align="center" class="button_row">
<td colspan="2"><input type="submit" name="update" value="update product" style="background-color:transparent; width:120px; height:25px; font-size:15px; color:black; padding-bottom:10px; border:2px solid gray; text-align:center;"></td>
</tr>

</table></center>
</form>
</body>
</html>
<?php
if(isset($_POST['update'])){
    $update_id=$pro_id;
    $product_title=$_POST['product_title'];
    $product_cat=$_POST['product_cat'];
    $product_brand=$_POST['product_brand'];
    $product_price=$_POST['product_price'];
    $product_desc=$_POST['product_desc'];
    $status='on';
    $product_keywords=$_POST['product_keywords'];

    //image names
    $product_img1 = $_FILES['product_img1']['name'];
    $product_img2 = $_FILES['product_img2']['name'];
    $product_img3 = $_FILES['product_img3']['name'];

    //image temo names
    $temp_name1 = $_FILES['product_img1']['tmp_name'];
    $temp_name2 = $_FILES['product_img2']['tmp_name'];
    $temp_name3 = $_FILES['product_img3']['tmp_name'];


    move_uploaded_file($temp_name1,"product_images/$product_img1");
    move_uploaded_file($temp_name2,"product_images/$product_img2");
    move_uploaded_file($temp_name3,"product_images/$product_img3");

    if(!empty($_FILES['product_img1']['name']) AND 
       !empty($_FILES['product_img2']['name']) AND 
       !empty($_FILES['product_img3']['name']))
    {

        $update_product="update products set cat_id='$product_cat',
                                brand_id='$product_brand',
                                product_title='$product_title',
                                product_price='$product_price',
                                product_img1='$product_img1',
                                product_img2='$product_img2',
                                product_img3='$product_img3',
                                product_desc='$product_desc',
                                product_keywords='$product_keywords' 
                         where product_id='$update_id'";

    } else {
        $update_product="update products set cat_id='$product_cat',
                                brand_id='$product_brand',
                                product_title='$product_title',
                                product_price='$product_price',
                                product_desc='$product_desc',
                                product_keywords='$product_keywords' 
                         where product_id='$update_id'";

    }

    $run_update=mysqli_query($db, $update_product);
    if($run_update){
        echo "<script>alert('product has been updated')</script>";
        echo "<script>window.open('index.php?view_products','_self')   </script>";
    }
}


?>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 CSAPPattacklab
    • ¥15 一直显示正在等待HID—ISP
    • ¥15 Python turtle 画图
    • ¥15 stm32开发clion时遇到的编译问题
    • ¥15 lna设计 源简并电感型共源放大器
    • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)