-
Posts
24,566 -
Joined
-
Last visited
-
Days Won
822
Everything posted by Barand
-
Example... <?php if ($_SERVER['REQUEST_METHOD']=='POST') { echo "<b>You selected</b><ul>"; foreach ($_POST['fruit'] as $fruit) { echo "<li>$fruit</li>"; // update database here } echo "</ul><hr>"; } ?> <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Example</title> </head> <body> <h1>Example</h1> <hr> <form method="POST"> Fruit: <br> <select name="fruit[]" multiple size="10"> <option>Apple</option> <option>Banana</option> <option>Date</option> <option>Kiwi</option> <option>Lemon</option> <option>Melon</option> <option>Orange</option> <option>Pear</option> <option>Quince</option> </select> <br> <input type="submit" name="btnSubmit" value="Submit"> </form> </body> </html>
-
How can I limit the number of times the for each loop will run?
Barand replied to guymclarenza's topic in PHP Coding Help
My car engine was leaking oil, so I've drained out all the oil. That's another problem fixed. -
Are you wanting something like this? function golfGroups($players) { $n = count($players); if ($n < 6) return $players; $num_3s = $n%4 ? 4 - $n%4 : 0; $num_remain = $n - $num_3s*3; $fours = array_chunk(array_slice($players, 0, $num_remain), 4); $threes = $num_3s ? array_chunk(array_slice($players, -$num_3s*3), 3) : []; return array_merge($fours, $threes); } ## ## TEST IT ## for ($i=6; $i<=25; $i++) { $p = range(1,$i); $p = array_map( function($v) { return "Player $v";}, $p); // create array of players echo "<h3>$i players</h3>"; echo '<pre>', print_r(golfGroups($p), 1), '</pre>'; echo '<hr>'; }
-
Name the select "troom[]" so the options are posted as an array Then foreach ($_POST['troom'] as $room) { // save $room }
-
Then $stmt = mysqli_prepare($link, "SELECT username , verified , email FROM users WHERE id = ? "); mysqli_stmt_bind_param($stmt, "i", $_SESSION['id']); mysqli_stmt_execute($stmt); mysqli_stmt_bind_result($stmt, $username, $verified, $email); mysqli_stmt_fetch($stmt); echo "$username, $email, $verified"; However, I'd recommend using PDO instead of mysqli $stmt = $pdolink->prepare("SELECT username , verified , email FROM users WHERE id = ? "); $stmt->execute([$_SESSION['id']]); $row = $stmt->fetch(); echo "{$row['username']}, {$row['email']}, {$row['verified']}"
-
When a user logs in, what do you store in $_SESSION so that you know they are logged in, and they can be identified? (ideally, this would be the id of their user record)
-
What information do you know about the logged in user? (I was assuming you have the email already in the session array, but perhaps it's the username or the id (recommended) ).
-
So the only condition you are interested in is "WHERE email = ?"
-
Can we please go back to max-height limitations on code boxes. It's real PITA to have to spend time scrolling through hundreds of lines of code. It's a particilar annoyance when there are also long lines of code which end in the middle of the next county. This means you spend time scrolling down to the bottom so that you can then scroll right then scroll back up to the long line (hoping you scrolled far enough and not too far right) Example here...
-
Your WHERE clause is suspect. Where username, verified and email are what? And if you know what they are, why the query? What is you query supposed to do? Where is $user defiined. You seem to be missing the steps that bind the result and fetch the data returned by the query.
-
To get the email from a database you would use a SQL SELECT statement. https://dev.mysql.com/doc/refman/5.7/en/select.html
-
Problem with PHP, taking care of empty variables
Barand replied to dantheuser's topic in PHP Coding Help
Why not use Date of Birth <input type="date" required> (You will still need to validate the date after the form is submitted to your php script) -
Check for mysql error message to see why it failed
-
-
What exactly does that mean? What happens, or doesn't happen.
-
If you stored your dates as something readable (like DATETIME) instead of using the unintelligible unix timestamps you would see that both those should appear on multiple days 2021-01-23 00:00:00 - 2021-01-30 00:00:00 2021-01-30 00:00:00 - 2021-02-01 00:00:00 Query each separate date is really inefficient. Get all for the month and store in an array. Then as you loop through the days, see if there are any events in the aray for that day and output them.
-
You need to read up on "variable scope". Variables inside a function are local to that function. If you want to use a variable from outside the function you need to pass it as an argument to the function (along with month and year). You need also to check queries for errors. The easiest way to do that is to put this line just before you make your connection to the database... mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT); (edit) .... and turn php's error reporting on.
-
The first query is defined but not executed. The second query probably failed $sql = "SELECT * FROM $tablename WHERE $current_epoch BETWEEN start_day AND end_day"; ^^^^^^^^^^ Where is $tablename defined?
-
Mysql query, combine command about age and separate by gender
Barand replied to nitiphone2021's topic in MySQL Help
Using an "age_group" table as suggested and a test table with 999 random records ... +--------------+------------+--------+--------+ +--------+---------------+------+-----+---------+----------------+ | age_group_id | group_name | lo_age | hi_age | | Field | Type | Null | Key | Default | Extra | +--------------+------------+--------+--------+ +--------+---------------+------+-----+---------+----------------+ | 1 | Under 18 | 0 | 17 | | id | int(11) | NO | PRI | NULL | auto_increment | | 2 | 18 - 29 | 18 | 29 | | name | varchar(30) | YES | | NULL | | | 3 | 30 - 39 | 30 | 39 | | age | tinyint(4) | YES | | NULL | | | 4 | 40 - 49 | 40 | 49 | | gender | enum('M','F') | YES | | NULL | | | 5 | 50 - 59 | 50 | 59 | +--------+---------------+------+-----+---------+----------------+ | 6 | 60 - 69 | 60 | 69 | | 7 | 70 - 79 | 70 | 79 | | 8 | 80+ | 80 | 120 | +--------------+------------+--------+--------+ ... then ... SELECT group_name as `Age Group` , SUM(gender='M') as Male , SUM(gender='F') as Female , COUNT(*) as Total FROM tb_infected i JOIN age_group a ON i.age BETWEEN a.lo_age AND a.hi_age GROUP BY age_group_id; giving +------------+------+--------+-------+ | Age Group | Male | Female | Total | +------------+------+--------+-------+ | Under 18 | 70 | 98 | 168 | | 18 - 29 | 44 | 72 | 116 | | 30 - 39 | 42 | 50 | 92 | | 40 - 49 | 54 | 56 | 110 | | 50 - 59 | 47 | 62 | 109 | | 60 - 69 | 42 | 68 | 110 | | 70 - 79 | 36 | 68 | 104 | | 80+ | 76 | 114 | 190 | +------------+------+--------+-------+ -
You should have searched these forums for "calendar"...
-
Mysql query, combine command about age and separate by gender
Barand replied to nitiphone2021's topic in MySQL Help
I'd be tempted to have an age_group table (id, group_name, lo_age, hi_age) and join to that on if_age BEWEEN lo_age AND hi_age. It would save all those case statements and ensure consistency. BTW, your current age ranges exclude 80 year olds. -
PHP https with $_SESSION send to another page
Barand replied to nitiphone2021's topic in PHP Coding Help
No need. That happens automatically. It's a case of "set it, forget it" -
[PHP 7.4.13 ] imge uppload to database using PDO::
Barand replied to Sprint666l's topic in PHP Coding Help
That still isn't going to work! (unless you aren't using PDO as you stated) mysqli_query() is not a PDO method (the clue's in the name). Get into the habit of using prepared statements and not putting variables in the sql code. $insert = "UPDATE users SET profile_img = ? WHERE username = ?"; $stmt = $connect->prepare($insert); $stmt->execute( [ $filename, $_SESSION['username'] ] );