Jump to content

Barand

Moderators
  • Posts

    24,607
  • Joined

  • Last visited

  • Days Won

    831

Everything posted by Barand

  1. Given the array values from you previous topic, you should be aiming to generate an SQL string that looks like this INSERT INTO Pantry (ItemName, ItemWeight, ItemPrice, ItemDate) VALUES ('Bacon', 500, 3.25, '2020-12-12'); 1 ) The $name needs to be inside single quotes otherwise it interprets bacon as a column name and not as a string value. 2 ) Your weight and price columns should be numeric types, not varchar (weight int, price decimal(10,2) ). 3 ) Your current date format of 12/12/2020 is not a valid DATE format. Your unquoted date string in that format is interpreted as "12 divide by 12 divide by 2020" You current method of putting variables inside the SQL string is unsafe. Use prepared statements and pass the values as parameters EG $stmt = $pdo->prepare(("INSERT INTO Pantry (ItemName, ItemWeight, ItemPrice, ItemDate) VALUES (?,?,?,?) "); $stmt->execute( [ $name, $weight, $price, $date ] );
  2. I hope you are not storing dates in your db table in that "12/12/2020" format. What is the structure of the table you are trying to update?
  3. Sort the array then loop through your array of dates. Store them in another array whose key is "month year" and the elements are arrays of days. EG [ 'January 2020' => [1, 22, 31], 'March 2020' => [10, 12] ] Loop through that array outputting the imploded day arrays and keys. edit: PS if your dates are a database table mysql> SELECT DATE_FORMAT(datecol, '%M %Y') as my -> , GROUP_CONCAT(day(datecol) SEPARATOR ', ') as days -> FROM datetbl -> GROUP BY DATE_FORMAT(datecol, '%M %Y') -> ORDER BY datecol; +----------------+------------+ | my | days | +----------------+------------+ | May 2005 | 21 | | November 2006 | 1, 2 | | September 2020 | 28, 29, 30 | | October 2020 | 1 | +----------------+------------+
  4. Correction to my above code <input type="checkbox" name="fruit['apple']" id="apple" value="1"> ^ ^ | | remove quotes in input names
  5. I've added the tags for you - this time. Next time, use the "code" icon
  6. Try running the script below. It will display the posted form input. If you see what is being posted it should give you a clue about how to process it. <?php if ($_SERVER['REQUEST_METHOD'] == 'POST') { // was form data submitted? if (isset($_POST['fruit'])) { echo "Your form inputs were<br>"; echo '<pre>$_POST = ', print_r($_POST, 1), '</pre>'; echo "<hr>"; } else echo "No fruits were selected<hr>"; } ?> <!DOCTYPE html> <html lang="en"> <head> <title>Example</title> <style type='text/css'> label { display: inline-block; width: 80px; } </style> </head> <body> <form method="POST"> <fieldset> <legend>Select fruits</legend> <label for='apple'>Apple</label> <input type="checkbox" name="fruit['apple']" id="apple" value="1"> <br> <label for='orange'>Orange</label> <input type="checkbox" name="fruit['orange']" id="orange" value="1"> <br> <label for='banana'>Banana</label> <input type="checkbox" name="fruit['banana']" id="banana" value="1"> <br> <label for='pear'>Pear</label> <input type="checkbox" name="fruit['pear']" id="pear" value="1"> <br> <label for='kiwi'>Kiwi</label> <input type="checkbox" name="fruit['kiwi']" id="kiwi" value="1"> <br> </fieldset> <br> <input type="submit" value="Submit"> </form> </body> </html>
  7. Yes, there will be some variables to output that were prepared in the php section, but any validation conditions requiring you to send a location header to another page will have occured before anything is sent to the browser in the html section.
  8. Simples! Do your php processing before the html section. <?php ?> <html> </html>
  9. Perhaps ## ## create a test table ## $db->exec("CREATE TABLE IF NOT EXISTS production_status ( id int not null auto_increment primary key, submit_time datetime ) "); ## ## randomly add 1,000 records for last 2 months ## $data = []; for ($i=0; $i < 1000; $i++) { $r = rand(1, 60); $dt = date('Y-m-d H:i:s', strtotime("-$r days")); $data[] = "('$dt')"; } $db->exec("INSERT INTO production_status (submit_time) VALUES " . join(',', $data)); ## ## Now the bit you need ## $start_date = date('Y-m-d', strtotime('first day of this month')); $res = $db->prepare("SELECT date_format(submit_time, '%a %d-%b-%y') as date , COUNT(*) as total FROM production_status WHERE date(submit_time) >= ? GROUP BY date ORDER BY submit_time "); $res->execute([ $start_date ]); RESULTS +---------------+-------+ | date | total | +---------------+-------+ | Tue 01-Sep-20 | 14 | | Wed 02-Sep-20 | 24 | | Thu 03-Sep-20 | 15 | | Fri 04-Sep-20 | 21 | | Sat 05-Sep-20 | 15 | | Sun 06-Sep-20 | 24 | | Mon 07-Sep-20 | 17 | | Tue 08-Sep-20 | 17 | | Wed 09-Sep-20 | 15 | | Thu 10-Sep-20 | 11 | | Fri 11-Sep-20 | 15 | | Sat 12-Sep-20 | 16 | | Sun 13-Sep-20 | 13 | | Mon 14-Sep-20 | 11 | | Tue 15-Sep-20 | 21 | | Wed 16-Sep-20 | 21 | | Thu 17-Sep-20 | 15 | | Fri 18-Sep-20 | 17 | | Sat 19-Sep-20 | 17 | | Sun 20-Sep-20 | 14 | +---------------+-------+
  10. You can't pass identifiers (table/column names) as parameters
  11. If you are starting with this (which could be the result from a table subquery) ... +----+----------------------+------------+-----------------+----------+ | id | disciplina | moduloUfcd | idcpDisciplinas | anoTurma | +----+----------------------+------------+-----------------+----------+ | 58 | Comunicação Visual | 8599 | 49 | 11 | | 59 | Comunicação Visual | 133 | 49 | 11 | | 60 | Comunicação Visual | 134 | 49 | 10 | +----+----------------------+------------+-----------------+----------+ then this query ... SELECT group_concat(id separator ', ') as ids , disciplina , group_concat(moduloUfcd separator ', ') as mods , idcpDisciplinas , anoTurma FROM gmc GROUP BY idcpDisciplinas, anoTurma; gives ... +--------+----------------------+-----------+-----------------+----------+ | ids | disciplina | mods | idcpDisciplinas | anoTurma | +--------+----------------------+-----------+-----------------+----------+ | 60 | Comunicação Visual | 134 | 49 | 10 | | 58, 59 | Comunicação Visual | 8599, 133 | 49 | 11 | +--------+----------------------+-----------+-----------------+----------+
  12. Also note that the above code does not put the lastname into the $result variable.
  13. Seems to me that the best approach would be to fix the problem instead of disabling the warning.
  14. 1 ) You don't need the mysql_real_escape_string. That is for strings (the clue is in the name). $cat_id is an int. 2 ) The mysql_xxxx functions are obsolete. If you ever get around to upgrading you version of PHP to someting less than 5 years old, they won't work. Switch to using PDO class.
  15. Not knowing what data you have, I'm going to guess that is correct. Now do the same in your php code.
  16. If $cat_id contains "" then the query will fail with a syntax error. But we don't know what's in it, nor do we know what's in your table - and we certainly have no idea what "don't work" means unless you tell us. Check if your query gave an error message.
  17. You do it in the same way you have in your get_post($pid) function, only this time pass the category id get_posts($cat_id)
  18. I'd start by sorting them by date so I know the oldest.
  19. You can't put functions inside strings like variables. <?php $x = date('Y'); $y = 1989; $description = "In this classic lecture which was delivered over " . ($x - $y) . " years ago, etc etc....."; echo $description; // ==> In this classic lecture which was delivered over 31 years ago, etc etc..... ?>
  20. Have you tried putting a WHERE clause in your query, for example WHERE blog.cat_id = 4
  21. Welcome.
  22. According to that information in your comment, the records don't have a "status_id" Why the exit();
  23. Instead of outputting the the HTML for a table with rows, you output the HTML for the cards instead.
  24. Since previous post
  25. Just ran a speedcheck while I'm reading this...
×
×
  • 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.