-
Posts
24,566 -
Joined
-
Last visited
-
Days Won
822
Everything posted by Barand
-
HINT: I am all for 1 or 2 character aliases for tables but you would make your queries more readable with more meaningful aliases for the tables than just a, b, c, d, e EG ssm_job j ssm_venue v ssm_customer c
-
Join twice to the user table with different aliases select a.job_manager_id , jm.username as manager , a.job_head_chef_id , hc.username as chef FROM ssm.job a JOIN user jm ON jm.user_id = a.job_manager_id JOIN user hc ON hc.user_id = a.head_chef_id
-
For example, if you have these tables ... CREATE TABLE `test1` ( `job_id` int(11) NOT NULL AUTO_INCREMENT, `test1_descrip` varchar(45) DEFAULT NULL, PRIMARY KEY (`job_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; CREATE TABLE `test2` ( `test2_id` int(11) NOT NULL AUTO_INCREMENT, `job_id` int(11) DEFAULT NULL, PRIMARY KEY (`test2_id`), KEY `idx_test2_job_id` (`job_id`), CONSTRAINT `test2_fk1` FOREIGN KEY (`job_id`) REFERENCES `test1` (`job_id`) ON DELETE CASCADE ON UPDATE NO ACTION ) ENGINE=InnoDB DEFAULT CHARSET=utf8; CREATE TABLE `test3` ( `test3_id` int(11) NOT NULL AUTO_INCREMENT, `job_id` int(11) DEFAULT NULL, PRIMARY KEY (`test3_id`), KEY `idx_test3_job_id` (`job_id`), CONSTRAINT `test3_fk1` FOREIGN KEY (`job_id`) REFERENCES `test1` (`job_id`) ON DELETE CASCADE ON UPDATE NO ACTION ) ENGINE=InnoDB DEFAULT CHARSET=utf8; CREATE TABLE `test4` ( `test4_id` int(11) NOT NULL AUTO_INCREMENT, `job_id` int(11) DEFAULT NULL, PRIMARY KEY (`test4_id`), KEY `idx_test4_job_id` (`job_id`), CONSTRAINT `test4_fk1` FOREIGN KEY (`job_id`) REFERENCES `test1` (`job_id`) ON DELETE CASCADE ON UPDATE NO ACTION ) ENGINE=InnoDB DEFAULT CHARSET=utf8; ... where test1 is the parent table and tables 2. 3 and 4 are the child tables, each with a foreign key referencing the primary key in the first table BEFORE +--------+---------------+ | job_id | test1_descrip | +--------+---------------+ | 20 | Job 20 | | 21 | Job 21 | | 22 | Job 22 | | 23 | Job 23 | | 24 | Job 24 | | 25 | Job 25 | +--------+---------------+ | +------------------+----------+------------------------------+ | | | +----------+--------+ +----------+--------+ +----------+--------+ | test2_id | job_id | | test3_id | job_id | | test4_id | job_id | +----------+--------+ +----------+--------+ +----------+--------+ | 8 | 21 | | 2 | 20 | | 4 | 21 | | 7 | 24 | | 3 | 21 | | 5 | 22 | +----------+--------+ | 4 | 22 | | 6 | 23 | +----------+--------+ | 7 | 24 | +----------+--------+ Delete query... DELETE FROM test1 WHERE job_id = 21; Records for job 21 also deleted from test2, test3, test4 due to the "ON DELETE CASCADE" settings in the foreign key definitions AFTER +--------+---------------+ | job_id | test1_descrip | +--------+---------------+ | 20 | Job 20 | | 22 | Job 22 | | 23 | Job 23 | | 24 | Job 24 | | 25 | Job 25 | +--------+---------------+ | +------------------+----------+------------------------------+ | | | +----------+--------+ +----------+--------+ +----------+--------+ | test2_id | job_id | | test3_id | job_id | | test4_id | job_id | +----------+--------+ +----------+--------+ +----------+--------+ | 7 | 24 | | 2 | 20 | | 5 | 22 | +----------+--------+ | 4 | 22 | | 6 | 23 | +----------+--------+ | 7 | 24 | +----------+--------+
-
1. array_combine(), as it says on the tin, combines two arrays. These arrays must be of equal length. Yours are not. 2. Sounds like you are attempting to insert a record but not providing the data for a required field (column 'Text'). You need to either provide a value alter the table to define a default value 3. RTFM
-
That was established in the question ??? FROM is part of the DELETE syntax just as it is part of the SELECT syntax and has the same role - defining the table references.
-
As with SELECT statements, INNER JOIN requires matching records. If some tables may not contain the job_id being deleted then you need LEFT JOINS DELETE ssm_job, ssm_menu_order, ssm_equipment_order, ssm_money_order FROM ssm_job LEFT JOIN ssm_menu_order USING (job_id) LEFT JOIN ssm_equipment_order USING (job_id) LEFT JOIN ssm_money_order USING (job_id) WHERE ssm_job.job_id = ?
-
Do all four tables contain a record with job_id = 21?
-
Brackets will indeed be an improvement. You also need to specify the tables you want to delete from. That will only delete from ssm_job.
-
@ginerjm is correct. You cannot pass table or column names as parameters to a prepared query; only data values. It sounds like you need to do something like this (three table example for brevity) DELETE table1, table2, table3 FROM table1 JOIN table2 USING (job_id) JOIN table3 USING (job_id) WHERE table1.job_id = ?
-
Creating executable sql code from html tables
Barand replied to seany1234's topic in PHP Coding Help
Something along these lines... include("path/to/simple_html_dom.php"); $html = file_get_html('mysource.html'); $rows = $html->find('tr[bgcolor="#EEEEEE"]'); $stmt = $conn->prepare("INSERT INTO thetable (uid, ... , etc)" VALUES(?,?,?, ...., etc)); foreach ($rows as $row) { if (strpos($row->children(0), '<th>')!==false) continue; $data = []; foreach ($row->children() as $k => $itm) { $val = $itm->plaintext; switch ($k) { case 6: // DoB case 9: // Expires $data[] = reformatDate($val); break; case 7: // Height case 8: // Weight case 10: // Wage $data[] = numOnly($val); break; case 11: // Value $data[] = reformatValue($val); break; default: $data[] = $val; } } $stmt->execute($data); } function reformatDate($str) { list($dob) = explode(' ', $str); $res = (DateTime::createFromFormat('d/m/Y', $dob))->format('Y-m-d') ; return $res; } function numOnly($str) { $res = ''; for($i=0, $k=strlen($str); $i<$k; $i++) { $res .= ctype_digit($str[$i]) ? $str[$i] : ''; } return $res; } function reformatValue($str) { // // This is left as an exercise for the reader! // return $str; // leave unchanged for now } -
Example pagination code here
-
Creating executable sql code from html tables
Barand replied to seany1234's topic in PHP Coding Help
You would do the required formatting before writing to the csv file. LOAD DATA INFILE command has a "IGNORE N rows" option for ignoring headings. If you don't want to use that LOAD DATA option (which is certainly the fastest and will load hundreds of records per second) then , with a PDO connection and a prepared statement you could change the loop to $stmt = $conn->prepare("INSERT INTO thetable (uid, ... , etc)" VALUES(?,?,?, ...., etc)); foreach ($rows as $row) { $data = []; foreach ($row->children() as $itm) { $val = $itm->plaintext; // do any required reformatting here $data[] = $val; } $stmt->execute($data); } -
Mainly, but it also saves having to escape string data too (For example, if you were trying to add "O'Reilly" as a name value.
-
How to use PHP to log ip, then redirect to url
Barand replied to omegatroller's topic in PHP Coding Help
1. Your code needs to open with <?php 2. Your timezone is invalid, use date_default_timezone_set("Europe/Oslo"); 3. If you exit before the redirect, how can it redirect? 4. The concatenation operator in php is "." and not "+" I recommend you turn error reporting on. -
A time stamp (eg 2019-12-15 19:02:00) is a string value and should therefore be inside single quotes in your query. (This would be the same in C# as it is purely SQL related, not C# or PHP). You should (in either language) be using prepared queries and not embedding variables in the query. For example (using PDO) $stmt = $conn->prepare("UPDATE userData SET timestamp = ? WHERE GUID = ?") $stmt->execute([ $timeStamp, $LoggedIn_guid ]);
-
Creating executable sql code from html tables
Barand replied to seany1234's topic in PHP Coding Help
P.S. Some of the data is unsuitable for data storage purposes and needs to be reformatted before storing in a database table +------------+--------------------------------+----------------+----------------------------------+ | Column | WRONG | CORRECT | Comments | +------------+--------------------------------+----------------+----------------------------------+ | Age | 31 | Not required | DoB tells you the age | | DoB | 3/4/1988 (31 years old) | 1988-04-03 | Store in correct date format | | Height | 194 cm | 194 | Store unformatted numeric value | | Weight | 83 kg | 83 | Store unformatted numeric value | | Expires | 30/6/2022 | 2022-06-30 | Store in correct date format | | Wage | £25,000 p/w | 25000 | Store unformatted numeric value | | Value | £12.75M | 12750000 | Store unformatted numeric value | +------------+--------------------------------+----------------+----------------------------------+ -
Creating executable sql code from html tables
Barand replied to seany1234's topic in PHP Coding Help
Try using domDocument or simple_html_dom. EG include("/path/to/simple_html_dom.php"); $html = file_get_html('my_source.html'); $rows = $html->find('tr[bgcolor="#EEEEEE"]'); $csvFile = fopen('mydata.csv', 'w'); foreach ($rows as $row) { $data = []; foreach ($row->children() as $itm) { $data[] = $itm->plaintext; } fputcsv($csvFile, $data); } fclose($csvFile); The csv file can then be loaded into a sql table using a LOAD DATA INFILE statement. -
You have taken the "|" characters out! How do expect to split it? The echoed string should look like guid|username|health|...|misses I.E. $row = [ 'guid' => 'xyz', 'username' => 'fred', 'health' => 1, 'level' => 42 ]; echo $row['guid'] . '|' . $row['username'] . '|' . $row['health'] . '|' . $row['level']; or (easier if you want every $row item in their natural ORDER) echo join('|', $row); Then when you split it, the array will look like Array ( [0] => xyz [1] => fred [2] => 1 [3] => 42 )
-
If you are splitting on "|", why put the PHP_EOLs in there? Also, try omitting the leading "|" in your string, so you have guid|username|health|...|misses When you split that on "|" the guid should be item 0. With the leading "|" (i.e "| guid | username | …") I would expect item 0 to be blank after the split and guid would be item 1..
-
Unsubscribe script not deleting database entry for user
Barand replied to UAFWolfcry's topic in PHP Coding Help
As you can see from @Psycho's comments, that was your first mistake. -
Unsubscribe script not deleting database entry for user
Barand replied to UAFWolfcry's topic in PHP Coding Help
Put this line mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT); before your mysqli connection line. Errors will then be reported automatically. -
Then use basename()
-
So does it now work OK?
-
You have a database named "*" !?
-
Put this line of code immediately before your line containing mysqli_connect mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT); That should get itt to report automatically any mysqli errors (such as the prepare() failing.