Joshua Del Valle
Joshua Del Valle

Reputation: 1

Update in PHP to mysql

if(isset($_POST['submit'])) {
    require_once('db.php');

    $prod_id = $_POST['product_id'];
    $pname = $_POST['prod_name'];
    $price = $_POST['prod_price'];
    $stock = $_POST['stocks'];
    $category = $_POST['category'];
    $update = null;

    if (($pname = '')){
        $update .= 'product_name = "'.$pname.'"'; 
    }

    if (($pcode = '')){
        if(!($update == '' || $update == null)) $update .= ',';
        $update .= "product_price='".$price."'";
    }

    if (($stock = '')){
        if(!($update == '' || $update == null)) $update .= ',';
        $update .= "stocks='".$stock."'";
    }

    if (($category = '')){
        if(!($update == '' || $update == null)) $update .= ',';
        $update .= "category='".$category."'";
    }

    $sql = 'UPDATE products_table set '.$update.' WHERE product_id = '.(int)$prod_id;
    echo $sql;
    $qry = mysql_query($sql);

    if (!$qry) {
        ?>
        <script>
            alert('Failed to update.');
            window.location.href = 'update.php?prod_id='+<?php echo $prod_id;?>;
        </script>
        <?php
    } else {
        ?>
        <script>
            alert('Product Updated.');
            window.location.href = 'products_list.php';
        </script>
        <?php
    }


}

Here is the update_process.php file.

    <div class="row">
            <h1 class="page-header">Update Product </h1>
    </div>

    <div class="row">
        <label><a href="products_list.php">Return from Products List</a></label>
    </div>

    <div class="row">
        <form method="POST" action="update_process.php" class="input-group">

            <input type="hidden" name="product_id" value="<?php echo $_GET['prod_id']?>" >
            <label>Product Name</label> 
                <input type="text" class="form-control" value="" name="prod_name" /><br />

            <label>Product Price</label>
                <input type="text"  class="form-control" value=""  name="prod_price" /><br />

            <label>Stocks</label> 
                <input type="text"  class="form-control" value="" name="stocks" /><br />
            <label>Category </label> 
                <input type="text"  class="form-control" value= "" name = "category" /> <br/> 


            <input type="submit" class="btn btn-primary" name="submit" value="Update Product" /> &nbsp
            <input type="reset" class="btn btn-primary" value="Clear Fields" />
        </form>
    </div>

<div/> 

This is the update.php file.

I am having a problem on updating a row in my products list, i don't know what seems to be the wrong why it is not updating

Upvotes: 0

Views: 53

Answers (2)

Jesper
Jesper

Reputation: 3834

if(isset($_POST['submit'])) {
    require_once('db.php');

    $prod_id = $_POST['product_id'];
    $pname = $_POST['prod_name'];
    $price = $_POST['prod_price'];
    $stock = $_POST['stocks'];
    $category = $_POST['category'];
    $update = null;

    if ($pname != ''){
        $update .= 'product_name = "'.$pname.'"';
    }

    if ($price != ''){
        if(!($update == '' || $update == null)) $update .= ',';
            $update .= "product_price='".$price."'";
    }

    if ($stock != ''){
        if(!($update == '' || $update == null)) $update .= ',';
            $update .= "stocks='".$stock."'";
    }

    if ($category != ''){
        if(!($update == '' || $update == null)) $update .= ',';
            $update .= "category='".$category."'";
    }



    $sql = 'UPDATE products_table set '.$update.' WHERE product_id = '.(int)$prod_id;
    echo $sql;
    $qry = mysql_query($sql);

    if (!$qry) {
    ?>
        <script>
            alert('Failed to update.');
            window.location.href = 'update.php?prod_id='+<?php echo $prod_id;?>;
        </script>
    <?php
    } else {
    ?>
        <script>
            alert('Product Updated.');
            window.location.href = 'products_list.php';
        </script>
    <?php
    }
}

I've made some changes to your if conditions so they're correct. You were using = instead of !=, since it seems like you're trying to check if they're not equal ''. If you're using '=' you're setting the value of variable to nothing (''), to check if something is equal, use '==' and not equal '!='.
Otherwise everything else seems fine. Try it out.

Upvotes: 0

Raghavendra
Raghavendra

Reputation: 3580

conditions are wrong.

if(($pname = ''))

should be

if($pname != '')

like that modify for all.

Upvotes: 1

Related Questions