Solved

error in while loop

Posted on 2014-04-02
3
666 Views
Last Modified: 2014-04-02
I have a mySQL table called filterSetup which has 16 records. in the body of the page I loop through the recordset to dynamically create a form.  That works fine.  I see 16 rows. Each row has two text fields: operator1, value1 for row 1, operator2, value2 for row 2, etc.  Again, this part seems to be fine.

On submit is where I am having trouble looping through the recordset, and perhaps I am even going about this whole thing wrong.  But I have added some "alerts" and see that it gets through the first time but doesn't move to the next record.

I know that I should be using mysqli instead of mysql, and that will be my next task when the site is up and running.

<?php
    require_once('config.php');
    if (!empty($_POST["applyFilter"]) )
    {
        $sql = "Delete from reportfilters";
        if (!$res = mysql_query($sql)) die( mysql_error() );  

        $sql = "SELECT * FROM filterSetup order by filterID" ;
        $res = mysql_query($sql);
        if (!$res) die( mysql_error() );
        while ($row = mysql_fetch_assoc($res))
        {
            echo "</br>top of while</br>";
            $filterID =$row["filterID"];
            $operator = $_POST["operator".$filterID];
            $value = '';
            if (!empty($_POST["value".$row["filterID"]]) ) {            
                $value = mysql_real_escape_string($_POST["value".$filterID]);
            } 
echo "id:".$filterID." operator:".$operator." value:".$value;
            //save the filterSetup
echo "</br>starting update";    
                $sql = "Update filterSetup set myOperator='".$operator."', filterValue='".$value."'".
                            " where filterID=".$filterID;
                if (!$res = mysql_query($sql)) die( mysql_error() ); 
echo "</br>update ok";
            //save the filterLine
                if ($value !='' ) {
                    if ($operator =='contains') {
                        $strFilter=" like '%". $value . "%'";
                    } else {
                        $strFilter=$operator . "'" . $value . "'";
                    }
                    $strFilter= $row["filterField"] . $strFilter;
                    $sql = "Insert into reportfilters (filterLine) values('".mysql_real_escape_string($strFilter)."')";
echo "</br>running: ".$sql;
                    if (!$res = mysql_query($sql)) die( mysql_error() ); 
echo "</br>insert completed";
                }
echo "</br>moving to next";
        }
        //header("Location: gridDisplay_new.php?viewid=1" );
        //exit;                               

    }
?>
<form action="filterForm.php" id="filterForm" name="filterForm" method="POST">
<?php 

    $sql2 = "SELECT * FROM filterSetup order by filterID" ;
    $res2 = mysql_query($sql2);
    if (!$res2) die( mysql_error() );
?>
    <table>
        <?php
            while ($row2 = mysql_fetch_assoc($res2))
            {
        ?>
                <tr>
                    <td><?php echo $row2["filterField"]; ?></td>
                    <td>
                        <select name="operator<?php echo $row2["filterID"]; ?>">
                            <option value = "contains" <?php if ($row2["myOperator"]=="contains") echo " selected" ?>>contains</option>
                            <option value = "=" <?php if ($row2["myOperator"]=="=") echo " selected" ?>>=</option>
                            <option value = "!=" <?php if ($row2["myOperator"]=="!=") echo " selected" ?>>!=</option>
                            <option value = "<" <?php if ($row2["myOperator"]=="<") echo " selected" ?>><</option>
                            <option value = ">" <?php if ($row2["myOperator"]==">") echo " selected" ?>>></option>
                            <option value = "<=" <?php if ($row2["myOperator"]=="<=") echo " selected" ?>><=</option>
                            <option value = ">=" <?php if ($row2["myOperator"]==">=") echo " selected" ?>>=></option>
                        </select>
                    </td>
                    <td><input type="text" name="value<?php echo $row2['filterID']; ?>" value="<?php echo $row2["filterValue"] ?>" /></td>
                </tr>
        <?php
            }
        ?>
    </table>
    <input type="submit" value="Apply Changes" name="applyFilter" id="applyFilter" />
</form>
<button id="closeSlide">Close Filter Menu</button>

Open in new window


Here is the result when I submit:
top of while
id:1 operator:contains value:bronco
starting update
update ok
running: Insert into reportfilters (filterLine) values('Manufacturer_Dealer_Name like \'%bronco%\'')
insert completed
moving to next
Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in /home/uniqueda/public_html/xtime/filterForm.php on line 11
0
Comment
Question by:UniqueData
  • 2
3 Comments
 
LVL 34

Accepted Solution

by:
Dan Craciun earned 500 total points
ID: 39973254
You're reusing the $res variable, which inside the loop will point to a new resource.

On line 25 give another name to the resource. $res1 or whatever strikes you fancy.
The same on line 37.

If you don't, then after the first loop pass, $res will point to the result of an INSERT, and not to the result of the original SELECT.

HTH,
Dan
0
 
LVL 7

Author Comment

by:UniqueData
ID: 39973399
oh my..of course it was something like that I missed.

thanks for your time
0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39973403
Glad I could help!
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
google analytics in wordpress 4 34
writing a post to a file that is on same folder and same webserver 5 28
Dlookup MSACCESS 5 25
Mysql Left Join Case 10 49
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question