Jump to content

PHP MYSQL - Test if VehicleId exists, and if it does, delete it, and save a new record of it


Zorglub

Recommended Posts

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();

    /////////////////////////////////////////////////////////

 

Link to comment
Share on other sites

            // 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

image.png.8cf12353329762458af45ac243d8c1cd.png

Edited by Zorglub
Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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 by Zorglub
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.