用户点击提交按钮时如何更新PHP表单?

时间:2013-08-23 12:01:46

标签: php html forms page-refresh

我有一个更新数据的表单。当我点击更新按钮时,它会更新数据库中的数据,但会在表单中显示旧数据,我必须再次刷新页面才能查看更新的数据。如果我更改页面的action,则会停止更新数据。

我希望更新页面在用户点击“提交”按钮时更新数据。

请告诉我解决这个问题的方法。

由于

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<script>


</script>
<link rel="stylesheet" type="text/css" href="stylesheet_dashboard.css"  />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Update Single Products</title>
</head>

<body>
<div id='Header'>
Logo
</div>
</body>
</html>

<?php
session_start();
if (isset ($_SESSION['username']) )
{

    //echo "<div id='nav'";
    echo "<ul><hr>
     <li><a href='insert_product.php' >Add Product  </a></li>   
     <li><a href='add_category.php'> Add Category </a></li> 
      <li><a href='add_sub_category.php'> Add Sub-Category </a></li>    
     <li><a href = 'view_products.php'  >View All Products</a>  </li>
     <li><a href = 'all_categories.php'  >View All Categories</a>  </li>
      <li><a href='view_all_sub_categories.php'>View All Sub Categories</a></li>

     </ul></hr>";



include 'connect.php';  
$id= $_GET['product_id'];
error_reporting(E_PARSE);
if (isset($id))
{
    //$order_id =array();


echo "<a href='view_products.php' >view Products </a>";

    /*  $select_query=  "select * from products Left join product_description 
        ON products.product_id=$id and product_description.product_id=$id";
        */
        /*
            $select_query=  "select * from products Left join product_description 
                    ON products.product_id=$id and product_description.product_id=$id";
            */
            /*
                                $select_query= "SELECT products.*,product_description.* FROM products
                                    INNER JOIN product_description
                        ON (products.product_id=$id AND product_description.product_id=$id 
                                )";     
                    */

                            $select_query=  "select * from products LEFT JOIN product_description 
                            ON products.product_id='".$id."' and 
                            product_description.product_id='".$id."' 
                            where products.product_id='".$id."'     ";

                    global $order_id;
                    global $i;      
                    global $location;

                        //  $image_form_query =     "select * from product_images where product_id= $id";
                        //  $image_form_run=        mysql_query($image_form_query);
                        //  $image_form_fetch=      mysql_fetch_array($image_form_run);
                        //  echo  'File     '.$image_from_fetch['name'];






            if(!$select_query_run=   mysql_query($select_query))
            {
                echo mysql_error();

                    }

            else

            {
                $product_images         ="select * from product_images where product_id=$id";
                $product_images_run     =mysql_query($product_images);


                while ($product_images_fetch    =mysql_fetch_array($product_images_run))
                {
                $location[]             =$product_images_fetch['images'];                       
                }




                $select_query_run2=  mysql_query($select_query);
                $fetch =     mysql_fetch_array ($select_query_run2);        
                 $fetch['product_id'];


                echo "  
                <form action='update_single_product.php?product_id=$id' method='POST'
                 enctype='multipart/form-data' >
                <table border=1>
                <tr>
                <td>

                <label>Product Name:</label> </td>  <td><input type='text' 
                name='product_name' value='".$fetch['name']."'  />*Required</td></tr>


                <tr><td><label>Item No:</label></td> <td><input type='text' 
                                name='item_no' value='".$fetch['item_no']."'></td></tr>


                <tr><td>Recipient   </td>   <td> <input type='text'
                                 name='recipient' value='".$fetch['recipient']."'  ></td></tr> 


                <tr><td>    Total Carat Weight</td> <td><input type='text' 
                                name= 'total_carat_weight' value='".$fetch['total_carat_weight']."' ></td></tr>


                <tr><td>    Metal </td><td><input type='text'
                                 name='metal'   value='".$fetch['metal']."' ></td></tr>


                <tr><td>    Stone Shape </td><td><input type='text'
                                 name='stone_shape' value='".$fetch['stone_shape']."' ></td></tr>


                <tr><td>    Stone Type</td><td> <input type='text'
                                 name='stone_type'  value='".$fetch['stone_type']."'></td></tr> 


                <tr><td>    Stone Setting</td><td> <input type='text'
                                 name='stone_setting'   value='".$fetch['stone_setting']."'></td></tr> 


                <tr><td>    Wastage </td><td><input type='text' 
                                name='wastage'  value='".$fetch['wastage']."'></td></tr></br></br>


                <tr><td>    Retail_price </td><td><input type='text'
                                 name='retail_price' value='".$fetch['retail_price']."'></td></tr>


                <tr><td>    Actual Price: </td><td> <input type= 'text'
                                 name= 'actual_price' value='".$fetch['actual_price']."' /></td></tr>*Required


                <tr><td>    Description:</td><td><input type='text'
                                 name='description'  value='".$fetch['description']."' /></td></tr>


                <tr><td>    Image1:</td><td> <input type='file'
                                 name= 'files[]' > 
                                 <img src='$location[0]' height='60' width='60' >   </td></tr> *Required


                <tr><td>    Image2:</td><td> <input type='file' 
                                name= 'files[]' >
                                <img src=  '$location[1]' height='60' width='60'>
                                </td></tr>


                <tr><td>    Image3:</td><td> <input type='file'
                                 name= 'files[]' >
                                 <img src = '$location[2]' height='60' width='60'>
                                 </td></tr></table> ";


    /*------------------
    Drop Down List Start
    ------------------  */      


                echo "<select name='category'>";

                    $select_query=          'Select * from category';
                    $select_query_run =     mysql_query($select_query);

                    $sub_category_query=    "Select * from sub_categories";
                    $sub_query_run=         mysql_query($sub_category_query);



                while   ($select_query_array=   mysql_fetch_array($select_query_run) )
                {

                            echo "<option value='".$select_query_array['category_id']."' >".
                            htmlspecialchars($select_query_array["name"]).

                            "<option value='".$sub_query_run['sub_category_id']."'  >" .
                            htmlspecialchars($sub_query_run['sub_category_name']).   "</option>".

                    "</option>";




                }

                echo "</br>";

                 $selectTag= "</br><input type='submit' value='Update Product'  /></select></form>";



                 echo "</div></div>";

                 echo $selectTag;


                     //NEW $_POST ADDING 
                     //To Avoid Update Problem

                 if (isset($_POST['product_name']) || isset ($_POST['item_no']) || isset ($_POST['recipient'])
                || isset($_POST['total_carat_weight']) || isset ($_POST['metal']) || isset($_POST['stone_shape'])
                || isset($_POST['stone_setting']) || isset($_POST['wastage'])  || isset($_POST['retail_price'])
                ||  isset($_POST['actual_price']) && isset($_POST['description'])   )
                     {



    /*-----------------
    Drop Down List End
    ------------------*/    

                                $product_name =         $_POST['product_name'];
                                $item_no=               $_POST['item_no'];
                                $recipient=             $_POST['recipient'];
                                $total_carat_weight=    $_POST['total_carat_weight'];
                                $metal=                 $_POST['metal'];
                                $stone_shape=           $_POST['stone_shape'];
                                $stone_type=            $_POST['stone_type'];
                                $stone_setting=         $_POST['stone_setting'];
                                $wastage=               $_POST['wastage'];
                                $retail_price=          $_POST['retail_price'];
                                $actual_price=          $_POST['actual_price'];
                                $description=           $_POST['description'];



                                $query= "Update products set name='".$product_name."'  
                                where product_id=$id";

                                $query2="Update product_description set recipient='".$recipient."' ,
                                        total_carat_weight='".$total_carat_weight."', metal='".$metal."',
                                        stone_shape='".$stone_shape."' , stone_type='".$stone_type."',
                                        stone_setting='".$stone_setting."', wastage='".$wastage."',
                                        retail_price='".$retail_price."',
                                         actual_price='".$actual_price. "',
                                         description='".$description."'
                                        where product_id=$id ";

                                        $insert_query=  "insert into product_description 
                                                VALUES($id,'$item_no','$recipient',
                                                '$total_carat_weight','$metal','$stone_shape',
                                                '$stone_type','$stone_setting', '$wastage',
                                                '$retail_price','$price' ,'$description'    ) ";

                                                $insert_query_run=  mysql_query($insert_query);

                                $query_run= mysql_query($query); 
                                $query_run2= mysql_query($query2) ;


                                /*-----------------
                                    IMAGE QUERY 2
                                ------------------*/
                            /*      

                            if (isset($_FILES['files'])
                            ||  ($_FILES["files"]["type"]   == "image/jpeg"))

                            {

                                foreach($_FILES['files']['tmp_name'] as $key=> $tmp_name)
                                    {

                                        //echo $tmp_name."<br>";

                                        echo 'number<br>';
                                        echo    $image_name=        $_FILES["files"]["name"][$key];  

                                        $random_name=       rand().$_FILES["files"]["name"][$key];

                                        $folder="upload/products/" .$random_name;                       

                                        move_uploaded_file($_FILES["files"]["tmp_name"][$key],
                                                    "upload/products/" . $random_name);




                                echo '<br>';                            
                                echo    $sql= "update product_images set name= '$random_name', 
                                            images= '$folder' where product_id= $id ";                              

                                                if ($query_run= mysql_query($sql))
                                                {

                                                    echo '<br>';
                                                    echo 'Done';
                                                    }

                                                    else
                                                    {
                                                        echo mysql_error();
                                                        }


                            }

                        }

                            */                                                                              
                                    /*-----------------
                                     IMAGE QUERY 2- END
                                    ------------------*/
                        /*
                        echo "Print Arrray </br>";
                        print_r ($_FILES['files']['name']);         
                        echo "Print Arrray </br>";

                        */
                                    /*-------------------
                                    IMAGE-QUERY Test 002
                                    --------------------*/
            $allowedExts = array("gif", "jpeg", "jpg", "png");
            $extension = end(explode(".", $_FILES["files"]["name"]));



                            if (isset($_FILES['files'])
                            ||  ($_FILES["files"]["type"]   == "image/jpeg")
                                    && in_array($extension, $allowedExts))

                            {
                                //$i=1;
                                    echo "Printing Number Count </br>";
                                    echo count($_FILES['files']);
                                    echo "<br>Counting End";

                                    //print_r($_FILES['files']['name']);
                                    //echo "$_FILES['files']['name']";      
        /*-----------------------
        Taking Current Order_id
        ------------------------*/

        //$order_sql=           "select MAX(ord) from product_images";

            $order_sql=         "SELECT ord from product_images where product_id=$id";                      


            $order_sql_run=     mysql_query($order_sql);


            echo mysql_error();                 
            //UNDOOOO ooo OOo oooOO oOo oOOOoo OooO oOo
            if ($_FILES['files']['name']=="")
            {
                echo "No Update";   
                    }

                    else
                        {   


$uploaded_files = array_filter($_FILES['files'], function($file){ 
    return $file['size']; 
});
echo "Stack    ";
print count($uploaded_files);
echo "Stack    ";            


        //  for($i=0; $i<=  $order_fetch=mysql_fetch_array($order_sql_run) ; $i++)  
        for($i=0; $i<=  $order_fetch=mysql_fetch_array($order_sql_run) ; $i++)  


            // while ($order_fetch=     mysql_fetch_array($order_sql_run))
             {              


                //echo  'ID '.          $order_id[$i]=$order_fetch[(ord)] ;
                                        $order_id[$i]=$order_fetch[(ord)] ;

                                        //  foreach($_FILES['files']['tmp_name'] as $key=> $tmp_name)
                                //  {
                                        //$image_sql= "select image_id from product_images where ";




                                        //echo $tmp_name."<br>";

                                        //echo 'number<br>';
                                    //  echo    $image_name=        $_FILES["files"]["name"][$key];  

                                        $image_name=        $_FILES["files"]["name"][$i];                                       

                                    //Old Method Of Taking Name
                                        $random_name=       rand().$_FILES["files"]["name"][$i];


                                    //New Method of taking Name
                            /*      
                                $image_extension=           $_FILES["files"]["name"];                               
                                $image_extension_pos=       strpos($image_extension,'.');
                        echo    "ImageExtension ".  $image_extension_substr=    
                        substr($image_extension,$image_extension_pos,5);
                                */      

                                        //$random_name=     rand().$i;

                                        $random_name=       rand().$_FILES["files"]["name"][$i];

                                        $folder="upload/products/" .$random_name;                       

                                        move_uploaded_file($_FILES["files"]["tmp_name"][$i],
                                                    "upload/products/" . $random_name);


                                //print_r ($order_id);

                    /*          
                        echo    'Val1   '.  $val1=   $order_id[1];
                        echo    'Val2   '.  $val2=   $order_id[2];
                        echo    'Val3   '.  $val3=   $order_id[3];
                        */                          
                                echo '<br>';    




                                echo    $sql= "update product_images set name= '$random_name', 
                                            images= '$folder' where product_id= $id and 
                                            ord=$order_id[$i] ";        

                            /*  
                            echo    $sql= "update product_images set name= '$random_name', 
                                            images= '$folder' where product_id= $id and 
                                            ord=$val1 ";

                        echo        $sql2= "update product_images set name= '$random_name', 
                                            images= '$folder' where product_id= $id and 
                                            ord=$val2 ";

                        echo        $sql3= "update product_images set name= '$random_name', 
                                            images= '$folder' where product_id= $id and 
                                            ord=$val3 ";
                                */          
                            //  mysql_query($sql2);
                            //  mysql_query($sql3);         


                                                if ($query_run= mysql_query($sql))
                                                {
                                                    //mysql_query ($sql2);

                                                    //mysql_query ($sql3);                                                  
                                                    echo '<br>';
                                                    echo 'Done';
                                                    header ('Location:view_products.php ');
                                                    }

                                                    else
                                                    {
                                                        echo mysql_error();
                                                        }
                                        }


                                //i=$i+1;
                            }

                        //}


                                    /*-------------------
                                    IMAGE-QUERY Test 002
                                    --------------------*/








                }
                //echo 'Printing Array<br>';






        /*-----------------------
        Taking Current Order_id
        ------------------------*/                  




                                if($query_run && $query2)
                                {


                                    echo "Records have been inserted. Please go to 
                                                <a href='view_products.php'>View Products page</a>to see the Updates";

                                    }

                                    elseif (mysql_affected_rows()==1)
                                    {
                                            $insert_query=  "insert into product_description 
                                                VALUES($id,'$item_no','$recipient',
                                                '$total_carat_weight','$metal','$stone_shape',
                                                '$stone_type','$stone_setting', '$wastage',
                                                '$retail_price','$price' ,'$description'    ) ";

                                            if ($insert_query_run=  mysql_query($insert_query))
                                            {
                                                echo "<br> Insert Query Runs!!! </br>"; 
                                                }
                                                else
                                                {
                                                    echo mysql_error();
                                                    }

                                        }

                                    else
                                    {
                                        echo mysql_error()."Please fill the Required Fields";
                                        }
                     }

    }
}
else
{
    echo "Invalid Product! Go back to";
    echo "<a href='dashboard.php'> Dashboard</a>";
    }
}

else
{
    echo "You Must need to Log in To Visit this Page!";

    }
?>

3 个答案:

答案 0 :(得分:0)

如果您要在同一页面上发布数据,请尝试关注。

<input type='text' value='<?php echo $_POST['field_name']; ?>' name='field_name' />

同样明智的你可以用于所有输入。

您也可以使用AJAX

答案 1 :(得分:0)

我假设您有一个处理更新的页面?在更新之后,你应该想要回到header('Location:');页面。这应该可以解决你的问题。

答案 2 :(得分:0)

Please follow this link,已经在这里回答了

我建议你使用AJAX调用函数

<script type="text/javascript">
function updatePage() {
    $.get("your php file name here", function() {
      location.reload();
    });
}
</script>