0

screenshot: https://i.sstatic.net/mX29j.png

im currently making an invoice receipt. the numbers you see on the image, 4600 and 33, are the retrieved values from the checklist from the first page. As you can see, it only lists ONE charge where as it should be two.

this is the part of the code:

$procedures = $_SESSION['receipt'];

        foreach($procedures as $key=>$procedureno)
            {
            $query = "select * from `charges` where `procedure_no` = $procedureno";
            echo $procedureno;
            $result2=mysql_query($query,$dbh1);
            if(mysql_num_rows($result2) > 0 ){
            while($row = mysql_fetch_array($result2)){
            echo '
            <tr>
                <td>'.$row[1].'</td>
                <td>'.$row[2].'</td>';

            echo '</tr>';
                }
    }

            }

I want this query $query = "select * from charges where procedure_no = $procedureno"; to be executed several times depending on how many the $procedureno is.

1
  • 1
    You are about to get a lecture on mysql injection... Commented Jan 10, 2013 at 17:34

2 Answers 2

2

MySQL understands lists. Try this:

SELECT * FROM charges WHERE procedure_no IN (1, 5, 33, 78)
Sign up to request clarification or add additional context in comments.

6 Comments

$procedureno=4600 33. How can I replace the space with comma?
Do you even know what you are doing? Try str_replace($procedureno, ' ', ','). Definitely not the best way, but here's not the place to teach you PHP basics...
It should be str_replace(" " , ", ", "$procedureno"); but either way it doesnt work
Just take my query and send it to the database manually. You will see that it actually does work. It's the most straightforward solution to your problem.
Yeah it works in sql. But my problem is doing it on a php code since there is no comma in the variable.
|
0

Create a method and pass the condition as a parameter. Inside that method put your FOR cycle.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.