ajax - Updating products in a mysqli database using PHP -


trying update 1 row @ time using php.

i want enable users update products have added database, have simple form relevant fields:

 <fieldset><legend><span> update product in database! </span></legend>          <form  id ="productsform" method="post" onsubmit="return false;" enctype="multipart/form-data">           <label> product name:               <input  type="text"     id="name"           name="name"/>           </label>          <label> product quantity:           <input  type="number"   id="quantity"       name="quantity"/>       </label>          <label> product description:        <input  type="text"     id="description"    name="description"/>    </label>          <label> product price:              <input  type="text"     id="price"          name="price"/>          </label>          </br>          <input id="submit" name="submit" type="button" class="reg" value="update product">          <div id="update"></div>              </form> 

i using ajax working correctly according console, im struggling php side of updating rows:

<?php  include("dbase/config_database.php");  $id = $_post["id"]; $name = $_post['name']; $quantity = $_post['quantity']; $description = $_post['description']; $price = $_post['price'];  $query = "update products set name = '{$name}', quantity = '{$quantity}', description = '{$description}', price = '{$price}'  id = {$id}";  mysqli_query($mysqli, $query);  ?> 

here initial file use add products database:

<?php include("dbase/config_database.php");  //stores information passed through ajax query $name = $_post['name']; $quantity = $_post['quantity']; $description = $_post['description']; $price = $_post['price'];   //adds information database $query = "insert products (name, quantity, description, price) values ('$name','$quantity','$description','$price')"; //runs query $result = $mysqli->query($query) or die("failed query $query"); echo $mysqli->error."<p>";  echo "product added";  $querynew = ("select id 'collectid' products name = '$name'and quantity = '$quantity'and description ='$description'and price = '$price'"); $resultnew = $mysqli->query($querynew) or die("failed query $querynew");  while($info = mysqli_fetch_array( $resultnew)){     $productid = $info['collectid']; }  $image = $_files['file1']['name']; $type = $_files['file1']['type']; $size = $_files['file1']['size']; $tmp_name = $_files['file1']['tmp_name'];  $imgpath = "images/".$productid.".jpg";  // run move_uploaded_file() function here $moveresult = move_uploaded_file($tmp_name, $imgpath); // evaluate value returned function if needed  $querytwo = ("select * products name = '$name' , quantity = '$quantity' , description = '$description' , price = '$price'"); $resulttwo = $mysqli ->query($querytwo) or die ("failed query $querynew");  $info = array(); while($row = mysqli_fetch_assoc($resulttwo)){     $product = array("id" => $row ['id'],         "name" => $row ['name'],         "quantity" => $row ['quantity'],         "description" => $row ['description'],         "price" => $row ['price'],  );      array_push($info,$product); }  $json_output = json_encode($info); echo $json_output; ?> 

any appreciated! have messed around update php because im sure problem in there cant find it.

you not getting $_post["id"] form,because there no input element name id.

when data table put id in form hidden field

like

<input type="hidden" name="id" value="<?=$row['id']?>"> 

then after submitting form you'll id value in $_post['id']

always try debug code thoroughly try print query can know happening actually.all best


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -