-
Posts
24,345 -
Joined
-
Last visited
-
Days Won
795
Posts posted by Barand
-
-
Set a UNIQUE index on phone. Tablets can all have null values.
EG
CREATE TABLE `customers` ( `id` int(11) NOT NULL AUTO_INCREMENT, `fullName` varchar(50) DEFAULT NULL, `phone` varchar(45) DEFAULT NULL, `pin` int(11) DEFAULT NULL, `device` varchar(45) DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `idx_customers_phone` (`phone`) ) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8; mysql> select * from customers; +----+----------+----------+------+--------+ | id | fullName | phone | pin | device | +----+----------+----------+------+--------+ | 1 | Cust_1 | 12346789 | 1234 | phone | | 2 | Cust_2 | 12356789 | 3456 | phone | | 3 | Cust_3 | 12366789 | 5678 | phone | | 4 | Cust_4 | NULL | NULL | tablet | | 5 | Cust_5 | NULL | NULL | tablet | +----+----------+----------+------+--------+ INSERT INTO customers (fullname,phone,pin,device) VALUES ('Cust_6', 12346789, 1010, 'phone') ON DUPLICATE KEY UPDATE pin = 1010; mysql> select * from customers; +----+----------+----------+------+--------+ | id | fullName | phone | pin | device | +----+----------+----------+------+--------+ | 1 | Cust_1 | 12346789 | 1010 | phone | <-- updated | 2 | Cust_2 | 12356789 | 3456 | phone | | 3 | Cust_3 | 12366789 | 5678 | phone | | 4 | Cust_4 | NULL | NULL | tablet | | 5 | Cust_5 | NULL | NULL | tablet | +----+----------+----------+------+--------+
- 1
-
Following the rule described in the last paragraph, how did user4 and user5 get inserted into two different row-positions?
-
No, you are still pulling the a_id and h_id magically from nowhere.
Try
INSERT INTO match_summ(MatchID ,match_venueID,stadium_LocationID,SeasonID,CompetitionID,Team_catId) SELECT 14,6,7,8,9, IFNULL(team_catid, 0) FROM ( SELECT a_id as teamid FROM matches UNION SELECT h_id as teamid FROM matches ) teams LEFT JOIN teamcat tc USING (teamid);
I'd be interested in seeing the entire data model for this application. It has a very weird feel to it.
-
Include those columns in the query selection and add them to the output of the results.
-
if the awayteam id a_id or hometeam id h_id exists in teamcat table then set Teamcat_id to the value of Teamcat_id
CASE WHEN teamcat.TeamID = h_id THEN Teamcat_id=23
Which is it?
-
According to your data, none of the teams in the matches table have a corresponding team_catid in the catID table. Is this a possible situation?
-
You peeped!
Your connection code is using the obsolete mysql_ library. Use mysqli_ or PDO connection
-
Make the primary key (pet_type, pet_id) where the pet_id is auto_increment. (This increments it within the pet type) EG
mysql> CREATE TABLE `pet` ( -> `user_id` int(11) DEFAULT NULL, -> `pet_type` varchar(45) NOT NULL, -> `pet_id` int(11) NOT NULL AUTO_INCREMENT, -> PRIMARY KEY (`pet_type`,`pet_id`) -> ) ENGINE=MyISAM DEFAULT CHARSET=utf8; Query OK, 0 rows affected (0.34 sec) mysql> INSERT INTO pet (user_id, pet_type) VALUES -> (1, 'cat'), -> (2, 'cat'), -> (2, 'dog'), -> (3, 'cat'), -> (4, 'dog'); Query OK, 5 rows affected (0.01 sec) Records: 5 Duplicates: 0 Warnings: 0 mysql> SELECT * FROM pet; +---------+----------+--------+ | user_id | pet_type | pet_id | +---------+----------+--------+ | 1 | cat | 1 | | 2 | cat | 2 | | 2 | dog | 1 | | 3 | cat | 3 | | 4 | dog | 2 | +---------+----------+--------+
-
Two methods. Loop through the query results
1 ) When the occupation changes, start a new table (hence the order by occupation)
or
2 ) store the results in an array indexed by occupation
This is the second method (you may peep if you get stuck)
$sql = "select sname , fname , foccup from nroll where fimp = 'Y' order by foccup, sname"; $res = $db->query($sql); # # store data in arrays by occupation # $data = []; foreach ($res as $student) { $data[$student['foccup']][] = $student; } # # now loop through the array data # creating your tables # foreach ($data as $occup => $students) { # new occupation table echo "<table border='1' style='border-collapse:collapse'> <tr><th colspan='2'>$occup</th></tr> <tr><th>Student Name</th><th>Fathers Name</th></tr>\n"; # output student rows foreach ($students as $student) { echo "<tr><td>$student[sname]</td><td>$student[fname]</td></tr>\n"; } echo "</table><br>\n"; }
And this is the first method (again, no peeping)
$sql = "select sname , fname , foccup from nroll where fimp = 'Y' order by foccup, sname"; $res = $db->query($sql); # # process data, starting new table on change of occupation # $current_occupation=''; foreach ($res as $student) { if ($student['foccup'] != $current_occupation) { if ($current_occupation) { echo "</table><br>\n"; // end previous table } # start new table echo "<table border='1' style='border-collapse:collapse'> <tr><th colspan='2'>$student[foccup]</th></tr> <tr><th>Student Name</th><th>Fathers Name</th></tr>\n"; $current_occupation = $student['foccup']; } echo "<tr><td>$student[sname]</td><td>$student[fname]</td></tr>\n"; } echo "</table><br>\n"; // end final table
-
1 ) GROUP BY foccup will give you one row for each value of foccup. Use ORDER BY.
2 ) Functions beginning with mysql_ are obsolete and have been removed from PHP. Use PDO (or mysqli_ functions)
-
Welcome to a fellow fortyniner.
-
A prerequisite to writing a working query is knowledge of the table structures and relationships.
So how about providing that information?
-
Best way is to sidestep the problem and use a datepicker component. Then you are not at the mercy of whatever format a user wants to enter.
-
If your table headings show the occupation, why the occupation column in the table?
-
It's the short term memory that goes fir ...... what was I saying?
-
It's guaranteed with my code - the hour is the array key
-
Even assembler - treated as an address (array name equivalent) and an offset (index equivalent) from that address
-
Provided that you can spell "February", only two of those formats do not work
$dates = [ 'feb 21, 1999', 'February 21, 1999', '02/21/99', '2/21/99', '99/2/21', '2-21-1999', '19990221', 'sun, Feb 21, 1999', 'Sunday February 21, 1999' ]; echo '<pre>'; foreach ($dates as $dstr) { printf("%-30s%-15s%s\n", $dstr, date('Y-m-d', strtotime($dstr)), strtotime($dstr)==0 ? 'X' : '' ); }
Outputsfeb 21, 1999 1999-02-21 February 21, 1999 1999-02-21 02/21/99 1999-02-21 2/21/99 1999-02-21 99/2/21 1970-01-01 X 2-21-1999 1970-01-01 X 19990221 1999-02-21 sun, Feb 21, 1999 1999-02-21 Sunday February 21, 1999 1999-02-21
For rogue formats you can always use DateTime::createFromFormat() -
Each time the status changes you need to store the status and timestamp in a new record. You may even want to store old_status|new_status|timestamp. In addition, each record will need the id of whatever the status refers to.
-
But the only table reference by that query is "match_summ" (the one you are inserting into).
As mentioned, what are you trying to do?
-
... it keeps showing me the errmsg.
What error message? Give us a clue.
-
Have you tried it without the WHERE line? That will give all scores with ranks.
Is $udid numeric? If not, it needs to be inside single quotes. (Really it shouldn't be in the query at all, it should be a prepared query with a placeholder for the udid parameter. But that's another lesson.
-
Sorry - I have no a crystal ball.
-
You can do something like this
my data
mysql> select * from score; +-------+-------+ | name | score | +-------+-------+ | Bob | 75 | | David | 106 | | Jane | 75 | | Joe | 61 | | Mary | 59 | | Mike | 61 | | Sam | 76 | +-------+-------+ 7 rows in set (0.00 sec)
query
SELECT a.name , a.score , COUNT(b.name)+1 as rank , tot FROM score a JOIN (SELECT COUNT(name) as tot FROM score) as total LEFT JOIN score b ON b.score > a.score WHERE a.name = 'Jane' -- OPTIONAL GROUP BY a.name ORDER BY a.score DESC; +------+-------+------+-----+ | name | score | rank | tot | +------+-------+------+-----+ | Jane | 75 | 3 | 7 | +------+-------+------+-----+
Taking form data directly to an array?
in PHP Coding Help
Posted
You don't show the full form html, so I am guessing that each height/weight pair is in a separate form and not, as they should be, all in the same form with a single submit.