Jump to content

Leaderboard

Popular Content

Showing content with the highest reputation on 11/14/2021 in all areas

  1. If you are outputting an image from a DB blob field, then here's an example... // EMULATE DATA FROM THE DATABASE $type = 'image/png'; $comments = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas porttitor congue massa. Fusce posuere, magna sed pulvinar ultricies, purus lectus malesuada libero, sit amet commodo magna eros quis urna.'; $image_data = file_get_contents('images/snowman.PNG'); // OUTPUT THE DATA echo "<div style='width:396;'> <img src='data:{$type};base64," . base64_encode( $image_data ) . "' width='394' height='393'> <p>$comments</p> "; RESULT
    3 points
  2. pagination involves two sql queries. the first one gets the total number of matching rows (including any join, where, or having terms), so that you can calculate the total number of pages, used when looping to produce pagination links and to test/limit the requested page number. the second one adds a limit term to the base query to get the requested page of data. it doesn't matter what your presentation code is doing with the data that it loops over. you are just producing some output for each pass through the loop. you should actually remove the database specific code from the html document, put it above the start of the html document, then fetch the data that the query matches into an appropriately named php variable. you would then test/loop over this variable where the database specific code is currently at in your html document.
    1 point
  3. you should not put user-provided data into your sql query strigs. It exposes you to "SQLinjection". Used prepared statements intead https://www.php.net/manual/en/pdo.prepare.php https://www.php.net/manual/en/pdostatement.execute.php The first query "SELECT quantity FROM book ..." gets the quantity then stores that quantity in the variable "$available". If the book id isn't found, fetchColumn() would return "false" hence the intva()l to change that to 0 - if no books or no record I don't want to update).
    1 point
  4. Your code seems to be assuming that $delete will be "false" if that first select query does not find any results. Wrong. It will be false only if the query fails with an error. Not finding records is not an error. I would use that first select query to find out how many books there were for that id. Here's a PDO example... if ($_SERVER['REQUEST_METHOD']=='POST') { $res = $conn->prepare("SELECT quantity FROM book WHERE idb = ? "); $res->execute( [$_POST['idb'] ] ); $available = intval($res->fetchColumn()); if ($available >= $_POST['quantity'] ) { $res = $conn->prepare("UPDATE book SET quantity = quantity - ? WHERE idb = ? "); $res->execute( [ $_POST['quantity'], $_POST['idb'] ] ); echo "Book/s successfully removed<br>"; } else { echo "There are only $available books in stock<br>"; } // Remove books with zero quantity $count = $conn->exec("DELETE FROM book WHERE quantity = 0"); echo "$count books were removed<br>"; }
    1 point
This leaderboard is set to New York/GMT-05:00
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.