-
Posts
24,604 -
Joined
-
Last visited
-
Days Won
830
Everything posted by Barand
-
SELECT whatever FROM mytable WHERE DATE(timestamp) = CURDATE()
-
What does this SQL query output? DESCRIBE staff;
-
Why are you doing a select query then checking the date and updating if not today. All you need is single update similar to this UPDATE staff SET clockedIn = 0, clockedOut = 0 WHERE indate <> CURDATE()
-
If you don't use table3 you need to remove these bits... The resulting query gives +---------------+--------------------------+ | payment_total | items | +---------------+--------------------------+ | 88 | RH Beer | | 125 | RH Beer, Nokia3210 | | 115 | 22Wine, IPhone4, RH Beer | +---------------+--------------------------+ Of course you can't tell to whom each set of items belong as the name came from table3
-
"distinct" is a keyword, not a function. You cannot apply it to a single column.
-
What you mean is "will I do all your thinking for you?"
-
I posted it an hour ago
-
Remove references to table3 from the query.
-
Tell the creator to Google "database normalization" and try an couple of tutorials. If you use a subquery which effectively puts table3 in the normalized form it should've had, which is Table3 +------+------------+ | name | payment_id | +------+------------+ | Lim | 11 | | Jhon | 2 | | Leo | 1 | +------+------------+ then it works. Without the subquery it doubles or trebles the totals and item lists. SELECT SUM(payment) as payment_total , name , GROUP_CONCAT(item_name SEPARATOR ', ') as items FROM table1 JOIN table2 USING (item_id) JOIN ( SELECT DISTINCT name , payment_id FROM table3 ) t3 USING (payment_id) GROUP BY payment_id; +---------------+------+--------------------------+ | payment_total | name | items | +---------------+------+--------------------------+ | 88 | Leo | RH Beer | | 125 | Jhon | Nokia3210, RH Beer | | 115 | Lim | 22Wine, RH Beer, IPhone4 | +---------------+------+--------------------------+
-
The table1 - table2 relationship is fine but each row in table1 matches several rows in table3 instead of just one. As this is hypothetical case (don't know what it represents) there is no way to know why the weird structure of table3 and the purpose of the payment_id columns in tables 1 and 3. Those relationships won't give what you want without a messy query.
-
You forgot to show your query where "group_concat is not working" EDIT: That peculiar data model that you have needs work.
-
Seems OK except for You need to call session_start() at beginning of every page that uses $_SESSION. As $_SESSION is always set, your code never calls it. $logoutAction - a lot of messing to set a variable that never gets used. Why not just check if $_GET['logout'] is set and equal to "true" mysql_* functions have been deprecated for years and now (since 7.0) no longer exist. Use PDO functions instead. Used prepared statements instead of putting variables directly into the query. You don't appear to be connecting to a database server anywhere. The only session variable you should be storing is the username. You set $logoutGoTo then immediately check it has a value - why? You should have error reporting turned on.
-
Firefox: right click ==> Select "Desktop" folder, name file and save. So you can - with Firefox and Chrome anyhow. (not Edge, surprise, surprise)
-
An alternative to the 2-table option is to treat costs as transactions, just like payments (cost amounts +ve, payment amounts -ve in this example)... DATA TABLE: payment +------+------+------------+--------------+---------+ | uid | name | trans_date | payment_type | payment | +------+------+------------+--------------+---------+ | 1 | kim | 2020-03-01 | cost | 100 | | 1 | kim | 2020-03-02 | card | -100 | | 2 | lee | 2020-03-01 | cost | 95 | | 2 | lee | 2020-03-02 | cash | -95 | | 3 | kent | 2020-03-01 | cost | 100 | | 3 | kent | 2020-03-03 | cash | -50 | | 3 | kent | 2020-03-04 | card | -50 | | 4 | iya | 2020-03-01 | cost | 80 | | 4 | iya | 2020-03-05 | cash | -40 | | 4 | iya | 2020-03-06 | card | -20 | +------+------+------------+--------------+---------+ then SELECT uid , name , date , cost , cash , card , total as balance FROM ( SELECT name , DATE_FORMAT(trans_date, '%b %D') as date , CASE payment_type WHEN 'cash' THEN -payment ELSE '-' END as cash , CASE payment_type WHEN 'card' THEN -payment ELSE '-' END as card , CASE payment_type WHEN 'cost' THEN payment ELSE '-' END as cost , @tot := CASE @previd WHEN uid THEN @tot+payment ELSE payment END as total , @previd := uid as uid FROM ( SELECT * FROM payment ORDER BY uid, trans_date ) sorted JOIN (SELECT @previd:=0, @tot:=0) initialize ) recs; +------+------+---------+------+------+------+---------+ | uid | name | date | cost | cash | card | balance | +------+------+---------+------+------+------+---------+ | 1 | kim | Mar 1st | 100 | - | - | 100 | | 1 | kim | Mar 2nd | - | - | 100 | 0 | | 2 | lee | Mar 1st | 95 | - | - | 95 | | 2 | lee | Mar 2nd | - | 95 | - | 0 | | 3 | kent | Mar 1st | 100 | - | - | 100 | | 3 | kent | Mar 3rd | - | 50 | - | 50 | | 3 | kent | Mar 4th | - | - | 50 | 0 | | 4 | iya | Mar 1st | 80 | - | - | 80 | | 4 | iya | Mar 5th | - | 40 | - | 40 | | 4 | iya | Mar 6th | - | - | 20 | 20 | +------+------+---------+------+------+------+---------+
-
With Firefox you can right click on the page and "Save as...". With Edge, ???
-
I totally agree with @requinix regarding the two tables. However, if you are willing to compromise over the output, you could do something like this SELECT uid , name , SUM(CASE payment_type WHEN 'cash' THEN payment ELSE 0 END) as cash , SUM(CASE payment_type WHEN 'card' THEN payment ELSE 0 END) as card , cost , cost-SUM(payment) as balance FROM payment GROUP BY uid +------+------+------+------+------+---------+ | uid | name | cash | card | cost | balance | +------+------+------+------+------+---------+ | 1 | kim | 0 | 100 | 100 | 0 | | 2 | lee | 95 | 0 | 95 | 0 | | 3 | kent | 50 | 50 | 100 | 0 | | 4 | iya | 40 | 20 | 80 | 20 | +------+------+------+------+------+---------+ If you really need every transaction listed, the SQL becomes quite complex involving user variables and subqueries. It would be much easier to do in the PHP as you output each row. [EDIT] ... For the sake of completeness SELECT uid , name , cash , card , cost , cost-total as balance FROM ( SELECT name , CASE payment_type WHEN 'cash' THEN payment ELSE 0 END as cash , CASE payment_type WHEN 'card' THEN payment ELSE 0 END as card , cost , @tot := CASE @previd WHEN uid THEN @tot + payment ELSE payment END as total , @previd := uid as uid FROM ( SELECT * FROM payment ORDER BY uid ) sorted JOIN (SELECT @previd:=0, @tot:=0) initialize ) recs; +------+------+------+------+------+---------+ | uid | name | cash | card | cost | balance | +------+------+------+------+------+---------+ | 1 | kim | 0 | 100 | 100 | 0 | | 2 | lee | 95 | 0 | 95 | 0 | | 3 | kent | 50 | 0 | 100 | 50 | | 3 | kent | 0 | 50 | 100 | 0 | | 4 | iya | 40 | 0 | 80 | 40 | | 4 | iya | 0 | 20 | 80 | 20 | +------+------+------+------+------+---------+
-
Variable names! while($rows = $resultset->fetch_assoc()) ^^^^ { echo "<option value='{$row[course_name]}'>{$row['course_name']}</option>"; ^^^ ^^^ } And the value should be the course_id
-
You don't have to create a web page on an iPad to be able to view it in a browser on an iPad
-
In future, use the <> button in the toolbar when posting code. (I've done it for you this time) In your query SELECT exam_time ,count() FROM test_booking_confirm WHERE DATEDIFF('exam_date',DATE_FORMAT('"+$date+"','%m-%d-%Y'))=0 GROUP BY exam_date,exam_time HAVING count()>1 the WHERE clause is FUBAR. when storing or working with dates in a db the format should be yyyy-mm-dd (DATE type) in PHP, the concatenation operator is a "." and not a "+" column names should not be in single quotes user-provided data should not be put directly into the query, used prepared statements Assuming the exam_date is the correct DATE type and format, make sure $date is the correct format EG $date = date('Y-m-d'); $stmt = $conn->prepare("SELECT exam_time FROM test_booking_confirm WHERE exam_date = ? GROUP BY exam_date,exam_time HAVING count(*)>1 "); $stmt->bind_param('s', $date); $stmt->execute(); $stmt->bind_result($exam_time); $slots=array(); while($stmt->fetch { $slots[] = $exam_time; } But, personally, I'd look for slots with a count < 2 and just display those available.
-
@KHS I agree that SELECT * should not be used, but those "rows" you mentioned are "columns".
-
Don't use deprecated html markup, such as font color. Use style attribute. echo "<p style='font-size:4pt; color:#ffffff'>$echo</p>"; or define a class for those links in your css .mylink { font-size: 4pt; color: #FFFFFF; } then echo "<p class='mylink'>$echo</p> NOTE: I have no way of knowing what $echo contains. If it contains <a> tag then the styling will need to be applied there and not to the <p>
-
Take $values out of the subject
-
Your script needs a "base time" - the time from which you are counting. Instead of always starting your timer from 00:00:00 you would first calculate the time elapsed from the base time and start from there. EG base time = 09:00, and it is now 11:10:35, so your timer should start from "02:10:35" when it loads. If you next open the page at midday then the timer shoud start from "03:00:00"
- 5 replies
-
- javascript js
- timer
-
(and 2 more)
Tagged with:
-
Input fields also work better when they have "name" attributes