-
Posts
24,563 -
Joined
-
Last visited
-
Days Won
822
Everything posted by Barand
-
Does this show anything echo '<pre>' . print_r($line_item->price->metadata, 1) . '</pre>';
-
Not sure - I don't see anything in there
-
Have you tried $amount_total = $line_item->amount_total; where $line_item is on of your line item objects?
-
Creating a quiz system with mysql and php (correction of code)
Barand replied to e-gab2's topic in PHP Coding Help
You still are not reading the replies. Re-read your initial post - it is now the code edited by gizmola. It even tells you at the bottom of the post that it was edited by gizmola. -
Don't double-post your questions. It wastes our time.
-
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).
-
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>"; }
-
Creating a quiz system with mysql and php (correction of code)
Barand replied to e-gab2's topic in PHP Coding Help
To get the most value from this forum it is best to actually read the replies given. Gizmola told you he had corrected your code in the initial post... -
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
-
You are passing the prices in the post data as the checkbox values, but don't use them (they can be spoofed). Instead use the id to get the price. EXAMPLE <?php $available_seasons = array ( 226 => array ( 'season_name' => '2022 - Season 1', 'season_price' => '25.99', 'season_start_date' => 'Jan. 1, 2022', 'season_end_date' => 'Mar. 31, 2022', 'prize' => 100, ), 227 => array ( 'season_name' => '2022 - Season 2', 'season_price' => '28.99', 'season_start_date' => 'Apr. 1, 2022', 'season_end_date' => 'Jun. 30, 2022', 'prize' => 100, ), 238 => array ( 'season_name' => '2022 - Season 3', 'season_price' => '40.99', 'season_start_date' => 'Jul. 1, 2022', 'season_end_date' => 'Sep. 30, 2022', 'prize' => 230, ), 239 => array ( 'season_id' => 239, 'season_name' => '2022 - Season 4', 'season_price' => '30.65', 'season_start_date' => 'Oct. 1, 2022', 'season_end_date' => 'Dec. 31, 2022', 'prize' => 300, ) ); if ($_SERVER['REQUEST_METHOD'] == 'POST') { $total = 0; echo "<h3>Chosen seasons</h3>"; foreach ($_POST['choice'] as $id => $chosen) { $price = $available_seasons[$id]['season_price']; // get price from your data, not the user $total += $price; echo "{$available_seasons[$id]['season_name']} — \${$price}<br>"; } echo "<h3>Total due  \${$total}</h3><hr><br><br>"; exit; } ?> <!DOCTYPE html> <html lang="en"> <head> <title>Example</title> <meta charset="utf-8"> <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script> <script type='text/javascript'> function checkTotal(box) { var sum = parseFloat($("#total").val()) if ( $(box).is(":checked")) { sum += $(box).val() * 1 } else { sum -= $(box).val() * 1 } $("#total").val(sum.toFixed(2)) } </script> </head> <body> <form name='seasonForm' method="post" action=''> <fieldset> <legend>Tickets for Seasons</legend> <?php foreach ( $available_seasons as $k => $season ) { echo "<input name='choice[$k]' class='uk-checkbox' type='checkbox' value='{$season[ 'season_price' ]}' onclick='checkTotal(this)'/> {$season['season_name']}<br>"; } echo "<button type='submit'>Continue</button><br>"; ?> </fieldset> <input id='total' value='0' > </body> </html>
-
As you are processing a csv file, use fgetcsv() You indexes for the data are wrong - "dmcnum" will be index [0] (arrays number from 0 by default). You are giving all records inserted the same ID (00001) - not good. Use an auto_incrementing key in your db table. Defore connecting to your DB, call this code below to automate mysql error reporting mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT); Forget mysqli and use the superior PDO library (better and easier)
-
AFAIK, DateTime and DateInterval only work with whole seconds. If you want to go smaller you'll need microtime()
-
Problems saving to sql after updating to PHP 7.3
Barand replied to JerryTellez's topic in PHP Coding Help
Two suggestions. Stop createing a new mysql connection every time you want to execute a query. Just connect once for each page. Stop using multi_query() - use query() -
A while() loop stops when the condition returns false (mysqli_fetch_array() returns false when there are no more records to fetch) A foreach() loops iterates through the elements of an array
-
I have a solution. SOLVED
-
<?php if (login_check($mysqli) == true) : ?> Cannot load
Barand replied to xhulio's topic in PHP Coding Help
-
Multidimentional Arrays [Creating and Looping for output]
Barand replied to Adamhumbug's topic in PHP Coding Help
If there are multiple reocords for years then each array element for the year need to be an array of records while($stmt -> fetch()){ $awards[$ay][] = [$id, $an, $aw]; } or while($stmt -> fetch()){ $awards[$ay][$id] = [$an, $aw]; }