简体   繁体   English

无法使用PHP表单编辑表中的数据

[英]unable to edit data in table using PHP form

Please look into this code unable to find out the actual error: 请调查以下代码,无法找出实际错误:

This is PHP upload code: 这是PHP上传代码:

<?php
            include_once("config.php");
            if(isset($_GET['pro_id']))
            {
            $id=$_GET['pro_id'];
            if(isset($_POST['submitBtn']))          {
            $dept_id = $_POST['dept_id'];
            $cat_id = $_POST['cat_id'];
            /*$pro_id = $_POST['pro_id'];*/
            $pro_name = $_POST['pro_name'];
            $pro_desc = $_POST['pro_desc'];
            $pro_spec = $_POST['pro_spec'];
            $pro_price = $_POST['pro_price'];
            $status = 'on';
            $pro_keywords = $_POST['pro_keywords'];
            //image names
            $pro_image = $_FILES['pro_image']['name'];
            //temp images names
            $temp_image = $_FILES['pro_image']['tmp_name'];

            if($dept_id=='' OR $cat_id=='' OR $pro_name=='' OR $pro_desc=='' OR $pro_spec=='' OR $pro_price=='' OR  $pro_image=='' OR $pro_keywords=='')
                    {
            echo "<script>alert('All the fields are mandatory')</script>";
            exit();
                    }
            else
            {
            //upload image to folder
            move_uploaded_file($temp_image,"images/product_images/$pro_image");
            $run_query1 = mysqli_query($login, "update products1 SET (dept_id,cat_id,pro_name,pro_desc,pro_spec,pro_price,pro_image,status,date,pro_keywords) values (  '$dept_id','$cat_id','$pro_name','$pro_desc','$pro_spec','$pro_price','$pro_image','$status','NOW()','$pro_keywords' WHERE pro_id='$id'");

            if($run_query1)
                    {
                echo "<script>alert('Product updated successfully')</script>";
                exit();     
                    }
            else
                {
                echo "<script>alert('Errors')</script>";
                }
            }           }

            $query1 = mysqli_query($login, "select * from products1 where pro_id='$id'");
            $query2 = mysqli_fetch_array($query1);
            ?>

This the form Part where data retrieve from table and when click on the update button nothing happened and page is redirected to view data page and showing the old data: 这是表单的一部分,从表中检索数据,并且单击更新按钮时什么也没有发生,并且页面被重定向到查看数据页面并显示旧数据:

<form action="ViewProduct.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
            <table width="650" border="0">

            <tr>
                <td width="183" align="right">Department:</td>
                <th width="231" align="left">
                <select name="dept_id" id="dept_id">
                    <option>Select Department</option>
                 <?php
                $result=dept_show();

                while($row=mysqli_fetch_assoc($result))
                {
                    echo "<option value='{$row['dept_id']}'>{$row['dept_name']}</option>";
                }                    
                ?>
                </select></th></tr>
              <tr>
                <td width="183" align="right">Catagory</td>
                <th width="231" align="left">
                <select name="cat_id" id="cat_id">
                    <option>Select Catagory</option>
                <?php
                $result1=cat_show();

                while($row=mysqli_fetch_assoc($result1))
                {
                    echo "<option value='{$row['cat_id']}'>{$row['cat_name']}</option>";
                }                    
                ?>
                </select></th></tr>
              <tr>
                <!--<td width="231"><input type="hidden" name="pro_id" id="pro_id" value="<t?php echo $pro_id; ?>" /></td>-->
              </tr>
              <tr>
                <td align="right">Product Name/Model:</td>
                <td><input type="text" name="pro_name" id="pro_name" value="<?php echo $query2['pro_name']; ?>" /></td>
              </tr>
              <tr>
                <td align="right">Product Description:</td>
                <td><textarea type="textarea" name="pro_desc" id="pro_desc" cols="45" rows="5"><?php echo $query2['pro_desc']; ?></textarea></td>
              </tr>
              <tr>
                <td align="right">Products Specification:</td>
                <td><textarea type="textarea" name="pro_spec" id="pro_spec" cols="45" rows="5"><?php echo $query2['pro_spec']; ?></textarea></td>
              </tr>
              <tr>
                <td align="right">Product Price:</td>
                <td><input type="text" name="pro_price" id="pro_price" value="<?php echo $query2['pro_price']; ?>" /></td>
              </tr>
              <tr>
                <td align="right">Product Image:</td>
                <td><input type="file" name="pro_image" id="pro_image" value="<?php echo $query2['pro_image']; ?>" /></td>
                </tr>
                <tr>
                <td></td>
                <td><input size="45" type="text" name="text" id="text" value="<?php echo $query2['pro_image']; ?>" /></td>
              </tr>
              <tr>
                <td align="right">Keywords:</td>
                <td><input size="45" type="text" name="pro_keywords" id="pro_keywords" value="<?php echo $query2['pro_keywords']; ?>" /></td>
              </tr>
              <tr>
                <td colspan="2" align="center"><input type="submit" name="submitBtn" id="submit" value="Update" /></td>
              </tr>
            </table>
          </form>
            </div> <?php } ?>
        </td>
        </tr>
    </table>
  </div>

Form method is POST and you are using GET method in if loop 表单方法是POST,并且您在if循环中使用GET方法

if(isset($_GET['pro_id']))

Use POST here. 在此处使用POST。

I have made the changes in you complete code. 我已经在您完整的代码中进行了更改。 Use this code and if required made the changes (if issues arrived) 使用此代码,并根据需要进行更改(如果问题已到达)

PHP code PHP代码

<?php
        include_once("config.php");

        if(isset($_POST['submitBtn']))          
        {
        $dept_id = $_POST['dept_id'];
        $cat_id = $_POST['cat_id'];
        $pro_id = $_POST['pro_id'];*/
        $pro_name = $_POST['pro_name'];
        $pro_desc = $_POST['pro_desc'];
        $pro_spec = $_POST['pro_spec'];
        $pro_price = $_POST['pro_price'];
        $status = 'on';
        $pro_keywords = $_POST['pro_keywords'];
        //image names
        $pro_image = $_FILES['pro_image']['name'];
        //temp images names
        $temp_image = $_FILES['pro_image']['tmp_name'];

        if($dept_id=='' OR $cat_id=='' OR $pro_name=='' OR $pro_desc=='' OR $pro_spec=='' OR $pro_price=='' OR  $pro_image=='' OR $pro_keywords=='')
                {
        echo "<script>alert('All the fields are mandatory')</script>";
        exit();
                }
        else
        {
        //upload image to folder
        move_uploaded_file($temp_image,"images/product_images/$pro_image");
        $run_query1 = mysqli_query($login, "update products1 SET (dept_id,cat_id,pro_name,pro_desc,pro_spec,pro_price,pro_image,status,date,pro_keywords) values (  '$dept_id','$cat_id','$pro_name','$pro_desc','$pro_spec','$pro_price','$pro_image','$status','NOW()','$pro_keywords' WHERE pro_id='$id'");

        if($run_query1)
                {
            echo "<script>alert('Product updated successfully')</script>";
            exit();     
                }
        else
            {
            echo "<script>alert('Errors')</script>";
            }
        }     
    }

        $query2 = array();
        if(isset($_GET['pro_id']))
        {
            $id=$_GET['pro_id'];
            $query1 = mysqli_query($login, "select * from products1 where pro_id='$id'");
            $query2 = mysqli_fetch_array($query1);
        }            
        ?>

HTML Code HTML代码

<form action="ViewProduct.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
        <table width="650" border="0">

        <tr>
            <td width="183" align="right">Department:</td>
            <th width="231" align="left">
            <select name="dept_id" id="dept_id">
                <option>Select Department</option>
             <?php
            $result=dept_show();

            while($row=mysqli_fetch_assoc($result))
            {
                echo "<option value='{$row['dept_id']}'>{$row['dept_name']}</option>";
            }                    
            ?>
            </select></th></tr>
          <tr>
            <td width="183" align="right">Catagory</td>
            <th width="231" align="left">
            <select name="cat_id" id="cat_id">
                <option>Select Catagory</option>
            <?php
            $result1=cat_show();

            while($row=mysqli_fetch_assoc($result1))
            {
                echo "<option value='{$row['cat_id']}'>{$row['cat_name']}</option>";
            }                    
            ?>
            </select></th></tr>
          <tr>
            <td width="231"><input type="hidden" name="pro_id" id="pro_id" value="<t?php echo $pro_id; ?>" /></td>
          </tr>
          <tr>
            <td align="right">Product Name/Model:</td>
            <td><input type="text" name="pro_name" id="pro_name" value="<?php echo $query2['pro_name']; ?>" /></td>
          </tr>
          <tr>
            <td align="right">Product Description:</td>
            <td><textarea type="textarea" name="pro_desc" id="pro_desc" cols="45" rows="5"><?php echo $query2['pro_desc']; ?></textarea></td>
          </tr>
          <tr>
            <td align="right">Products Specification:</td>
            <td><textarea type="textarea" name="pro_spec" id="pro_spec" cols="45" rows="5"><?php echo $query2['pro_spec']; ?></textarea></td>
          </tr>
          <tr>
            <td align="right">Product Price:</td>
            <td><input type="text" name="pro_price" id="pro_price" value="<?php echo $query2['pro_price']; ?>" /></td>
          </tr>
          <tr>
            <td align="right">Product Image:</td>
            <td><input type="file" name="pro_image" id="pro_image" value="<?php echo $query2['pro_image']; ?>" /></td>
            </tr>
            <tr>
            <td></td>
            <td><input size="45" type="text" name="text" id="text" value="<?php echo $query2['pro_image']; ?>" /></td>
          </tr>
          <tr>
            <td align="right">Keywords:</td>
            <td><input size="45" type="text" name="pro_keywords" id="pro_keywords" value="<?php echo $query2['pro_keywords']; ?>" /></td>
          </tr>
          <tr>
            <td colspan="2" align="center"><input type="submit" name="submitBtn" id="submit" value="Update" /></td>
          </tr>
        </table>
      </form>
        </div> <?php } ?>
    </td>
    </tr>
</table>

Your pro_id field is commented out in your HTML using <!-- and --> , so the following never is true: 您的pro_id字段使用<!---->在HTML中被注释掉,因此以下内容永远都不成立:

if(isset($_GET['pro_id']))

Also, you have a mismatch between your form method POST and $_GET that you are looking for. 另外,您在查找的表单方法POST和$ _GET之间不匹配。

Your query of update is correct? 您的更新查询正确吗? I think you must use 我认为你必须使用

UPDATE products1 SET dept_id='$dept_id',cat_id ='$cat_id'... the rest of values
WHERE pro_id='$id'

And verify if your dept_id is INT as well cat_id, so if they are INT you don't need '' 并验证您的dept_id和cat_id是否也是INT,因此如果它们是INT,则不需要''

UPDATE products1 SET dept_id=$dept_id,cat_id =$cat_id

Try to do this steps: 尝试执行以下步骤:

First thing comment out this line, 首先把这行注释掉,

<!--<td width="231"><input type="hidden" name="pro_id" id="pro_id" value="<t?php echo $pro_id; ?>" /></td>-->   

next step, you are sending data using POST ie (form method="post"), so use this 下一步,您正在使用POST发送数据,即(form method =“ post”),因此请使用

if(isset($_POST['pro_id'])) , then comment out $pro_id = $_POST['pro_id']; 

you will get $pro_id value. 您将获得$ pro_id值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM