Jump to content

Barand

Moderators
  • Posts

    24,607
  • Joined

  • Last visited

  • Days Won

    831

Everything posted by Barand

  1. try this for cocoa_type_chocolate.php <?php mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); $mysqli = mysqli_connect("localhost", "root", "*****", "cocoa"); if (isset($_GET['type_chocolate'])) { $res = $mysqli->prepare("SELECT c.id_chocolate , c.name_chocolate , t.id_type_chocolate , t.type_chocolate FROM chocolate c INNER JOIN type_chocolate t ON c.id_chocolate = t.id_type_chocolate WHERE type_chocolate = ? "); $res->bind_param('s', $_GET['type_chocolate']); $res->execute(); $res->bind_result($id_chocolate, $name_chocolate, $id_type_chocolate, $type_chocolate) ; echo '<pre>'; while ($res->fetch()) { printf("%s %s %s %s\n", $id_chocolate, $name_chocolate, $id_type_chocolate, $type_chocolate); } echo '</pre>'; } PS not sure if the join is right as I don't know your table structure edit - forgot the execute (I hate mysqli - too many things to do. Give me PDO any day)
  2. I removed the passwords from your posts. You can do the reformatting yourself if you want people to read them. You do seem to be connecting to the DB server 3 ties in same file though - once is sufficient.
  3. That query statement is an example of what I said about the code...
  4. I don't see how... 100000000000000000 9223372036854775807 He isn't running it again - he attempting to run a query on a statement object instead of a string of SQL.
  5. @ginerjm Please use PHP code type for your PHP code. This enables the format highlighting features (such as the comments above). I have edited this one for you.
  6. The column names in the query results wil be id_chocolate name_chocolate id_type_chocolate type_chocolate id_chocolate The tablename. prefixes are not part of them.
  7. Answer: The first line only. The rest is a hotch-potch of pasted code with no thought for what each line does or what the variables contain.
  8. If you ever get around to looking at the PHP manual and look up "shuffle()" yo will see its description is This function shuffles (randomizes the order of the elements in) an array. As it is randomising what you already have (ie a random number) what are you gaining? The second thing to note in the description is that it is for shuffling the elements of an array, not the characters in a string.
  9. The shorter answer would be to the question "What would work?"
  10. The whole point of prepared statements is not to put variables in the query. Look at my code again.
  11. Would you like me to move this thread to the "Politics & Economics" forum?
  12. Brilliant, Sherlock! If you had taken the trouble to read the (old and already fixed) post you would have seen that it already uses the modulo operator.
  13. You were right with the COUNT(*). You should be using a prepared statement and not trying to sanitize the input using htmlspecialchars (which is an output function) $sql = 'SELECT COUNT(*) FROM myTable WHERE myField = ?'; $stmt = $con->prepare($sql); $stmt->execute( [ $_GET["example"] ] ); if ($stmt->fetchColumn() == 0 {
  14. Yes, of course there is. If your array contains, say, 10 expression then you can write out 10 if() expressions - 1 for each array element). For example if (preg_match($regex[0], $mystr)) { // do something } elseif (preg_match($regex[1], $mystr)) { // do something else } elseif (preg_match($regex[2], $mystr)) { // do something else } ... //etc
  15. That code doesn't do that. That will be a problem as you don't store the data, you just fetch it and throw it away.
  16. I don't see your problem. I have given you a solution using @mac_gyver's example.
  17. You now create all your TreeLinks with id='tom' and all SimilarLinks with id='jerry'. Why are are you having difficulty with the concept of UNIQUE? I'll type this slowly for you ... No -- two -- elements -- on -- a -- page-- can -- have-- the -- same -- id -- value. Try some thing like this (As we only see snippets of your code, as though through keyholes, and not the whole picture, I am assuming that the following code is inside a loop and $index is incremented on each iteration). Prefix the TreeLink IDs with "T" and the SiimilarLink IDs with "S". echo "<br><a href='#' onclick='showDiv(\"T$index\")' style='float: right; margin-top: -6px; margin-right: 8px;'>Show domain linkage</a>"; echo getLinkTree($pdo, $row['url'], "T$index"); echo "<br><a href='#' onclick='showDiv(\"S$index\")' style='float: right; margin-top: -6px; margin-right: 8px;'>Show similar linkage</a>"; echo getLinkSimilar($pdo, $row['url'], "S$index");
  18. IDs need to be unique. You cannot have two (or more) elements with the same id. Give them different ids - simple!.
  19. I may be reading it wrong but it looks to me that you are allocating the same id (ie $index) to the linkTree div and the linkSimilar tree.
  20. The simpler you make it the less chance there is of errors.
  21. 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
  22. 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.
  23. Set a UNIQUE constraint on the appointment datetime in your database table. Validate that all the time are on the hour and half-hour.
  24. @phppup if you think that code is a substitute for isset(), then it's time to RTFM again
  25. 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";
×
×
  • 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.