Jump to content

Barand

Moderators
  • Posts

    24,563
  • Joined

  • Last visited

  • Days Won

    822

Everything posted by Barand

  1. The simpler you make it the less chance there is of errors.
  2. You can echo "<br><div id='$index' class='mydiv'>"; or, if you specifically want double-quotes (as is sometimes necessary), you can echo "<br><div id=\"$index\" class='mydiv'>"; Either way there is no concatenation and convoluted nested quotes required
  3. I tried out @mac_gyver's excellent idea using these tables... TABLE: booking_slot; TABLE: booking; +---------+---------------+ +------------+--------------+---------+--------+ CREATE TABLE `booking` ( | slot_no | slot_times | | booking_id | booking_date | slot_no | userid | `booking_id` int(11) NOT NULL AUTO_INCREMENT, +---------+---------------+ +------------+--------------+---------+--------+ `booking_date` date DEFAULT NULL, | 9 | 09:00 - 10:00 | | 1 | 2022-11-14 | 10 | 101 | `slot_no` int(11) DEFAULT NULL, | 10 | 10:00 - 11:00 | | 2 | 2022-11-14 | 10 | 102 | `userid` int(11) DEFAULT NULL, | 11 | 11:00 - 12:00 | | 3 | 2022-11-14 | 11 | 103 | PRIMARY KEY (`booking_id`), | 12 | 12:00 - 13:00 | | 7 | 2022-11-14 | 11 | 104 | UNIQUE KEY `unq_booking_1` (`booking_date`,`slot_no`,`userid`), | 13 | 13:00 - 14:00 | | 8 | 2022-11-14 | 12 | 105 | KEY `idx_booking_slot_no` (`slot_no`), | 14 | 14:00 - 15:00 | | 5 | 2022-11-15 | 11 | 103 | KEY `idx_booking_userid` (`userid`) | 15 | 15:00 - 16:00 | | 10 | 2022-11-15 | 11 | 107 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8; | 16 | 16:00 - 17:00 | | 9 | 2022-11-15 | 12 | 106 | | 17 | 17:00 - 18:00 | | 11 | 2022-11-16 | 9 | 101 | +---------+---------------+ +------------+--------------+---------+--------+ On experimenting, it appears that "FROM DUAL" is optional as this worked fine $stmt = $pdo->prepare("INSERT IGNORE INTO booking (booking_date, slot_no, userid) SELECT ?, ?, ? WHERE (SELECT COUNT(*) FROM booking WHERE booking_date = ? AND slot_no = ?) < 2 "); $stmt->execute([ $date, $slot, $user, $date, $slot ]); if ($stmt->rowCount()==0) { $error = "Booking was unsuccessful"; } The unique key in the booking table prevents a single user booking both places in a timeslot.
  4. Set a UNIQUE constraint on the appointment datetime in your database table. Validate that all the time are on the hour and half-hour.
  5. @phppup if you think that code is a substitute for isset(), then it's time to RTFM again
  6. While you are fixing the spelling, there are one or two other things you might want to consider. Don't use SELECT *. The more data you fetch from the server, the slower the query and you don't need every column. In this case you would want the user's id to store in your session variables as evidence of logging in. Don't put user-provided variable directly ito your query. It makes it vulnerable to an SQL injection attack. Use prepared statements instead. Don't store passwords as plain text, it's insecure. Use password_hash() when storing and password_verify() when checking. Check the manual for the correct parameters to us with mysql_query(). If you follow the above you should end up with somethng like $res = $con->prepare("SELECT user_id , password FROM users WHERE email = ? "); $res->bind_param('s', $email); $res->execute(); $res->bind_result($user_id, $hash); if ($row = $res->fetch()) { if (password_verify($password, $hash)) { $_SESSION['user_id'] = $user_id; echo "login successful"; } else { echo "invaild"; } } else echo "invalid"; A final piece of advice. As you haven't invvested a great deal of time into learning mysqli, now is a good time tme to switch to the better PDO interface. In which case the code becomes $res = $con->prepare("SELECT user_id , password FROM users WHERE email = ? "); $res->execute([ $email ]); if ($row = $res->fetch()) { if (password_verify($password, $row['password'])) { $_SESSION['user_id'] = $row['user_id']; echo "login successful"; } else { echo "invaild"; } } else echo "invalid";
  7. Missing semicolon at end of line 5. You have it inside the string instead of after it.
  8. Give all the divs a class of "mydiv". Then in the <style> section change #mydiv { display: none; } to .mydiv { display: none; } then they all start off hidden.
  9. What has that response to do with my last question? i.e.
  10. I guessed that but your code shows no indication if any code that is buiding your links and divs from a query result. You've shown us that you put the row id in the onclick event call. Where do you put the same id in the div's id attribute?
  11. Where did $row suddenly spring from? Your code showed nothing like that.
  12. Each time I click the button it either shows or hides the div so I cannot reproduce your problem. Is your code different from mine?
  13. Same problem
  14. As basic as it gets... <!DOCTYPE html> <html lang="en"> <head> <title>Sessions &amp; Terms</title> <meta charset="utf-8"> <script type='text/javascript'> function showDiv() { let thediv = document.getElementById("mydiv") if (thediv.style.display == "block") { thediv.style.display = "none" } else { thediv.style.display = "block" } } </script> <style type='text/css'> #mydiv { display: none; } </style> </head> <body> <button onclick='showDiv()'>Show Div</button> <br> <div id='mydiv'> <h1>Hello, world</h1> </div> </body> </html>
  15. As the message tells you, you can't call session_start() after you have sent data to the browser. You are sending output on line 10 apparently. For example, the gap between ?> and the following <?php will result in a couple of newlines being sent.
  16. $arr = [ 1, 2, 3, 1, 4, 5, 2, 6, ]; $arr2 = array_unique($arr); // to force a unique set of values $arr3 = array_count_values($arr); // to verify they are all unique
  17. 2 new things. fetchAll() is a PDO method.
  18. So there is! $result = $db->query("select sku_numbers from table"); $skus = $result->fetchAll(PDO::FETCH_COLUMN); Thanks @mac_gyver - one keeps on learning.
  19. $result = $db->query("select sku_numbers from table"); $rows = $result->fetchAll(); $sku_array = array_column($rows, 'sku_numbers'); Probably faster using the inbuilt functions.
  20. Not according to the first post...
  21. Right. The links need to be created on your web page, SQL is confined to the DB server. This query fetches the chosen page of results after a link is clicked..
  22. No problem. Make that query a subquery of your query doing the pagination so that you paginate the results of the query. For example SELECT rank , name , donated , received FROM ( -- THIS IS THE QUERY TO DO THE RANKING - NOW A SUBQUERY SELECT id , name , @seq := @seq + 1 as seq , @rank := CASE WHEN total_received = @prevr THEN CASE WHEN total_donated = @prevd THEN @rank ELSE @seq END ELSE @seq END as rank , @prevr := total_received as received , @prevd := total_donated as donated FROM ( SELECT id , name , total_donated , total_received FROM donation WHERE total_participant = 1 ORDER BY total_received DESC, total_donated ASC LIMIT 18446744073709551615 ) ordered JOIN ( SELECT @rank := 0, @seq := 0, @prevd := 0, @prevr := 0 ) init ) ranked -- END OF RANKING SUBQUERY ORDER BY rank LIMIT ?, ? "; Running that query with LIMIT 0, 4, then LIMIT 4,4 gave 2 pages... Page 1 +------+---------+---------+----------+ | rank | name | donated | received | +------+---------+---------+----------+ | 1 | Dasher | 250 | 500 | | 2 | Cupid | 370 | 400 | | 2 | Prancer | 370 | 400 | | 4 | Comet | 370 | 380 | +------+---------+---------+----------+ Page 2 +------+---------+---------+----------+ | rank | name | donated | received | +------+---------+---------+----------+ | 5 | Dancer | 510 | 200 | | 6 | Vixen | 200 | 100 | | 7 | Donner | 510 | 100 | +------+---------+---------+----------+
  23. UPDATE users SET status = 'Offline now' WHERE status = 'Active now' ;
  24. The purpose of the subquery is to sort the data into the required order (ORDER BY total_received DESC, total_donated ASC) before applying the rankings. MariaDB ignores ORDER BY in subqueries. The way round it is the large LIMIT value to force it to write to a temporary table on disc.
  25. In that case you need to uncomment the line in the query where it says "required by mariaDB users"
×
×
  • 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.