0

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);
2
Contributors
4
Replies
24
Views
2 Years
Discussion Span
Last Post by bolfescu
0

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

0

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

0

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.

0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.