check if data already exist inside mysql database
<p>i am new in PHP so i am struggling to solve this issue.</p>

<p>here is my code which stores the variable values in specific table rows inside the table "booked".</p>

<p>i want to make sure mysql rejects the insertion of the value inside the database table "booked" if all the variable values already exist inside the database.</p>

<p>note: all the values have to be identical to be rejected. for example: if all the values are identical except for one value, the system should still accept the insertion.</p>

<p>the code i wrote below first checks if the data is already present inside the database. if it is present it echos "data already exists", if not then it inserts the new value inside the database.</p>

<p>however this code doesn't seem to work, so i was wondering if you guys could check my code to see what i am doing wrong?</p>

<pre><code>     &lt;?php

        $servername = "localhost";
        $name = "root";
        $password = "root";
        $dbname = "my computer";

        // Create connection
        $conn = mysqli_connect($servername, $name, $password, $dbname);
        // Check connection
        if (!$conn) {
           die("Connection failed: " . mysqli_connect_error());
        }



        $query = "SELECT * FROM `booked` WHERE 

        `name` = '{$username}'
        `date` = '{$date}'
        `computer_id` = '{$select3}'
        `start_time` = '{$select1}'
        `end_time` = '{$select2}'
        `room` = '{$room}'

        ";

        $result = mysqli_query($conn, $query);



        if ( mysqli_num_rows ( $result ) &gt; 1 )
        {

        echo "data already exists";

        }




        else
        {
        $sql = "INSERT INTO booked (date, computer_id, name, start_time, end_time, room)
            VALUES ('$date', '$select3', '$username', '$select1', '$select2', '$room')";



        if (mysqli_query($conn, $sql)) {
           echo "New record created successfully";
            } else {
           echo "Error: " . $sql . "&lt;br&gt;" . mysqli_error($conn);
            }

        mysqli_close($conn);

        }
        ?&gt;
</code></pre>