tags:

views:

75

answers:

3

I was wondering how can I check a value from an array to see if its in the database if it is don't added to the database again. How would I be able to do this using PHP & MySQL?

PHP code.

for ($x = 0; $x < count($cat_id); $x++){
    $cat_query = "INSERT INTO posts_categories (category_id, post_id, date_created) VALUES ('" . mysqli_real_escape_string($mysqli, strip_tags($cat_id[$x])) . "', '" . mysqli_real_escape_string($mysqli, strip_tags($post_id)) . "', NOW())";
}

Revise PHP code.

    if(isset($cat_id)){

        for($x = 0; $x < count($cat_id); $x++){
            $check_query = mysqli_query($mysqli,"SELECT category_id FROM posts_categories WHERE category_id = '" . $cat_id[$x] ."' AND post_id = '" . $post_id . "'");

            if ($check_query == TRUE) {
                unset($cat_id[$x]);
            }
        }


        for($x = 0; $x < count($cat_id); $x++){
            $cat_query = "INSERT INTO posts_categories (category_id, post_id, date_created) VALUES ('" . mysqli_real_escape_string($mysqli, strip_tags($cat_id[$x])) . "', '" . mysqli_real_escape_string($mysqli, strip_tags($post_id)) . "', NOW())";
        }
    }
+1  A: 

You can use INSERT .. ON DUPLICATE UPDATE.

troelskn
Will this stop me from adding or updating the table row?
help
No, it tells MySql that you want to insert a row, but if it already exists (as determined per the primary key, which in this case probably should be `category_id` + `post_id`), it should not attempt to insert a new record, but instead update the existing one. You would typically update a column `updated_at` or similar in the update part of the query.
troelskn
i want to stop from adding or updating the table row with the duplicate value.
help
there is INSERT IGNORE.. (see below)
Kieran Allen
Ah yes - `INSERT .. IGNORE` is probably better in this case.
troelskn
A: 
$sql = "SELECT * FROM your_table WHERE your_column='".$yourArray['value']."'";
if(!mysql_num_rows(mysql_query($sql))){
    // no rows so add it to the database...
}
Thomas Clayson
didn't work for me :(
help
can you post the code you used here please?
Thomas Clayson
the new posted code is not an exact match of your code its where I'm at right know in trying new things.
help
of course this isn't going to work.. he's using mysqli and you've posted an example with the mysql lib!
Kieran Allen
oh, well wasn't made clear when I posted. surely there is a similar/same function in mysqli?
Thomas Clayson
+1  A: 

As you're using mysqli you should probabaly use prepared statements (from both a security and performance standpoint)

$stmt   =   $mysqli->prepare("INSERT IGNORE INTO posts_categories (category_id, post_id, date_created) VALUES (?, ?, ?)");

// Should use a prepared statement here.
foreach ($categories as $key => $cat) {

    // Bind params
    $stmt->bind_param('iis', $cat, $post_id, 'NOW()');

    // Exectute the query
    $stmt->execute();
}

// Close the connection!
$mysqli->close();

NOTE: I also used INSERT IGNORE, so the insert will silently fail if the key exists.

Kieran Allen