PHP update through text fields

Currently, the update only works if all the text fields are full, so the user cannot just update the productName, for example. An error does not occur, but if the rest of the text fields remain empty, the database is updated with spaces and 0. I want this to update any text fields that receive input, be it one or all, and leave the rest of the information separately if nothing is entered.

If the productName for this line is Samsung, the description of "Phone" wholesalePrice is 179.99, and I just update only the productName text box. I still want the description and wholesale price to remain unchanged. Right now, if I just update productName only then the wholesale price is displayed as 0.00 and the description is empty. I tried to use the OR operators, not the commas in the query, and any text field that I entered the information returned 0.

if(isset($_POST['id'])) {
    try {
        $query = "UPDATE products SET productName = :productName, description = :description, wholesalePrice = :wholesalePrice,
    retailPrice = :retailPrice, category = :category, quantityOnHand = :quantityOnHand
    WHERE productID = :productID";
        $statement = $db->prepare($query);
        $statement->bindValue(':productID', $_POST['id']);
        $statement->bindValue(':productName', $productName);
        $statement->bindValue(':description', $description);
        $statement->bindValue(':wholesalePrice', $wholesalePrice);
        $statement->bindValue(':retailPrice', $retailPrice);
        $statement->bindValue(':category', $category);
        $statement->bindValue(':quantityOnHand', $quantityOnHand);
        $statement->execute();
        $statement->closeCursor();

//reload page after data is entered into the table and display a message if successful for 3 seconds before redirect
        $page = $_SERVER['PHP_SELF'];
        header('Location: ' . $_SERVER["HTTP_REFERER"] );
        exit;
+4
source share
2 answers

You can use a helper array for columns to dynamically bind values ​​if a value is set $_POST. Then you can create an update request only for these values.

$fields = array('productName', 'description', 'wholesalePrice', 'retailPrice', 'category', 'quantityOnHand');
$values = array();
$binds = array();

foreach($fields as $key => $value) {
    if (isset($_POST[$value])) {
        $values[] = $value.' = :'.$value;
        $binds[':'.$value] = $_POST[$value];
    }
}

if (!empty($values)) {
    $query = "UPDATE products SET ";
    $query .= implode(', ', $values);
    $query .= " WHERE productID = :productID";
    $binds[':productID'] = $_POST['id'];
    $statement = $db->prepare($query);
    $statement->execute($binds);
    $statement->closeCursor();
}

EDIT:

, , :

foreach($fields as $key => $value) {
    if (isset($$value)) {
        $values[] = $value.' = :'.$value;
        $binds[':'.$value] = $$value;
    }
}
+3

, , sql, . , .

<textarea name="description">'.$row['description'].'</textarea>

if(isset($_POST['id'])) {
  $productname = $_POST['productname'];
  $description = $_POST['description'];
  // etc ....
  try {
    // sql
  }catch{
    // error
  }
}
0

Source: https://habr.com/ru/post/1617506/


All Articles