Ubernator
Ubernator

Reputation: 743

Checking to see if ID is already in database, if it is don't INSERT it again

When I run the page with an empty database, it will insert the data correctly. When I run the page again, it displays there is already an ID in the database, but it inserts it anyway. Not sure how or why but I've tried every combination of booleans inside the if statements and cant get it to chooch correctly.

//pass in an ID to compare:
function checkOrderID($orderID) {
//Connect to the database: 
$mysqli = new mysqli("localhost", "root", "", "price");
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

//Ask the database for some sweet, sweet data: 
$stmt1 = "SELECT orderID FROM orders";
$result = $mysqli->query($stmt1);

//flag (we want to believe that there are no similar IDS so lets make it true):
$flag = true;

//while we got some data, display that shit
while ($row = $result->fetch_assoc()) {
    //asign data to variable:
    $rowOrderID = $row['orderID'];

    //Does it match? if it does set the flag to false so it doesnt get inserted. 
    if ($rowOrderID == $orderID) {
        echo "Row ID" . $row["orderID"] . " Passed ID: " . $orderID . "<br>";
        echo "This order is already in the database" . "<br>";
        $flag = false;
    }
}
//hand the flag over to who ever needs it
return flag;
}

.

if (checkOrderID($orderID) == true) {
    //some mysql insert logic here

}

Upvotes: 1

Views: 1155

Answers (2)

Dallas Caley
Dallas Caley

Reputation: 5888

Why are you making this complicated. just do something like this:

$con=mysqli_connect("localhost","root","","price");
$check_query = mysqli_query($con,"SELECT * FROM orders WHERE orderID = $orderID");
if (mysqli_num_rows($check_query) == 0) {
    //mysql insert logic here
}

(Noted of course you are going to have your connection logic as well)


Note: You are using Mysqli in object oriented manner but in this example i have not used object oriented manner of DB connection. The connection variable $con must be passed to mysqli_query() method.

Also... random side note, but it's generally a good idea to have a password for your root mysql user.

Upvotes: 3

BetaDev
BetaDev

Reputation: 4684

Here better and short, but please try to use DB connection globally not inside your mothod and try to use prepared statements. But except those you can use following code.

//pass in an ID to compare:
function checkOrderID($orderID) {
    //Connect to the database: I suggest use global DB connection 
    $mysqli = new mysqli("localhost", "root", "", "price");
    if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }
    //gets recodrs based on $orderID passed to this method
    $stmt1 = "SELECT * FROM orders where orderID=$orderID"; //Try to use prepared statement
    $result = $mysqli->query($stmt1);
    //Store number of rows found
    $row_count = $result->num_rows;
    if($row_count>0){
        return true;
    }
    else{
        return false;
    }
}

Upvotes: 1

Related Questions