Zorglub Posted September 25, 2021 Share Posted September 25, 2021 Hi, I am trying to make a latest visited cars segment where I save the image and id of the car the the user sees. But I am lost as how to go from here... // DATABASE CONNECTION ///////////////////////////////////// $servername = "localhost"; $username = "root"; $password = "Wichmand14"; $dbname = "visited"; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $dateValue = date('d-m-Y H:i:s'); $imgUrl = $vehicle->Pictures[1]; $getTimeStamp = "SELECT * FROM latest"; $result = $conn->query($getTimeStamp); $sql = "INSERT INTO latest (timestamp, vehicleid, img) VALUES ('$dateValue', '$vehicle->VehicleId', '$imgUrl')"; if ($result->num_rows > 0) { // output data of each row while($row = $result->fetch_assoc()) { if($row['vehicleid'] <> $vehicle->VehicleId) { echo "Timestamp: " . $row['timestamp'] . "<br>"; echo "VehicleId: " . $row['vehicleid'] . "<br>"; } } } if ($conn->query($sql) === TRUE) { echo "New record created successfully"; } else { echo "Error: " . $sql . "<br>" . $conn->error; } $conn->close(); ///////////////////////////////////////////////////////// Quote Link to comment https://forums.phpfreaks.com/topic/313808-php-mysql-test-if-vehicleid-exists-and-if-it-does-delete-it-and-save-a-new-record-of-it/ Share on other sites More sharing options...
Barand Posted September 25, 2021 Share Posted September 25, 2021 (edited) It's a pity you don't have a vehicle table which contains the image url of the vehicle. Edited September 25, 2021 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/313808-php-mysql-test-if-vehicleid-exists-and-if-it-does-delete-it-and-save-a-new-record-of-it/#findComment-1590332 Share on other sites More sharing options...
Zorglub Posted September 25, 2021 Author Share Posted September 25, 2021 49 minutes ago, Barand said: It's a pity you don't have a vehicle table which contains the image url of the vehicle. $temp .= "<img class='cc-images' src='" . $vehicle->Pictures[$p] . "'>"; It's from a JSON file... the URL of the IMG... Quote Link to comment https://forums.phpfreaks.com/topic/313808-php-mysql-test-if-vehicleid-exists-and-if-it-does-delete-it-and-save-a-new-record-of-it/#findComment-1590334 Share on other sites More sharing options...
Zorglub Posted September 25, 2021 Author Share Posted September 25, 2021 (edited) // Testing if exists if($row['vehicleid'] = $vehicle->VehicleId) { $rowcount=mysqli_num_rows($result); echo "Timestamp: " . $row['timestamp'] . "<br>"; echo "VehicleId: " . $row['vehicleid'] . "<br>"; echo "img: " . $row['img'] . "<br>"; } The url example: https://i.ebayimg.com/images/g/FQ0AAOSw8WdbfU5w/s-l1600.jpg Edited September 25, 2021 by Zorglub Quote Link to comment https://forums.phpfreaks.com/topic/313808-php-mysql-test-if-vehicleid-exists-and-if-it-does-delete-it-and-save-a-new-record-of-it/#findComment-1590335 Share on other sites More sharing options...
Barand Posted September 25, 2021 Share Posted September 25, 2021 3 hours ago, Zorglub said: I save the image and id of the car the user sees Is it irrelevant which user sees the car? Quote Link to comment https://forums.phpfreaks.com/topic/313808-php-mysql-test-if-vehicleid-exists-and-if-it-does-delete-it-and-save-a-new-record-of-it/#findComment-1590337 Share on other sites More sharing options...
Zorglub Posted September 26, 2021 Author Share Posted September 26, 2021 Is it irrelevant which user sees the car? There are no users.. Quote Link to comment https://forums.phpfreaks.com/topic/313808-php-mysql-test-if-vehicleid-exists-and-if-it-does-delete-it-and-save-a-new-record-of-it/#findComment-1590352 Share on other sites More sharing options...
Zorglub Posted September 26, 2021 Author Share Posted September 26, 2021 I mean it would be relevant, but I have only registered the cars being visited. Not by whom. Quote Link to comment https://forums.phpfreaks.com/topic/313808-php-mysql-test-if-vehicleid-exists-and-if-it-does-delete-it-and-save-a-new-record-of-it/#findComment-1590358 Share on other sites More sharing options...
Barand Posted September 26, 2021 Share Posted September 26, 2021 22 hours ago, Zorglub said: But I am lost as how to go from here... When you are lost and want directions, it helps if you tell us where you want to go to. Why don't you tell us what you are trying to do. So far you have only told us how you are trying to get there. There could well be a better route. (And perhaps explain what that magical $vehicle object is and where comes from - it just pops up from nowhere in your code) Quote Link to comment https://forums.phpfreaks.com/topic/313808-php-mysql-test-if-vehicleid-exists-and-if-it-does-delete-it-and-save-a-new-record-of-it/#findComment-1590361 Share on other sites More sharing options...
Zorglub Posted September 27, 2021 Author Share Posted September 27, 2021 (edited) On 9/25/2021 at 3:19 PM, Zorglub said: Hi, I am trying to make a latest visited cars segment where I save the image and id of the car the the user sees. But I am lost as how to go from here... // DATABASE CONNECTION ///////////////////////////////////// $servername = "localhost"; $username = "root"; $password = "Wichmand14"; $dbname = "visited"; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $dateValue = date('d-m-Y H:i:s'); $imgUrl = $vehicle->Pictures[1]; $getTimeStamp = "SELECT * FROM latest"; $result = $conn->query($getTimeStamp); $sql = "INSERT INTO latest (timestamp, vehicleid, img) VALUES ('$dateValue', '$vehicle->VehicleId', '$imgUrl')"; if ($result->num_rows > 0) { // output data of each row while($row = $result->fetch_assoc()) { if($row['vehicleid'] <> $vehicle->VehicleId) { echo "Timestamp: " . $row['timestamp'] . "<br>"; echo "VehicleId: " . $row['vehicleid'] . "<br>"; } } } if ($conn->query($sql) === TRUE) { echo "New record created successfully"; } else { echo "Error: " . $sql . "<br>" . $conn->error; } $conn->close(); ///////////////////////////////////////////////////////// I want to get the latest 3 cars viewed. In the to I save the view in til MYSQL and in the code above I read the data I get from saving the vehicleid, the timestamp and the img in the table latest. Edited September 27, 2021 by Zorglub Quote Link to comment https://forums.phpfreaks.com/topic/313808-php-mysql-test-if-vehicleid-exists-and-if-it-does-delete-it-and-save-a-new-record-of-it/#findComment-1590381 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.