triple data

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
michaelk46
Forum Commoner
Posts: 67
Joined: Mon Oct 12, 2009 9:50 pm

triple data

Post 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????
User avatar
manohoo
Forum Contributor
Posts: 201
Joined: Wed Dec 23, 2009 12:28 pm

Re: triple data

Post 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?
michaelk46
Forum Commoner
Posts: 67
Joined: Mon Oct 12, 2009 9:50 pm

Re: triple data

Post 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...
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Re: triple data

Post 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
User avatar
manohoo
Forum Contributor
Posts: 201
Joined: Wed Dec 23, 2009 12:28 pm

Re: triple data

Post by manohoo »

Good, can you also post the form code?
michaelk46
Forum Commoner
Posts: 67
Joined: Mon Oct 12, 2009 9:50 pm

Re: triple data

Post 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>
michaelk46
Forum Commoner
Posts: 67
Joined: Mon Oct 12, 2009 9:50 pm

Re: triple data

Post 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
Post Reply