-
Posts
24,607 -
Joined
-
Last visited
-
Days Won
831
Everything posted by Barand
-
a href="cocoa_type_chocolate.php?type_chocolate='dark'">Dark</a>
Barand replied to bertrc's topic in PHP Coding Help
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) -
a href="cocoa_type_chocolate.php?type_chocolate='dark'">Dark</a>
Barand replied to bertrc's topic in PHP Coding Help
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. -
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.
- 1 reply
-
- 1
-
-
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.
-
The whole point of prepared statements is not to put variables in the query. Look at my code again.
-
Would you like me to move this thread to the "Politics & Economics" forum?
-
Need help in Checking divisibility of Numbers in Php
Barand replied to Marie's topic in PHP Coding Help
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. -
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 {
-
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
-
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.
-
I don't see your problem. I have given you a solution using @mac_gyver's example.
-
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");
-
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.
-
Set a UNIQUE constraint on the appointment datetime in your database table. Validate that all the time are on the hour and half-hour.
-
@phppup if you think that code is a substitute for isset(), then it's time to RTFM again
-
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";