-
Posts
24,344 -
Joined
-
Last visited
-
Days Won
795
Posts posted by Barand
-
-
You need to implement the method I gave you in PHP. For instance, executing that sql query would be a good move.
-
Your db connection code needs adjustment
//make connection to DB server // call mysql_report so that all errors are reported as execptions // this saves you from having to check every mysql function call to see if it worked or not mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT); $connS = mysqli_connect($servernameS, $usernameS, $passwordS, $databaseS);
Now you can remove your code which checks for errors and replace your insert query with the coed I gave you
-
Use try .. catch, for example
// pseudocode - in case you didn't notice)... try { insert the record // attemp the insert } catch (exception) { if (exception error code is 1062) { // error detected - was it a duplicate? output your duplicate record message // yes it was so report it } else { throw (exception) // no it wasn't so let php handle the exception } }
-
Something like...
<!DOCTYPE html> <html lang="en"> <head> <title>Example</title> <meta charset="utf-8"> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script type='text/javascript'> $(function() { $(".form-button").click(function(e) { e.preventDefault() // stop button from submitting the form alert("Button " + $(this).val() + " clicked") }) }) </script> <style type='text/css'> #myform { padding: 30px; width: 330px; margin: 20px auto; border: 1px solid blue; text-align: center; } </style> </head> <body> <form id='myform' > <button class='form-button' name='form-button' value='1'>Button 1</button> <button class='form-button' name='form-button' value='2'>Button 2</button> <button class='form-button' name='form-button' value='3'>Button 3</button> <br><br> <button>Submit</button> </form> </body> </html>
-
- 1
-
10 minutes ago, nearenough1 said:
so if the above requires 2 arguments how do i do that please?
Following the link to the required page in the manual that I provided would be a start.
-
RTFM
mysqli_query() requires 2 arguments.
There is more to switching from mysql_xxx() to mysqli_xxx() than just adding an "i". They are completely different animals.
-
Take look at simpleXML.
- 1
-
Is this the effect you are looking for?
<head> <style type='text/css'> .curved-bottom { clip-path: ellipse(100% 60% at 50% 40%); background-color: #FFFF00; color: black; } .w3-row { background-color: black; color: white; } .w3-col { padding: 50px 0; text-align: center; } </style> </head> <body> <div class='w3-row'> <div class='w3-col m12 curved-bottom'> I have a curvy bottom </div> </div> <div class='w3-row'> <div class='w3-col m12'> I'm a straight guy </div> </div> </body>
- 1
-
The only MyIsam-only functionality that I can think of is the ability to have a compound primary key EG
PRIMARY KEY (year, number)
where the 2nd part auto_increments within the first part, so if you have
CREATE TABLE `test1` ( `year` int(11) NOT NULL, `number` int(11) NOT NULL AUTO_INCREMENT, PRIMARY KEY (`year`,`number`) ) ENGINE=MyISAM ; mysql> select * from test1; +------+--------+ | year | number | +------+--------+ | 2022 | 1 | | 2022 | 2 | +------+--------+ mysql> insert into test1 (year) values (2022), (2022), (2023), (2023), (2024); mysql> select * from test1; +------+--------+ | year | number | +------+--------+ | 2022 | 1 | | 2022 | 2 | | 2022 | 3 | | 2022 | 4 | | 2023 | 1 | | 2023 | 2 | | 2024 | 1 | +------+--------+
- 2
-
17 hours ago, Barand said:
INSERT queries do not have a WHERE clause
17 hours ago, Strider64 said:There's no `WHERE` in `INSERT INTO`
2 hours ago, Moorcam said:Only SELECT queries have WHERE clauses mate.
There is definitely an echo in here.
@Moorcam WRONG! What about UPDATE and DELETE queries?
-
24 minutes ago, kiril86 said:
I dont't want to access it at all- wihtout accessing it through localhost i want automatically to post the data at the database.
Write it as a CLI script and use a CRON job to run it as frequently as required
-
47 minutes ago, kiril86 said:
I don't get any mistakes
Then try running it when it isn't December or before 6am.
-
INSERT queries do not have a WHERE clause. You need to to use an "INSERT ... ON DUPLICATE KEY UPDATE ... "
EG
INSERT INTO table (id, col_a, col_b) VALUES (?, ?, ?) ON DUPLCATE KEY UPDATE col_a = VALUES(col_a), col_b = VALUES(col_b) ;
-
If this is your insert query...
INSERT INTO `Customer` (`SyncroID`, `PrimaryContactID`, `CompanyName`, `Address`, `City`, `State`, `Postcode`, `Country`, `AccountStatus`, `UserName`, `Password_Hash`, `Password_Salt`, `Notes`, `BillingContactID`) VALUES ('12321', NULL, 'test', NULL, NULL, 'QLD', NULL, 'Australia', 4, NULL, NULL, NULL, NULL, NULL);
...there is only a 50% correlation between those column names and the data keys in the customer array...
-------------------------------------+----------------------------------------- | Matched | Unmatched | -------------------------------------+----------------------------------------- $customer["id"]; $customer["firstname"]; $customer["lastname"]; $customer["business_name"]; $customer["email"]; $customer["address"]; $customer["phone"]; $customer["city"]; $customer["mobile"]; $customer["state"]; $customer["business_and_full_name"]; $customer["zip"]; $customer["business_then_name"]; $customer["fullname"];
-
Firstly, these lines of code do absolutely nothing...
$customer["id"]; $customer["firstname"]; $customer["lastname"]; $customer["fullname"]; $customer["business_name"]; $customer["email"]; $customer["phone"]; $customer["mobile"]; $customer["address"]; $customer["city"]; $customer["state"]; $customer["zip"]; $customer["business_and_full_name"]; $customer["business_then_name"];
Secondly, you are using the least efficient means of inserting data that there is. Single inserts are slow. Marginally faster are prepared statements where you prepare once then execute in a loop with new value for each call. By far the fastest way is use multiple inserts.
EG INSERT INTO table (id, name) VALUES (1, 'Name A'), (2, 'Name B'), (3, 'Name 3');
To implement...
$data = []; foreach ($customers as $c) { $data[] = sprintf("( %d, '%s')", $c['id'], $c['business_name']); } $pdo->exec("INSERT INTO Customer (SyncroID, CompanyName) VALUES " . join(',', $data));
-
Are you sure you don't revisit index.php between runs of page.php?
Might be safer if you only set the value if is not already set
if (!isset($_SESSION['abc']) { $_SESSION['abc'] = rand(10000, 99999); }
-
-
@Senthilkumar is this any faster than yours?
It should get all the data you need in a single query.
<?php $sql = "SELECT c.branch , c.printed , c.pending , c.cancelled , c.over7days , c.reminder , y.year , y.yrtotal , y.yrcount , m.month , m.mthtotal , m.mthcount FROM ( SELECT c.branch , sum(status=1) as printed , sum(status=0) as pending , sum(status=2) as cancelled , sum(status=0 AND today <= CURDATE() - INTERVAL 7 DAY) as over7days , sum(first='0000-00-00' AND date <= CURDATE() - INTERVAL 80 DAY) as reminder FROM calibrationdata1 c JOIN userdetails1 u USING (branch) WHERE u.id = ? GROUP BY branch ) c JOIN ( SELECT branch , date_format(date, '%Y') as year , sum(amount) as yrtotal , count(*) as yrcount FROM calibrationdata1 GROUP BY branch, year(date) ) y USING (branch) JOIN ( SELECT branch , date_format(date, '%b') as month , sum(amount) as mthtotal , count(*) as mthcount FROM calibrationdata1 WHERE year(date) = year(curdate()) GROUP BY branch, month(date) ) m USING (branch) "; $res = $pdo->prepare($sql); // NOTE: PDO connection in use $res->execute([ $_SESSION['id'] ?? 16 ]); $counts = []; $yrdata = []; $monthdata = []; foreach ($res as $row) { $counts = array_slice($row, 0, 6); $yrdata[$row['year']] = array_slice($row, 6,3); $monthdata[$row['month']] = array_slice($row, 9); } echo "<table style='width: 600px; margin: 20px; text-align: center'>" . "<tr><th>" . join('</th><th>', array_keys($counts)) . "</th></tr>" . "<tr><td>" . join('</td><td>', array_values($counts)) . "</td></tr>" . "</table>"; echo printArray($yrdata); echo printArray($monthdata); function printArray($arr) { $out = "<table style='width: 200px; margin: 20px; text-align: center'>" . "<tr><th>" . join('</th><th>', array_keys(current($arr))) . "</th></tr>"; foreach ($arr as $a) { $out .= "<tr><td>" . join('</td><td>', array_values($a)) . "</td></tr>"; } $out .= "</table>\n"; return $out; } ?>
Example output
- 1
-
Yess Phill, you are correct but there will only be a single branch.
My original query was
SELECT branch , sum(status=1) as printed , sum(status=0) as pending , sum(status=2) as cancelled , sum(status=0 AND today <= CURDATE() - INTERVAL 7 DAY) as over7days , sum(first='0000-00-00' AND date <= CURDATE() - INTERVAL 80 DAY) as reminder FROM calibrationdata c WHERE branch = ?
but I changed it on posting to demonstrate that he should be joining to the userdetails table intead of the first query to get the branch
-
Try this to replace your first 6 queries
SELECT u.branch , sum(status=1) as printed , sum(status=0) as pending , sum(status=2) as cancelled , sum(status=0 AND today <= CURDATE() - INTERVAL 7 DAY) as over7days , sum(first='0000-00-00' AND date <= CURDATE() - INTERVAL 80 DAY) as reminder FROM calibrationdata c JOIN userdetails u USING (branch) WHERE u.id = 16; +--------+---------+---------+-----------+-----------+----------+ | branch | printed | pending | cancelled | over7days | reminder | +--------+---------+---------+-----------+-----------+----------+ | Mumbai | 8 | 3 | 0 | 3 | 3 | +--------+---------+---------+-----------+-----------+----------+
-
Look at the results from your first query.
- How many rows are returned?
- How many fetches do you perform?
-
Alternatively
usort($data, fn($a, $b) => isoDate($a) <=> isoDate($b)); echo $data[0]; //--> 03/11/2023 06 This is another text in the line function isoDate($text) { $d = substr($text, 0, 10); return DateTime::createFromFormat('d/m/Y', $d)->format('Y-m-d'); }
- 1
-
use usort() with a callback function which converts the first 10 chars of each line to yyyy-mm-dd format and compares them.
Php Mysql make duplicate error customized
in PHP Coding Help
Posted
The code you post doesn't insert anything, anywhere. All is does is define the content of the string $sql.
Are all three databases on the same server (host) or are you connecting to three separate hosts?