Undefined index

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 à 17:21
medboy94 Messages postés 44 Date d'inscription jeudi 6 septembre 2012 Statut Membre Dernière intervention 14 mai 2016 - 11 mai 2016 à 17:59
Bonjour , j'ai cette erreur Undefined index: product_marque_id mercii


<?php
	include_once('includes/connect_database.php'); 
	include_once('functions.php'); 
?>
<div id="content" class="container col-md-12">
	<?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;
			
	if(isset($_POST['btnAdd'])){
			$product_title = $_POST['product_title'];
			$product_category_id = $_POST['category_ID'];
			$product_marque_id = $_POST['product_marque_id']; /* erreur*/
			$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($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_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>";
				}
			}
				
			}
	?>
	<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="category_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'); ?>

1 réponse

Pitet Messages postés 2826 Date d'inscription lundi 11 février 2013 Statut Membre Dernière intervention 21 juillet 2022 525
11 mai 2016 à 17:25
Salut,

Le paramètre $_POST['product_marque_id'] n'est pas définie.
Vérifie que ton formulaire possède bien un input dont l'attribut name est égal à product_marque_id.

Bonne journée,
2
medboy94 Messages postés 44 Date d'inscription jeudi 6 septembre 2012 Statut Membre Dernière intervention 14 mai 2016
11 mai 2016 à 17:42
salut , mercii pour votre réponse ca marche mtn mais il ajoute pas le produits dans la base de donnée .
0
jordane45 Messages postés 38304 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 23 novembre 2024 4 705
11 mai 2016 à 17:58
Bonjour,

Et au cas où : (histoire d'être le plus propre possible ) :
https://forums.commentcamarche.net/forum/affich-37636387-php-notice-undefined-index

Par contre:

ca marche mtn mais il ajoute pas le produits dans la base de donnée

Nouvelle question ... = Ouvrir une nouvelle DISCUSSION.
(et penser à mettre celle-ci en résolu).
Merci.
0
medboy94 Messages postés 44 Date d'inscription jeudi 6 septembre 2012 Statut Membre Dernière intervention 14 mai 2016
11 mai 2016 à 17:59
dac mercii :)
0