简体   繁体   中英

MySQL PHP jQuery - Row doesn't update?

I am writing a CMS and I'm stuck at updating existing products in the products table....

The problem is that every value will receive the update besides the "text" and "stock" row... they go blank....

This is my code

Form:

<form class="form-horizontal form-inline">
            <div class="form-group">
                <label class="control-label col-sm-3" for="title">Product:</label>
                <input type="text" class="form-control" id="title" name="title" placeholder="Enter new title">
            </div>
            <div class="form-group">
                <label class="control-label col-sm-3" for="price">Price:</label>
                <input type="text" class="form-control" id="price" name="price" placeholder="Enter new price">
            </div>
            <div class="form-group">
                <label class="control-label col-sm-3" for="stock">Stock:</label>
                <input type="text" class="form-control" id="stock" name="stock" placeholder="Enter new stock">
            </div>
            <div class='form-group'>
                    <label class="control-label col-sm-3" for="text">Text:</label>
                    <textarea class='form-control' rows='5' id='newPostText' name='text' placeholder='Enter new text'></textarea>
            </div>
            <div class="form-group">
                <label class="control-label col-sm-3" for="description">Description:</label>
                <input type="text" class="form-control" id="description" name="description" placeholder="Enter new description">
            </div>
            <div class="form-group">
                <label class="control-label col-sm-3" for="brand">Brand:</label>
                <input type="text" class="form-control" id="brand" name="brand" placeholder="Enter new brand">
            </div>
            <div class="form-group">
                <label class="control-label col-sm-3"  for="keywords">Keywords:</label>
                <input type="text" class="form-control" id="keywords" name="keywords" placeholder="Enter new keywords">
            </div>
            <a href="#" id="updateProductRecord" pid="<?php echo $pro_id; ?>"><button type="submit" id="submitUpdateProduct" class="btn btn-default">Submit</button></a>
            </form>

jQuery:

$("body").delegate("#updateProductRecord","click",function(event){
event.preventDefault();

var product_id = $(this).attr('pid');
updateProductRecord(product_id);

animatedLoading();
});

function updateProductRecord(product){

var title = $("#title").val();
var text = $("#text").val();
var summary = $("#description").val();
var keywords = $("#keywords").val();
var price = $("#price").val();
var stock = $("#stock").val();
var brand = $("#brand").val();

var product = product;

$.ajax({
    url :   "action.php",
    method: "POST",
    data    :   {updateProductRecord:1,pid:product,title:title,text:text,summary:summary,keywords:keywords,price:price,stock:stock,brand:brand},
    success :   function(data){
        $(".row").append(data);
    },
});

}

PHP:

if(isset($_POST['updateProductRecord'])) {

$product_id = $_POST['pid'];
$title = $_POST["title"];
$category = $_POST["brand"];
$text = $_POST['text'];
$summary = $_POST['summary'];
$keywords = $_POST['keywords'];
$price = $_POST['price'];
$stock = $_POST['stock'];
$date=date('d.m.y h:i:s');


$sql = "UPDATE products SET `title`='$title', `desc`='$summary', `price`='$price', `brand`='$category', `keywords`='$keywords', `stock`='$stock', `text`='$text' WHERE id='$product_id'";

$run_query = mysqli_query($connect,$sql);
if($run_query){

    echo "
        <div class='alert alert-success'>
            <a href='#' class='close' data-dismiss='alert' aria-label='close'>&times;</a>
            <b>Your post has been submitted.!</b>
        </div>
        ";
}
else {

    echo "
        <div class='alert alert-warning'>
            <a href='#' class='close' data-dismiss='alert' aria-label='close'>&times;</a>
            <b>Oops, something went wrong..</b>
        </div>
        ";
}

}

更改jQuery文本字段值

var text = $("#newPostText").val();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM