Probleme d'ajout de donnée dans BDD
Résolu/Fermé
medboy94
Messages postés
44
Date d'inscription
jeudi 6 septembre 2012
Statut
Membre
Dernière intervention
14 mai 2016
-
11 mai 2016 à 18:04
medboy94 Messages postés 44 Date d'inscription jeudi 6 septembre 2012 Statut Membre Dernière intervention 14 mai 2016 - 12 mai 2016 à 18:30
medboy94 Messages postés 44 Date d'inscription jeudi 6 septembre 2012 Statut Membre Dernière intervention 14 mai 2016 - 12 mai 2016 à 18:30
A voir également:
- Probleme d'ajout de donnée dans BDD
- Comment enlever la limite d'ajout sur snapchat - Forum Snapchat
- Ajout rapide snap - Forum Snapchat
- Ajouter agenda outlook dans google agenda - Guide
- Ignoré de ils m'ont ajouté snap ✓ - Forum Snapchat
- Reinstaller windows sans perte de données - Guide
1 réponse
jordane45
Messages postés
38314
Date d'inscription
mercredi 22 octobre 2003
Statut
Modérateur
Dernière intervention
24 novembre 2024
4 705
Modifié par jordane45 le 11/05/2016 à 18:23
Modifié par jordane45 le 11/05/2016 à 18:23
Bonjour,
Déjà ... On place le code PHP ... AVANT TOUT LE HTML.
Ca le rend plus simple à lire ....
Voici ton code remis en forme un peu mieux....
Ensuite.....
Vires moi tous ces
et utilises donc des bloc TRY / CATCH !!
par exemple :
Cordialement,
Jordane
Déjà ... On place le code PHP ... AVANT TOUT LE HTML.
Ca le rend plus simple à lire ....
Voici ton code remis en forme un peu mieux....
<?php //Affichage des erreurs PHP '(à mettre au DEBUT de ta page php ! ) error_reporting(E_ALL); // Connexion à la BDD + Fichier de fonctions include_once('includes/connect_database.php'); include_once('functions.php'); $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 $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'] ); } // 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(); } //$max_serve = 10; // RECUPERATION PROPRE DES VARIABLES AVANT DE LES UTILISER $product_title = isset($_POST['product_title']) ? $_POST['product_title'] : NULL; $product_category_id = isset($_POST['category_ID']) ? $_POST['category_ID'] : NULL; $product_marque_id = isset($_POST['product_marque_id']) ? $_POST['product_marque_id'] : NULL; $product_price = isset($_POST['product_price']) ? $_POST['product_price'] : NULL ; $serve_for = isset($_POST['serve_for']) ? $_POST['serve_for']: NULL; $product_description = isset($_POST['product_description']) ? $_POST['product_description'] : NULL; $product_quantity = isset($_POST['product_quantity']) ? $_POST['product_quantity'] : NULL; // get image info $product_image = !empty($_FILES['product_image']['name']) ? $_FILES['product_image']['name'] : NULL; $image_error = !empty($_FILES['product_image']['error']) ? $_FILES['product_image']['error'] : NULL; $image_type = !empty($_FILES['product_image']['type']) ? $_FILES['product_image']['type'] : NULL; //Traitement du submit if(isset($_POST['btnAdd'])){ // 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_marque_id)){ $error['product_marque_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 $extension = end(explode(".", $_FILES["product_image"]["name"])); if($image_error > 0){ $error['product_image'] = " <span class='label label-danger'>Not uploaded!</span>"; }else 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_marque_id) && !empty($product_price) && is_numeric($product_price) && !empty($serve_for) && empty($error['product_image']) && !empty($product_description) && !empty($product_quantity) && is_numeric($product_quantity)){ // 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; // upload new image $upload = move_uploaded_file($_FILES['product_image']['tmp_name'], 'upload/images/'.$product_image); // insert new data to menu table $sql_query = "INSERT INTO products (product_title, product_category_id,product_marque_id, product_price, Serve_for, product_image, product_description, product_quantity) VALUES(?, ?, ?, ?, ?, ?, ?, ?)"; $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('sssssss', $product_title, $product_category_id, $product_marque_id, $product_price, $serve_for, $upload_image, $product_description, $product_quantity ); // Execute query $stmt->execute(); // store result $result = $stmt->store_result(); $stmt->close(); } if($result){ $error['add_menu'] = " <span class='label label-primary'>Success Added</span>"; }else { $error['add_menu'] = " <span class='label label-danger'>Failed</span>"; } } } ?> <!-- Le HTML --> <div id="content" class="container col-md-12"> <div class="col-md-12"> <h1>Add Menu <?php echo isset($error['add_menu']) ? $error['add_menu'] : '';?></h1> <hr /> </div> <div class="col-md-12"> <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" class="form-control" name="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" class="form-control" name="product_price"/> <br/> <label>Stock :</label><?php echo isset($error['product_quantity']) ? $error['product_quantity']:'';?> <input type="text" class="form-control" name="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="category_ID" class="form-control"> <?php while($stmt_category->fetch()){ ?> <option value="<?php echo $category_data['cat_id']; ?>"><?php echo $category_data['cat_title']; ?></option> <?php } ?> </select> <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()){ ?> <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"/> </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"></textarea> <script type="text/javascript" src="css/js/ckeditor/ckeditor.js"></script> <script type="text/javascript"> CKEDITOR.replace( 'product_description' ); </script> </div> </div> <br/> <div class="col-md-3"> <div class="panel panel-default"> <div class="panel-heading">Add</div> <div class="panel-body"> <input type="submit" class="btn-primary btn" value="Add" name="btnAdd" /> <input type="reset" class="btn-danger btn" value="Clear"/> </div> </div> </div> </form> </div> <div class="separator"> </div> </div> <?php $stmt_category->close(); include_once('includes/close_database.php'); ?>
Ensuite.....
Vires moi tous ces
if($stmt->prepare($sql_query)) {
et utilises donc des bloc TRY / CATCH !!
par exemple :
<?php $upload_image = 'upload/images/'.$product_image; try{ // insert new data to menu table $sql = "INSERT INTO products (product_title, product_category_id,product_marque_id, product_price, Serve_for, product_image, product_description, product_quantity) VALUES(?, ?, ?, ?, ?, ?, ?, ?)"; $a_datas = array('sssssss', $product_title, $product_category_id, $product_marque_id, $product_price, $serve_for, $upload_image, $product_description, $product_quantity ); $stmt = $connect->stmt_init(); $prepare = $stmt->prepare($sql_query) { // Execute query $result = $prepare->execute($a_datas); $stmt->close(); }catch(Exception $e){ echo "<br><b>Erreur !</b>"; echo "<br><b>Requete SQL :</b> ".$sql; echo "<br><b>a_datas = </b><br>"; print_r($a_datas); }
Cordialement,
Jordane
11 mai 2016 à 22:14
" Number of elements in type definition string doesn't match number of bind variables "
merci pour votre réponse .
11 mai 2016 à 23:21
11 mai 2016 à 23:39
12 mai 2016 à 00:01
12 mai 2016 à 18:26