Jump to content

[SOLVED] After executing a command, table disappears


Strikers13

Recommended Posts

I'm trying to setup an admin page for a website. So far, I have it so you select what you want to view/edit and then you can search for the name. Then it brings up the requested information. The problem I'm having is that every time you update or delete something in the table that pops up after you make a search, it disappears. I would like to make it so when you update or delete something, you still see the table with the requested information without having to re-type it in. There's multiple sections to this code, but this is only one section.

 

  <?php
    mysql_connect(localhost, "---", "---");
    @mysql_select_db(admin) or die("Unable to connect to database.");
    
    
    //Update Section
if (isset($_POST['update']))
    {
        if(!empty($_POST['quanity']))
        {    
    foreach($_POST['quanity'] as $id=>$quanity)
    {
        $update = "UPDATE `test` SET `quanity` = '$quanity' WHERE `id`='$id' ";
        @mysql_query($update);
    }
        }
    }
    //Delete Section
        if (isset($_POST['delete']))
    {
        if (isset($_POST['del']))
        {
            $dele = $_POST['del'];
            foreach ($dele as $value )
            {
                $del = "DELETE FROM test WHERE id = $value";
                $de = mysql_query($del);
            }
            echo "Entry Deleted";
        }else
        {
            echo "No data selected.";
        }
    }
?>
<html>
<head>
</head>
<body>
<form method='POST'>
<div style="float:left; border:1px; border-style:solid; width:250px">
<b>Name Search</b><br/><input type="text" name="name_search" />
<br/>
<br/>
<b> Select what you want to edit </b>
<select name="edit_table">  
<option value="test"> test  
</select>
<input type ="submit" name="search" value ="Search" />
</div>
</form>

<div style ="float: right;" > <!--RIGHT SIDE-->
<?php
if (isset($_POST["search"]))
{
      mysql_connect(localhost, "---", "---");
      @mysql_select_db(admin) or die("Unable to connect to database.");
      
    $name_search = $_POST["name_search"];
    $edit_table = $_POST["edit_table"];
    
    if($edit_table == "test")
    {
    if(!$name_search)
        {
        echo "Please type in a name to search for" ;
        }    
        else{
        $query = "select * from test where uname = '$name_search'";
        $result=mysql_query($query);    
        ?>
        <form method="post">
        <table border=1px>
        <?php
        while(list($uname, $id, $name, $description, $price, $quanity)=mysql_fetch_array($result))
        {
        ?>    
        <th>Name</th>
        <th>Description</th>
        <th>Price</th>
        <th>Quanity</th>
        <th>Delete</th>
        <tr>
        <td> <?php echo $name?> </td>
        <td> <?php echo $description?> </td>
        <td> $<?php echo $price?> </td>
        <td><input type ="text" name="quanity[<?php echo $id ?>]" value="<? echo $quanity?>" /> </td>
        <td> <input type="checkbox" name="del[]" value="<?php echo $id ?>"> </td>
        </tr>
        <?php } ?>
        </table>
        <input type ="submit" name="update" value ="Update" />
         <input type="submit" value="Delete Checked" name="delete" />
         <br/><br/><br/>        
        </form>
        <?php
    }
}    
        ?>
</div>
</body>
</html>
<?php } ?> 

Link to comment
Share on other sites

the basic page is just a search box, a select list, and a submit button.

once the user preforms a search, $_POST['search'] (the value of the search button) isset() as it were. as a result, the search results + (1 text field, 1 checkbox per row) + update/delete buttons appear in a table below the search area.

 

your problem is that once the update/delete buttons are pressed, the page reloads without the search results. this happens because once you submit from the results form, $_POST['search'] is not re-created, and so is not isset(), and so the search results do not appear. what you need to do is add:

<input type="hidden" name="search" value="<?php $_POST['search']; ?>" />

to your search-result form (before the update and delete buttons).

 

this will cause $_POST['search'] to be re-set to the same value each time you update/delete, resulting in the search results re-appearing.

 

hope that was comprehensive.

 

test what i suggested, and let me know what happens.

Link to comment
Share on other sites

Adding that line didn't do anything.

  <?php
    mysql_connect(localhost, "---", "---");
    @mysql_select_db(admin) or die("Unable to connect to database.");
    
    
    //Update Section
if (isset($_POST['update']))
    {
        if(!empty($_POST['quanity']))
        {    
    foreach($_POST['quanity'] as $id=>$quanity)
    {
        $update = "UPDATE `test` SET `quanity` = '$quanity' WHERE `id`='$id' ";
        @mysql_query($update);
    }
        }
    }
    //Delete Section
        if (isset($_POST['delete']))
    {
        if (isset($_POST['del']))
        {
            $dele = $_POST['del'];
            foreach ($dele as $value )
            {
                $del = "DELETE FROM test WHERE id = $value";
                $de = mysql_query($del);
            }
            echo "Entry Deleted";
        }else
        {
            echo "No data selected.";
        }
    }
?>
<html>
<head>
</head>
<body>
<form method='POST'>
<div style="float:left; border:1px; border-style:solid; width:250px">
<b>Name Search</b><br/><input type="text" name="name_search" />
<br/>
<br/>
<b> Select what you want to edit </b>
<select name="edit_table"> 
<option value="test"> test 
</select>
<input type ="submit" name="search" value ="Search" />
</div>
</form>

<div style ="float: right;" > <!--RIGHT SIDE-->
<?php
if (isset($_POST["search"]))
{
      mysql_connect(localhost, "---", "---");
      @mysql_select_db(admin) or die("Unable to connect to database.");
      
    $name_search = $_POST["name_search"];
    $edit_table = $_POST["edit_table"];
    
    if($edit_table == "test")
    {
    if(!$name_search)
        {
        echo "Please type in a name to search for" ;
        }    
        else{
        $query = "select * from test where uname = '$name_search'";
        $result=mysql_query($query);    
        ?>
        <form method="post">
        <table border=1px>
        <?php
        while(list($uname, $id, $name, $description, $price, $quanity)=mysql_fetch_array($result))
        {
        ?>   
        <th>Name</th>
        <th>Description</th>
        <th>Price</th>
        <th>Quanity</th>
        <th>Delete</th>
        <tr>
        <td> <?php echo $name?> </td>
        <td> <?php echo $description?> </td>
        <td> $<?php echo $price?> </td>
        <td><input type ="text" name="quanity[<?php echo $id ?>]" value="<? echo $quanity?>" /> </td>
        <td> <input type="checkbox" name="del[]" value="<?php echo $id ?>"> </td>
        </tr>
        <?php } ?>
        </table>
<input type="hidden" name="search" value="<?php $_POST['search']; ?>" />
        <input type ="submit" name="update" value ="Update" />
         <input type="submit" value="Delete Checked" name="delete" />
         <br/><br/><br/>       
        </form>
        <?php
    }
}    
        ?>
</div>
</body>
</html>
<?php } ?>  

Link to comment
Share on other sites

oh, ya. sorry striker. i missed some stuff (it was late  :P):

 

change:

if (isset($_POST["search"])) //was the button sent

to:

if (isset($_POST["name_search"])) //was the text field sent - won't break you're code, unless some1 sends an empty search maybe

 

then change:

<input type="hidden" name="search" value="<?php $_POST['search']; ?>" />

to:

<input type="hidden" name="name_search" value="<?php $_POST['name_search']; ?>" />

 

and add another hidden field:

<input type="hidden" name="edit_table" value="<?php $_POST['edit_table']; ?>" />

 

that should do it i think.

good luck.

Link to comment
Share on other sites

It still didn't work.

 

   <?php
    mysql_connect(localhost, "---", "---");
    @mysql_select_db(admin) or die("Unable to connect to database.");
    
    
    //Update Section
if (isset($_POST['update']))
    {
        if(!empty($_POST['quanity']))
        {    
    foreach($_POST['quanity'] as $id=>$quanity)
    {
        $update = "UPDATE `test` SET `quanity` = '$quanity' WHERE `id`='$id' ";
        @mysql_query($update);
    }
        }
    }
    //Delete Section
        if (isset($_POST['delete']))
    {
        if (isset($_POST['del']))
        {
            $dele = $_POST['del'];
            foreach ($dele as $value )
            {
                $del = "DELETE FROM test WHERE id = $value";
                $de = mysql_query($del);
            }
            echo "Entry Deleted";
        }else
        {
            echo "No data selected.";
        }
    }
?>
<html>
<head>
</head>
<body>
<form method='POST'>
<div style="float:left; border:1px; border-style:solid; width:250px">
<b>Name Search</b><br/><input type="text" name="name_search" />
<br/>
<br/>
<b> Select what you want to edit </b>
<select name="edit_table"> 
<option value="test"> test 
</select>
<input type ="submit" name="search" value ="Search" />
</div>
</form>

<div style ="float: right;" > <!--RIGHT SIDE-->
<?php
if (isset($_POST["name_search"]))
{
      mysql_connect(localhost, "---", "---");
      @mysql_select_db(admin) or die("Unable to connect to database.");
      
    $name_search = $_POST["name_search"];
    $edit_table = $_POST["edit_table"];
    
    if($edit_table == "test")
    {
    if(!$name_search)
        {
        echo "Please type in a name to search for" ;
        }    
        else{
        $query = "select * from test where uname = '$name_search'";
        $result=mysql_query($query);    
        ?>
        <form method="post">
        <table border=1px>
        <?php
        while(list($uname, $id, $name, $description, $price, $quanity)=mysql_fetch_array($result))
        {
        ?>   
        <th>Name</th>
        <th>Description</th>
        <th>Price</th>
        <th>Quanity</th>
        <th>Delete</th>
        <tr>
        <td> <?php echo $name?> </td>
        <td> <?php echo $description?> </td>
        <td> $<?php echo $price?> </td>
        <td><input type ="text" name="quanity[<?php echo $id ?>]" value="<? echo $quanity?>" /> </td>
        <td> <input type="checkbox" name="del[]" value="<?php echo $id ?>"> </td>
        </tr>
        <?php } ?>
        </table>
	<input type="hidden" name="name_search" value="<?php $_POST['name_search']; ?>" />
	<input type="hidden" name="edit_table" value="<?php $_POST['edit_table']; ?>" />
        <input type ="submit" name="update" value ="Update" />
         <input type="submit" value="Delete Checked" name="delete" />
         <br/><br/><br/>       
        </form>
        <?php
    }
}    
        ?>
</div>
</body>
</html>
<?php } ?> 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.