Check if mysql row exisits within foreach loop of php array -
Check if mysql row exisits within foreach loop of php array -
i have array of items looping on , checking if value exists in database, if update it, if not create new entry. first item in array seen , returns true, after first 1 returns 0 if know fact in database right item_name , ticket_id.
i have tried researching before asking, stumped @ point. help in advance.
if($func == 'edit') { foreach ($additem $key => $value) { if (empty($key) || $key=='amount_paid') { continue; } $ticket_items = mysql_query("select * ticket_items ticket_id = '$ticket_id' , item_name = '$key'"); if (mysql_num_rows($ticket_items)) { print 'updated '. $key ."\n"; mysql_query("update ticket_items set item_name='$key', item_price='$value' ticket_id='$ticket_id'"); } else { print 'created '. $key ."\n"; mysql_query("insert ticket_items (ticket_id, item_name, item_price) values ('$ticket_id', '$key', '$value')"); } } }
notice that, in update statement, updating every row has same $ticket_id, regardless of item_name. should be:
update ticket_items set item_name='$key', item_price='$value' ticket_id='$ticket_id' , item_name='$key';
php mysql foreach
Comments
Post a Comment