0

I'm Trying to change the values in the database using PHP and MySQL.

I am getting the values from database and storing them in placeholder for each input but when i submit the form again it submit the inputs with empty values, i tried storing them in Value attribute for each input again the old value is overwriting what i write in the input field so nothing in the database change.

How can i keep the old value in the input fields but in case the content of these fields changed the new value is passed back to the database instead of the old one.

Here is my Code :

function list_products () {
$get = mysql_query('SELECT `id`, `SKU`, `sub`, `pname`, `desc`, `price`, `ship`, `qty`, `cat` FROM `products` ORDER BY `id` DESC');
if (mysql_num_rows($get) == 0) {
    echo "There are no product to display!";
} else {
    while ($get_row = mysql_fetch_assoc($get)) {
        echo 
        '<h5>'.$get_row['pname'].' id: '.$get_row['id'].'</h5> 
        <form action="delete_product.php" method="post">
            <input type="hidden" value="'.$get_row['id'].'" name="id">
            <input type="submit" name="submit" value="DELETE" class="btn btn-lg btn-danger">
        </form>
        <form action="update_p.php" method="post">
            <input type="hidden" placeholder="'.$get_row['id'].'" name="id" value="'.$get_row['id'].'">
            <label>SKU:</label>
            <input type="text" placeholder="'.$get_row['SKU'].'" name="SKU" value="'.$get_row['SKU'].'" required="">
            <label>Name:</label>
            <input type="text" placeholder="'.$get_row['pname'].'" name="pname" required="" value="'.$get_row['pname'].'">
            <label>Subtitle:</label>
            <textarea rows="2" maxlength="46" name="desc" placeholder="'.$get_row['sub'].'" required="">'.$get_row['sub'].'</textarea>
            <label>Description:</label>
            <textarea rows="4" name="desc" placeholder="'.$get_row['desc'].'" required="">'.$get_row['desc'].'</textarea>
            <label>Price:</label>
            <input type="text" placeholder="'.number_format($get_row['price'], 2).'" name="price" value="'.number_format($get_row['price'], 2).'" required="">
            <label>Shipping:</label>
            <input type="text" placeholder="'.number_format($get_row['ship'], 2).'" name="ship" value="'.number_format($get_row['ship'], 2).'" required="">
            <label>Quantity:</label>
            <input type="text" placeholder="'.$get_row['qty'].'" name="qty" value="'.$get_row['qty'].'" required="">
            <label>Category:</label>
            <input type="text" placeholder="'.$get_row['cat'].'" name="cat" value="'.$get_row['cat'].'" required=""><br>
            <input type="submit" name="submit" value="EDIT" class="btn btn-success btn-lg">
        </form>
        <hr>
        ';
    };
}
}

The update_p.php page:

if (empty($_POST) === false && empty($errors) === true) {
$id = $_POST['id'];
$update_data = array(
    'pname'     => $_POST['pname'],
    'desc'      => $_POST['desc'],
    'price'     => $_POST['price'],
    'ship'      => $_POST['ship'],
    'qty'       => $_POST['qty'],
    'cat'       => $_POST['cat']
);
update_product($id, $update_data);

The update_data function :

function update_product($id, $update_data) {
    $update = array();
    array_walk($update_data);
    foreach($update_data as $field=>$data) {
        $update[] = '`' . $field . '` = \'' . $data . '\'';
    }   
    mysql_query("UPDATE `products` SET " . implode(', ', $update) . " WHERE `id` = $id");
}

@MaveRick Thanks for the effort, but my question is how to overwrite the value of the input fields on the page before we send the information to the server, i think its can be done using JavaScript more than php, however to make more clear these input fields refers to values in the database already stored and i would like to give the option for the user to change them in his admin panel by retrieving the content of these fields from the database and print them in the actual input fields so in case the customer pressed edit(submit) with out touching anything the same values will be sent again to the database this way nothing will be change, and in another case where the customer added or changed any value in any of these fields then the new value will be passed. hopefully i clarified my issue now. Thanks for your help anyway.

@Prafful Garg i already tried the value field but it didn't work thanks for your help anyway

4
  • 1
    Show the part of code with the mysql UPDATE statement pls Commented Mar 25, 2014 at 11:21
  • 1
    Placeholders is only for viewing purpose. They are not passed with the $_POST object. Commented Mar 25, 2014 at 11:24
  • Please show the update statement Commented Mar 25, 2014 at 11:26
  • @MaveRick the update statement is in the update_product function, Thanks Commented Mar 25, 2014 at 11:35

1 Answer 1

1

You can retrieve the data again from the database in file update_p.php as follow:

if (empty($_POST) === false && empty($errors) === true) {
$id = $_POST['id'];
$get = mysql_query("SELECT * FROM `products` WHERE `id` = '$id';");
$get_row = mysql_fetch_assoc($get);

$update_data = array(
    'pname'     => (empty($_POST['pname'])?$get_row['pname']:$_POST['pname']),
    'desc'      => (empty($_POST['desc'])?$get_row['desc']:$_POST['desc']),
    'price'     => (empty($_POST['price'])?$get_row['price']:$_POST['price']),
    'ship'      => (empty($_POST['ship'])?$get_row['ship']:$_POST['ship']),
    'qty'       => (empty($_POST['qty'])?$get_row['qty']:$_POST['qty']),
    'cat'       => (empty($_POST['cat'])?$get_row['cat']:$_POST['cat'])
);
update_product($id, $update_data);

But this is not a professional way to do it. What you can do is a loop to create the update statement and test each input; if not empty then add to the update statement , name=value otherwise to skip this input and process the next one in the array POST. something similar to the following:

function update_product($id, $update_data) {
    $update = array();
    array_walk($update_data);
    foreach($update_data as $field=>$data) {
        $update[] = '`' . $field . '` = \'' . $data . '\'';
    }  
    $query = "UPDATE `products` SET `id` = '$id'";
    foreach($update_data AS $k=>$v){
        if(!empty($v) && strlen($v)>0) 
           $query .= ", `".$k."` = '".$v."'";
    }
    $query .= " WHERE `id` = '$id';";
    mysql_query($query);
}

IMPORTANT NOTE: mysql() is vulnerable and deprecated, please try to avoid using mysql() and use mysqli() or dbo() extensions instead.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.