php search and display

I’ve recently started using php, what I am trying to do is I have 4 categories and CategoryId as 1,2,3,4 save id category table, I have another table expense in which data is saved against these id, I wrote a code but problem is it only displays one row/record while I need to display all the records saved in expense table against categoryId 1 or 2 as entered by user.
please help me with this as soon as possible. I am attaching my code here. Thank you


search.txt (2.2 KB)

In case the OP revisits this thread.

You need to provide the user with an input method that doesn’t require them to remember what each value means. You would either use a select/option menu, with the multiple attribute or check-boxes, so that they would simply select or check the choices they want. You would display the actual category names, but the category id would be he value(s) that the form submits. For either a select/option menu or check-boxes, you would use an array for the form field name. This will cause an array of the selected category ids to be submitted.

In the sql query statement, the WHERE clause would need to use an IN() comparison, which is equivalent to having multiple OR’ed terms - WHERE CategoryId IN() The part of the syntax would need to be dynamically produced to match the number of category choices that were picked and submitted. the IN() comparison works with one or more more values, so it can be used even if a single value is submitted.

You also need to use a prepared query and if possible switch to use the much simpler php PDO extension, instead of the php mysqli extension. For a prepared query, the part of the syntax would be a comma separated list of ? place-holder(s), one for each submitted value. You would then bind and/or supply the actual values when you execute the query. You would need to research prepared queries on the web or in the php.net documentation.

Your code is also mixing mysqli query php statements and mysql fetch php statements, so it is unlikely that the posted code displays any value at all. Just learn the php PDO extension and use it.

Sponsor our Newsletter | Privacy Policy | Terms of Service