how to check id is existed before insert to database

Tuesday, November 3, 2015

I have 2 related table



one is claim_item_category



|---------------------|------------------|
| claim_item_id | claim_item_name |
|---------------------|------------------|
| 1 | Transport |
|---------------------|------------------|
| 2 | Medical |
|---------------------|------------------|


second is claim_list



|---------------------|------------------|------------------|
| claim_list_id | claim_item_id | Amount |
|---------------------|------------------|------------------|
| 1 | 1 | 20.50 |
|---------------------|------------------|------------------|


User have a selector box to select Claim Item



<select name="claim_item_id" class="form-control" required="required" aria-required="true">
<option value="" selected="SELECTED">...</option>
<option value="1">Transport</option>
<option value="2">Medical</option>
</select>


IF they inspect the code change option value to 3 which is not existed in claim_item_category table and it will also save in to database and then output will be blank.



Its possible check the claim_item_category - claim_item_id is already existed then only save?



My insert sql query :



sprintf("INSERT INTO `claim_list`
(`claim_item_id`,`Amount`)
VALUES
('%d','%s');"
,$this->claim_category_id,$this->claim_amount);

0 comments:

Post a Comment