hy i'm having problem with this mysql qry, i dont whant to insert in the db if its empty the field, ive tryied with is NULL, but its not working...

$qry=mysql_query("INSERT INTO trans_autista (autista, ditta)
SELECT * FROM (SELECT '$trans_autista', '$cat') AS tmp 
WHERE NOT EXISTS (
    SELECT autista FROM trans_autista WHERE autista = '$trans_autista' and ditta ='$cat'
) LIMIT 1", $con);

Recommended Answers

All 4 Replies

Hi,

If you are referring to the value of the variables then check it before executing the query:

if( ! is_null($trans_autista) && ! is_null($cat))
{
    # execute query
}

If instead you are referring to the table rows, then create a unique index of autista and ditta columns. And then use INSERT IGNORE ...

hmm..can't use the unique index, and is_null its not working.....its still insert empty values..

Then try empty():

if( ! empty($trans_autista) && ! empty($cat))
{
    # execute query
}

This will match null, false and empty strings: http://php.net/empty

But be careful: empty() will match also 0, so if you have a category 0 the condition will fail.

By the way, the insert query does not support the WHERE statements, you can do it by inserting the subquery in the first SELECT:

INSERT INTO trans_autista (autista, ditta) SELECT '$trans_autista', '$cat' FROM dual WHERE NOT EXISTS (select autista FROM trans_autista WHERE autista = '$trans_autista' AND ditta = '$cat') limit 1;

And it should work.

Thank you, it worked with the condition, thank you very much

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.