Récupertion de donnée et modification

Fermé
medboy94 Messages postés 44 Date d'inscription jeudi 6 septembre 2012 Statut Membre Dernière intervention 14 mai 2016 - 14 mai 2016 à 11:32
DelNC Messages postés 2234 Date d'inscription samedi 25 octobre 2014 Statut Membre Dernière intervention 22 février 2020 - 14 mai 2016 à 23:54
Bonjour , j'ai crée un site-web de ecommerce j'ai un petit probléme quand je veux modifié un produit il récuppere tous lles donnée normal apar la category et la marque et quand je click sur update il me dise que le produit et modifié avec succes sauf qu'il se change rien . mercii pour votre aide .



<?php
 include_once('includes/connect_database.php'); 
 include_once('functions.php'); 
?>
<div id="content" class="container col-md-12">
 <?php 
 
  if(isset($_GET['id'])){
   $ID = $_GET['id'];
  }else{
   $ID = "";
  }
  
  // create array variable to store category data
  $category_data = array();
   
  $sql_query = "SELECT cat_id, cat_title 
    FROM categories 
    ORDER BY cat_id ASC";


    
  $stmt_category = $connect->stmt_init();
  if($stmt_category->prepare($sql_query)) { 
   // Execute query
   $stmt_category->execute();
   // store result 
   $stmt_category->store_result();
   $stmt_category->bind_result($category_data['cat_id'], 
    $category_data['cat_title']
    );
    
  }

  $marque_data = array();
   
  $sql_query = "SELECT marque_id, marque_title 
    FROM marques 
    ORDER BY marque_id ASC";

    
    
  $stmt_marque = $connect->stmt_init();
  if($stmt_marque->prepare($sql_query)) { 
   // Execute query
   $stmt_marque->execute();
   // store result 
   $stmt_marque->store_result();
   $stmt_marque->bind_result($marque_data['marque_id'], 
    $marque_data['marque_title']
    );
    
  }
   


   
  $sql_query = "SELECT product_image FROM products WHERE product_id = ?";
  
  $stmt = $connect->stmt_init();
  if($stmt->prepare($sql_query)) { 
   // Bind your variables to replace the ?s
   $stmt->bind_param('s', $ID);
   // Execute query
   $stmt->execute();
   // store result 
   $stmt->store_result();
   $stmt->bind_result($previous_product_image);
   $stmt->fetch();
   $stmt->close();
  }
  
  
  // get currency symbol from setting table
  $sql_query = "SELECT Value 
    FROM tbl_setting 
    WHERE Variable = 'Currency'";
  
  
  $stmt = $connect->stmt_init();
  if($stmt->prepare($sql_query)) { 
   // Execute query
   $stmt->execute();
   // store result 
   $stmt->store_result();
   $stmt->bind_result($currency);
   $stmt->fetch();
   $stmt->close();
  } 
  
  
  if(isset($_POST['btnEdit'])){
   
   $product_title = $_POST['product_title'];
   $product_category_id = $_POST['product_category_id'];
   $product_marque_id = $_POST['product_marque_id'];
   $product_price = $_POST['product_price'];
   $serve_for = $_POST['serve_for'];
   $product_description = $_POST['product_description'];
   $product_quantity = $_POST['product_quantity'];
   
   // get image info
   $product_image = $_FILES['product_image']['name'];
   $image_error = $_FILES['product_image']['error'];
   $image_type = $_FILES['product_image']['type'];
    
   // create array variable to handle error
   $error = array();
   
   if(empty($product_title)){
    $error['product_title'] = " <span class='label label-danger'>Required!</span>";
   }
    
   if(empty($product_category_id)){
    $error['$product_category_id'] = " <span class='label label-danger'>Required!</span>";
   }    
    
   if(empty($product_price)){
    $error['product_price'] = " <span class='label label-danger'>Required!</span>";
   }else if(!is_numeric($product_price)){
    $error['product_price'] = " <span class='label label-danger'>product_price in number!</span>";
   }

   if(empty($product_quantity)){
    $error['product_quantity'] = " <span class='label label-danger'>Required!</span>";
   }else if(!is_numeric($product_quantity)){
    $error['product_quantity'] = " <span class='label label-danger'>product_quantity in number!</span>";
   }
    
   if(empty($serve_for)){
    $error['serve_for'] = " <span class='label label-danger'>Not choosen</span>";
   }   

   if(empty($product_description)){
    $error['product_description'] = " <span class='label label-danger'>Required!</span>";
   }
   
   // common image file extensions
   $allowedExts = array("gif", "jpeg", "jpg", "png");
   
   // get image file extension
   error_reporting(E_ERROR | E_PARSE);
   $extension = end(explode(".", $_FILES["product_image"]["name"]));
   
   if(!empty($product_image)){
    if(!(($image_type == "image/gif") || 
     ($image_type == "image/jpeg") || 
     ($image_type == "image/jpg") || 
     ($image_type == "image/x-png") ||
     ($image_type == "image/png") || 
     ($image_type == "image/pjpeg")) &&
     !(in_array($extension, $allowedExts))){
     
     $error['product_image'] = "*<span class='label label-danger'>Image type must jpg, jpeg, gif, or png!</span>";
    }
   }
   
     
   if(!empty($product_title) && !empty($product_category_id) && !empty($product_price) && is_numeric($product_price) &&
    !empty($serve_for) && !empty($product_description) && empty($error['product_image']) && !empty($product_quantity) && is_numeric($product_quantity)){
    
    if(!empty($product_image)){
     
     // create random image file name
     $string = '0123456789';
     $file = preg_replace("/\s+/", "_", $_FILES['product_image']['name']);
     $function = new functions;
     $product_image = $function->get_random_string($string, 4)."-".date("Y-m-d").".".$extension;
    
     // delete previous image
     $delete = unlink("$previous_product_image");
     
     // upload new image
     $upload = move_uploaded_file($_FILES['product_image']['tmp_name'], 'upload/images/'.$product_image);
   
     // updating all data
     $sql_query = "UPDATE products 
       SET product_title = ? , product_category_id = ?, product_marque_id = ?,  product_price = ?, Serve_for = ?, product_image = ?, product_description = ?, product_quantity = ? 
       WHERE product_id = ?";
     
     $upload_image = 'upload/images/'.$product_image;
     $stmt = $connect->stmt_init();
     if($stmt->prepare($sql_query)) { 
      // Bind your variables to replace the ?s
      $stmt->bind_param('ssssssss', 
         $product_title, 
         $product_category_id, 
         $product_marque_id,
         $product_price, 
         $serve_for, 
         $upload_image,
         $product_description,
         $product_quantity,
         $ID);
      // Execute query
      $stmt->execute();
      // store result 
      $update_result = $stmt->store_result();
      $stmt->close();
     }
    }else{
     
     // updating all data except image file
     $sql_query = "UPDATE products 
       SET product_title = ? , product_category_id = ?, product_marque_id = ?,
       product_price = ?, Serve_for = ?, product_description = ?, product_quantity = ? 
       WHERE product_id = ?";
       
     $stmt = $connect->stmt_init();
     if($stmt->prepare($sql_query)) { 
      // Bind your variables to replace the ?s
      $stmt->bind_param('sssssss', 
         $product_title, 
         $product_category_id, 
         $product_marque_id,
         $product_price, 
         $serve_for, 
         $product_description,
         $product_quantity,
         $ID);
      // Execute query
      $stmt->execute();
      // store result 
      $update_result = $stmt->store_result();
      $stmt->close();
     }
    }
     
    // check update result
    if($update_result){
     $error['update_data'] = " <span class='label label-primary'>Success update</span>";
    }else{
     $error['update_data'] = " <span class='label label-danger'>failed update</span>";
    }
   }
   
  }
  
  // create array variable to store previous data
  $data = array();
   
  $sql_query = "SELECT * FROM products WHERE product_id = ?";
   
  $stmt = $connect->stmt_init();
  if($stmt->prepare($sql_query)) { 
   // Bind your variables to replace the ?s
   $stmt->bind_param('s', $ID);
   // Execute query
   $stmt->execute();
   // store result 
   $stmt->store_result();
   $stmt->bind_result($data['product_id'], 
     $data['product_title'], 
     $data['product_category_id'], 
     $data['product_marque_id'], 
     $data['product_price'], 
     $data['Serve_for'], 
     $data['product_quantity'],
     $data['product_description'],
     $data['product_image']
     );
   $stmt->fetch();
   $stmt->close();
  }
  
   
 ?>
 <div class="col-md-12">
 <h1>Edit Menu <?php echo isset($error['update_data']) ? $error['update_data'] : '';?></h1>
 <hr />
 </div>
 
 <form method="post" enctype="multipart/form-data">
 <div class="col-md-9">
  <div class="col-md-12">
   <label>Menu Name :</label><?php echo isset($error['product_title']) ? $error['product_title'] : '';?>
   <input type="text" name="product_title" class="form-control" value="<?php echo $data['product_title']; ?>"/>
  </div>

  <div class="col-md-3">
     <br>
     <label>product_price (<?php echo $currency;?>) :</label><?php echo isset($error['product_price']) ? $error['product_price'] : '';?>
  <input type="text" name="product_price" class="form-control" value="<?php echo $data['product_price'];?>"/>
  <br/>

  <label>Stock :</label><?php echo isset($error['product_quantity']) ? $error['product_quantity'] : '';?>
  <input type="text" name="product_quantity" class="form-control" value="<?php echo $data['product_quantity'];?>"/>
  <br/>

     <label>Status :</label><?php echo isset($error['serve_for']) ? $error['serve_for'] : '';?>
  <select name="serve_for" class="form-control">
   <option>Available</option>
   <option>Sold Out</option>
  </select>
  <br/>
     <label>Category :</label><?php echo isset($error['product_category_id']) ? $error['product_category_id'] : '';?>
  <select name="product_category_id" class="form-control">
   <?php while($stmt_category->fetch()){ 
    if($category_data['cat_id'] == $data['cat_id']){?>
     <option value="<?php echo $category_data['cat_id']; ?>" selected="<?php echo $data['cat_id']; ?>" ><?php echo $category_data['cat_title']; ?></option>
    <?php }else{ ?>
     <option value="<?php echo $category_data['cat_id']; ?>" ><?php echo $category_data['cat_title']; ?></option>
    <?php }} ?>
  </select>
  
     <br/>
  <label>Marque :</label><?php echo isset($error['product_marque_id']) ? $error['product_marque_id'] : '';?>
  <select name="product_marque_id" class="form-control">
   <?php while($stmt_marque->fetch()){ 
    if($marque_data['marque_id'] == $data['marque_id']){?>
     <option value="<?php echo $marque_data['marque_id']; ?>" selected="<?php echo $data['marque_id']; ?>" ><?php echo $marque_data['marque_title']; ?></option>
    <?php }else{ ?>
     <option value="<?php echo $marque_data['marque_id']; ?>" ><?php echo $marque_data['marque_title']; ?></option>
    <?php }} ?>
  </select>
  
     <br/>

  <label>Image :</label><?php echo isset($error['product_image']) ? $error['product_image'] : '';?>
  <input type="file" name="product_image" id="product_image"/><br />
  <img src="../admin/upload/images/<?php echo $data['product_image']; ?>" width="210" height="160"/>
  </div>

  <div class="col-md-9">
  <br>
  <label>product_description :</label><?php echo isset($error['product_description']) ? $error['product_description'] : '';?>
  <textarea name="product_description" id="product_description" class="form-control" rows="16"><?php echo $data['product_description']; ?></textarea>
  <script type="text/javascript" src="css/js/ckeditor/ckeditor.js"></script>
  <script type="text/javascript">                        
            CKEDITOR.replace( 'product_description' );
        </script>
  </div>
 </div>
  
 <div class="col-md-3">
 <br/>
  <div class="panel panel-default">
   <div class="panel-heading">Add</div>
    <div class="panel-body">
     <input type="submit" class="btn-primary btn" value="Update" name="btnEdit" />
    </div>
  </div>
 </div>
 </form>
 <div class="separator"> </div>
</div>

<?php 
 $stmt_category->close();
 include_once('includes/close_database.php'); ?>








A voir également:

1 réponse

DelNC Messages postés 2234 Date d'inscription samedi 25 octobre 2014 Statut Membre Dernière intervention 22 février 2020 2 002
Modifié par DelNC le 14/05/2016 à 23:57
Bonjour

Dans un des programmes que j'ai fait, il y a unexemple de modification de la fiche d'élément.

Partie 1
On sélectionne l'élément à modifier. Ici, on donne la référence (attention, c'est une clé primaire)
<?php             			
echo "<form method='post' action='modif_action.php'> \n";
echo "<table>\n";
echo "<tr><td> Réference du produit   </td>
	       <td><input  type='text'  name='ref_produit'></td></tr>\n";
echo "<tr><td></td>
	        <td><input  type='submit'></td></tr>	\n";
echo "</table> \n";
echo "</form> \n";
echo "<br/>\n";
?>


Partie 2
On récupère les informations sur l'élément qui à la ref_produit saisie
On est dans la page modif_action.php
<?php 
	if(isset($_REQUEST["ref_produit" ])) {$ref_produit = $_REQUEST["ref_produit" ];}
	else {$ref_produit = "" ;}
	
	$query      = "SELECT * ";
	$query     .= "FROM produits ";
	$query     .= "WHERE ref_produit = '$ref_produit' ";
	$query     .= "; ";
	$result = execute_query($query);	

while($row = mysql_fetch_array($result)) {

echo "<form method='post' action='modifi_result.php'>\n";
echo "<table>\n";
echo "<tr><td> nom des champs </td><td> valeurs actuelles </td>	<td> modifications </td></tr>\n";
echo "<tr><td> Référence </td><td >" . $row[0] . "</td><td><input  type='text' name='ref_produit' value='" . $row[0] . "'></td></tr> 	\n";
echo "<tr><td> Désignation </td><td>" . $row[1] . "</td><td><input  type='text' name='designation' value='" . $row[1] . "'></td></tr> 	\n";
echo "<tr><td> Prix </td><td>" . $row[2]  . "</td>	<td><input  type='text' name='prix' value='" . $row[ 2] . "'></td></tr> 	\n";
echo "<tr><td> </td><td></td> <td><input  type='submit'> </td></tr>	\n";
echo "</table>\n";
echo "</form>\n";
}//end while 
?>



Partie 3
On est dans la page modif_result.php

On recupère les corrections apportées
<?php
if(isset($_REQUEST["ref_produit" ])) {$ref_produit = $_REQUEST["ref_produit" ];}
else {$ref_produit = "" ;}

if(isset($_REQUEST["designation" ])) {$designationt = $_REQUEST["designation" ];}
else {$designation = "" ;}

if(isset($_REQUEST["prix" ])) {$prix = $_REQUEST["prix" ];}
else {$prix= "" ;}

//on realise la mise a jour dans la base de donnes
echo "<br/>\n";
$query      = "UPDATE  produits ";
$query     .= "SET ";
$query     .= 	" ref_produit =	'" . $ ref_produit . "',";
$query     .= 	" designation =	'" . $designation . "',";
$query     .= 	" prix =	'" . $prix . "',";
$query     .= "WHERE  ref_produit 	= '$ref_produit' ";
$query     .= "; ";
$result = execute_query($query);
?>
0