Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I added (INSERT) one Volvo ($car) with and IP ($ip). (Volvo, 200.0.0.01, 1)
F5 then I UPDATE Volvo with my IP (Volvo, 200.0.0.01, 2)
Update it again with Volvo and IP (Volvo, 200.0.0.01, 3)
and so on
another man adds Volvo+IP (Volvo, 300.5.0.07, 1)
another man adds VW+IP (VW, 500.2.0.04, 1)
and so on
Do you understand what I mean?
database:
Quote:
CREATE TABLE cars (
cars varchar(100) NOT NULL,
ip varchar(15) NOT NULL,
count mediumint(8) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
if($car && $ip) { //car and ip already exists, then UPDATE ...
$sql_update="UPDATE cars
SET count = ".++$count."
WHERE car = '$car'"
AND ip = '$ip'";
$sql_update = mysqli_real_escape_string($link, $sql_update); //boughtonp (injection)
}
else { //car and ip doesn't exist the first time, then INSERT ...
$sql_insert = "INSERT INTO cars (car, ip, count)
VALUES ('".$car."', '".$ip."', 1);";
$sql_update = mysqli_real_escape_string($link, $sql_update); //boughtonp (injection)
if(mysqli_query($link, $sql_insert)) {
echo "<br>Records inserted successfully.";
}
else {
echo "<br>ERROR: Could not able to execute $sql_insert.".mysqli_error($link)." ".mysqli_errno($link);
} //end if mysqli_query
} //end if(car && ip)
// Close connection
mysqli_free_result($link);
mysqli_close($link);
PHP Version 7.4.3
System Linux ubuntu-2gb-nbg1-1 5.4.0-45-generic #49-Ubuntu SMP Wed Aug 26 13:38:52 UTC 2020 x86_64
Build Date May 26 2020 12:24:22
Server API FPM/FastCGI
...
I haven't time, until now. I can't edit the first time on code anymore post6163783. But I have update code here.
https://cars.com/cars.php?cars=volvo
When there is new car with new IP, then INSERT, but only 1 INSERT. The rest is UPDATE with the same car and same IP. I wish that it works:
Quote:
CAR - IP - TODAY - COUNT
volvo - 104.00.01.02 - 2020-09-27 16:08 - 1 //insert once
another day:
Quote:
CAR - IP - TODAY - COUNT
volvo - 104.00.01.02 - 2020-09-28 16:08 - 2 //update
lada - 104.00.01.02 - 2020-09-28 19:08 - 1 //insert once
fiat - 54.90.61.43 - 2020-09-29 16:08 - 1 //insert once
But it works not. Now there is only Records were updated successfully UPDATE cars SET count = 1 WHERE ip = '104.00.01.02' AND car='volvo'. I have deleted also with volvo+ip, but still it has updated successfully
Code:
<?php
//prepare
set_time_limit(0);
ini_set('memory_limit', -1);
$car = htmlspecialchars($_GET["car"]);
$ip = htmlspecialchars($_SERVER['REMOTE_ADDR']);
$link = mysqli_connect(/*...*/); //success
if (mysqli_connect_errno()) {
var_dump("<br>Connect failed: %s\n", mysqli_connect_error());
exit();
}
//select, list
$count = 0;
$sql_select = "SELECT car, ip, today, count
FROM cars";
if($result = mysqli_query($link, $sql_select)) {
if(mysqli_num_rows($result) > 0) {
echo "<table>";
echo "<tr>";
echo "<th>car</th>";
echo "<th>ip</th>";
echo "<th>today</th>";
echo "<th>count</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['car'] . "</td>";
echo "<td>" . $row['ip'] . "</td>";
echo "<td>" . $row['today'] . "</td>";
echo "<td>" . $row['count'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_free_result($result);
}
else {
echo "<br>No records matching your query were found $sql_select ".mysqli_error($link)." ".mysqli_errno($link);
}
}
else {
echo "<br>ERROR: Could not able to execute $sql_select " . mysqli_error($link)." ".mysqli_errno($link);
}
//insert (only 1 car+ip), or update (2 or rest)
$count = 0;
$sql_select = "SELECT car, ip, today, count
FROM cars
WHERE car = '".$car."'
AND ip = '".$ip."'";
if ($result = mysqli_query($link, $sql_select)) {
$row_cnt = mysqli_num_rows($result);
printf("<br>Result set has %d rows.\n", $row_cnt);
//Result set has 0 rows.
$sql_update="UPDATE cars
SET count = ".(++$count)."
WHERE ip = '".$ip."'
AND car='".$car."'";
echo "<br>sql_update: ".$sql_update;
if(mysqli_query($link, $sql_update)) {
echo "<br>Records were updated successfully $sql_update ".mysqli_error($link)." ".mysqli_errno($link);
//Records were updated successfully UPDATE cars SET count = 1 WHERE ip = '104.00.01.02' AND car='volvo'
}
else {
echo "<br>ERROR: Could not able to execute $sql_update ".mysqli_error($link)." ".mysqli_errno($link);
}
mysqli_free_result($result);
}
else {
$sql_insert = "INSERT INTO cars (car, ip, today, count)
VALUES ('".$car."', '".$ip."', NOW(), 1);";
if(mysqli_query($link, $sql_insert)) {
echo "<br>Records inserted successfully $sql_insert ".mysqli_error($link)." ".mysqli_errno($link);;
}
else {
echo "<br>ERROR: Could not able to execute $sql_insert ".mysqli_error($link)." ".mysqli_errno($link);
}
}
CREATE TABLE cars (
cars varchar(100) NOT NULL,
ip varchar(15) NOT NULL,
today datetime NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE current_timestamp(),
count mediumint(8) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
I am posted with " and without sql.
Code:
$sql_update="UPDATE cars SET count = count+1 WHERE car='$car' AND ip = '$ip'";
if(mysqli_query($link, $sql_update)) {
echo "<br>Records were updated successfully $sql_update ".mysqli_error($link)." ".mysqli_errno($link);
}
else {
echo "<br>ERROR: Could not able to execute $sql_update ".mysqli_error($link)." ".mysqli_errno($link);
}
//Records were updated successfully UPDATE cars SET count = 0+1 WHERE ip = '104.00.01.02' AND car='volvo' 0
//and also with new car, not INSERT yet, but I suppose it will have ....inserted successfully..., but: https://cars.com/cars.php?cars=lada ...updated successfully..., and nothing car (lada+104.00.01.02) will be SELECTed.
@michaelk: Yes I know with date and time, ex. the time here is now 23:41.
sql: volvo - 104.00.01.02 - 2020-09-29 23:41 - 3
or
Code:
$sql_select = "SELECT car, ip, ".date_create('now')->format('Y-m-d H:i:s').", count
FROM cars
WHERE car = '".$car."'
AND ip = '".$ip."'";
...
volvo - 104.00.01.02 - 2020-09-29 23:41 - 3
I have not tested it yet on today datetime NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE current_timestamp()
maybe you can have only today datetime NOT NULL DEFAULT '0000-00-00 00:00:00'.
But still is only ...updated successfully..., not ...inserted successfully... on the first time.
@boughtonp: It is 99% the same on Object oriented style (only mysql), Procedural style (only mysql) and PDO with Prepared Statements (mysql and other sql). I use now procedural style for a more easy and newbie. But again, what exactly will you mean about it? Tiny different style, but it is only this ...updated successfully.... Exactly the same.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.