Page 1 of 1

triple data

Posted: Wed Dec 30, 2009 12:45 pm
by michaelk46
I am running into a weird issue...

Everything works the way it should except that the data is being displayed three times on the screen... See below
Image
Here is the PHP code that calls it...

Code: Select all

<?php
if ($_SERVER['REQUEST_METHOD'] != "POST") //looks to see if a button wasn't pressed 
    {                                                                  
        include "search.html.php";
    }
else //looking to see if the edit button has been hit from edit.html.php
    {
        foreach($_POST as $name => $_POST['name']) 
        if('Edit' === substr($name, 0, 4)) //looks to see if the word 'Edit' is in the name field of the button
            {
                $name = $_POST['name'];
                $trimmed = trim($name, 'Edit #');
                $sql = 'SELECT pages.id, category.cat, manufacturer.manu, pages.pagename, pages.retailprice, 
                        pages.salesprice, pages.upc, pages.sku, pages.caption, pages.pagename, image.name FROM pages 
                        INNER JOIN category ON pages.categoryid = category.id 
                        INNER JOIN image ON pages.imageid = image.id
                        INNER JOIN manufacturer ON pages.manufactureid = manufacturer.id WHERE pages.id = "'. $trimmed .'"';    
                $result = mysqli_query($link, $sql);
                    if (!$result)
                        {
                            $error = 'No Pages Found.';
                            include 'error.html.php';
                            exit();
                        }
                $row = mysqli_fetch_array($result);
                include 'edit.html.php';
            }
        else
            {
                $pagetable = NULL; 
                $pageinfo = array();
                if(!empty($_POST['searchid']) || (!empty($_POST['pagename'])))  //looks to see if either search field is populated
                    {
                        //begins to generate search string 
                        $sql = "SELECT pages.id, category.cat, manufacturer.manu, pages.pagename FROM pages 
                                INNER JOIN category ON pages.categoryid = category.id 
                                INNER JOIN manufacturer ON pages.manufactureid = manufacturer.id WHERE ";
                    
                        if(!empty($_POST['searchid']))
                            {
                                $searchid = ($_POST['searchid']);
                                $sql .= "pages.id = ".$_POST['searchid']."";
                            }
                
                        if(!empty($_POST['searchid']) && (!empty($_POST['pagename'])))
                            {
                                $sql .= " and ";
                            }
        
                        if(!empty($_POST['pagename']))
                            {
                                $sql .= "pages.pagename = ".$_POST['pagename']." ";
                                $pagename = ($_POST['pagename']);
                            }
                
                                                
                        $result = mysqli_query($link, $sql);
                        if (!$result)
                            {
                                include 'searchres.html.php';
                                $error = 'No Pages Found.';
                                include 'error.html.php';
                                exit();
                            }
                                                            
                        $pagetable = "<table border='1'><TR><TD WIDTH=150 BGColor='#00FF00'>Page ID</TD><TD WIDTH=150 BGColor='#00FF00'>Pagename</TD><TD WIDTH=150 BGColor='#00FF00'>Category</TD><TD WIDTH=150                         
                                    BGColor='#00FF00'>Manufacturer</TD></TR>";
                          
                        while ($row = mysqli_fetch_array($result)) 
                            {
                                $pagetable .= ('<TR>' . '<TD>' . ($row['id']) . '</TD>' . '<TD>' . ($row['pagename']) . '</TD>' . '<TD>' . ($row['cat']) . '</TD>' . '<TD>' . 
                                ($row['manu']) . '</TD>' .  '<TD>' . '<form action="?" method="POST">' . '<input type="submit" name="Edit" value="Edit #' . ($row['id']) . '"/>' . '</form>' . '</TD>' . '</TR>');
                            }   
                        $pagetable .= '</table>';                           
                    }
                else $error = "You must enter search terms"; 
                include "searchres.html.php"; 
            }
    }
?>     
The problem started when I added line 8... I know it's a foreach loop issue, but i cannot see what is causing it to loop three times... Can anyone explain why????

Re: triple data

Posted: Wed Dec 30, 2009 1:03 pm
by manohoo
What's the content of $_POST?

before line 8 do this:

Code: Select all

 
echo "<pre>";
var_dump($_POST);
 
what's the output?
... and what's your intent for each one of the $_POST entries?

Re: triple data

Posted: Wed Dec 30, 2009 1:20 pm
by michaelk46
Here is what happened

array(3) {
["searchid"]=>
string(1) "1"
["pagename"]=>
string(0) ""
["search"]=>
string(6) "Search"
}


As far as my intent... I am trying to see if the button name contains the string 'Edit' and then run code specific to that button...

Re: triple data

Posted: Wed Dec 30, 2009 3:30 pm
by daedalus__
why dont you use check boxes instead of submit buttons?

also take that loop out i dont even know why that is there.

the value of the checkk box is the id of the record and the name of the submit button is the action so use a switch statement or something simple

Re: triple data

Posted: Wed Dec 30, 2009 3:33 pm
by manohoo
Good, can you also post the form code?

Re: triple data

Posted: Wed Dec 30, 2009 4:17 pm
by michaelk46
Here it is...

Code: Select all

<form action="?" method="POST">
    <div> 
        <label for="pagename">Record ID</label> 
        <input type="text" name="id" id="id" size="10" value="<?php echo htmlspecialchars ($row['id'], ENT_QUOTES, 'utf-8');?>"/>
    </div> 
    <p></p>
    <div> 
        <label for="pagename">Page Name</label> 
        <input type="text" name="pagename" id="pagename" size="45" value="<?php echo htmlspecialchars ($row['pagename'], ENT_QUOTES, 'utf-8');?>"/>
    </div> 
    <p></p>
        <div> 
        <label for="sku">SKU</label> 
        <input type="text" name="sku" id="sku" size="15" value="<?php echo htmlspecialchars ($row['sku'], ENT_QUOTES, 'utf-8');?>"/>
    </div> 
    <p></p>
    <div> 
        <label for="upc">UPC</label> 
        <input type="text" name="upc" id="upc" size="15" value="<?php echo htmlspecialchars ($row['upc'], ENT_QUOTES, 'utf-8');?>"/>
    </div> 
    <p></p>
    <div> 
        <label for="category">Category</label> 
        <input type="text" name="category" id="category" size="45" value="<?php echo htmlspecialchars ($row['cat'], ENT_QUOTES, 'utf-8');?>"/>
    </div> 
    <p></p>
    <div> 
        <label for="manufacturer">Manufacturer</label> 
        <input type="text" name="manufacturer" id="manufacturer" size="15" value="<?php echo htmlspecialchars ($row['manu'], ENT_QUOTES, 'utf-8');?>"/>
    </div> 
    <p></p>
    <div> 
        <label for="retailprice">Retail Price</label> 
        <input type="text" name="retailprice" id="retailprice" size="15" value="<?php  echo htmlspecialchars ($row['retailprice'], ENT_QUOTES, 'utf-8');?>"/>
    </div> 
    <p></p>
    <div> 
        <label for="salesprice">Sale Price</label> 
        <input type="text" name="salesprice" id="salesprice" size="15" value="<?php echo htmlspecialchars ($row['salesprice'], ENT_QUOTES, 'utf-8');?>"/>
    </div> 
    <p></p>
    <div> 
        <label for="caption">Caption:</label>
        <div></div>
        <textarea id="caption" name="caption" rows="7"><?php echo htmlspecialchars ($row['caption'], ENT_QUOTES, 'utf-8');?></textarea>
    </div> 
    <p></p>
     <div>
        <input type="submit" name="save" value="Save Changes"/>  <input type="submit" name="return" value="Return to Search"/>
    </div>
    
</form>

Re: triple data

Posted: Wed Dec 30, 2009 7:49 pm
by michaelk46
I finally found the answer...


I had to change

Code: Select all

foreach($_POST as $name => $_POST['name'])
        if('Edit' === substr($name, 0, 4)) //looks to see if the word 'Edit' is in the name field of the button
            {
                $name = $_POST['name']; 
 

to this

Code: Select all

if(isset($_POST['Edit'])) 
    {
       $id = substr($_POST['Edit'], 0, 4);
     }
Thank You all for your help and suggestions