Data base which save record and display in a table on the same page

Codes here !

Moderators: egami, macek, gesf

Post Reply
Mang
New php-forum User
New php-forum User
Posts: 1
Joined: Mon Nov 05, 2018 3:12 am

Mon Nov 05, 2018 3:31 am

Scope :
I have main three buttons:
1. Add Records - this button entered new records to the database.
2. Show - this button shows the entered data below the entry form on the same page.
3. Delete - each table row ends with a Delete button which can delete data already entered into the table.


Now I want that whenever i press Delete button, updated list of the data in the table after deleting the clicked rows has to show up automatically without the the need to clicked [Show] button again and again.


How do i do this.

my code is: I used the [insert_php ] plug in for word press.

Code: Select all

 [insert_php]

//including the database connection file
$cser=mysqli_connect(""xxxx","xxxx","xxxxx","sometable"") or die("connection failed:".mysqli_error());
if(isset($_POST['Submit'])) {    
    $pname = $_POST['pname'];
    $pcode = $_POST['paddress'];
    $pprice = $_POST['pemail'];
        
    // checking empty fields
    if(empty($pname) || empty($pcode) || empty($pprice)) {                
        if(empty($pname)) {
            echo "<font color='red'>Name field is empty.</font><br/>";
        }
        
        if(empty($pcode)) {
            echo "<font color='red'>Address field is empty.</font><br/>";
        }
        
        if(empty($pprice)) {
            echo "<font color='red'>Email  field is empty.</font><br/>";
        }
        
        //link to the previous page
        echo "<br/><a href='javascript:self.history.back();'>Go Back</a>";
    } else { 
        // if all the fields are filled (not empty)             
        //insert data to database
        $result = mysqli_query($cser, "INSERT INTO crud2 (pname,pcode,pprice) VALUES('$pname','$pcode','$pprice')");

        
        //display success message
        echo "<font color='green'>Data added successfully.\nTo see the values click\n<strong>SHOW</strong> button";
        
    }
}
?>
<fieldset class="block"><legend class="legend_">Address Book</legend>
    <form action="" method="post" name="form1">
        <table width="25%" border="0">
            <tr> 
                <td>Name</td>
                <td><input type="text" name="pname"></td>
            </tr>
            <tr> 
                <td>Address</td>
                <td><input type="text" name="paddress"></td>
            </tr>
            <tr> 
                <td>Email </td>
                <td><input type="text" name="pemail"></td>
            </tr>
            <tr> 
                <td class="legend_"><input type="submit" name="Submit" value="Add Record" ></td>
                <td class="legend_"><input type="submit" name="Show" value="Show"  ></td>
            </tr>
        </table>
    </form>
</fieldset>
[/insert_php]
<!-- End of First Part-->


            
        

        [insert_php]


     if(isset($_POST['Show'])){
echo '<table width="30%" border=0 >
        <tr bgcolor="grey" class="yes">
            <td>Name</td>
            <td>Address </td>
            <td>E-mail </td>
</tr>';           


//including the database connection file
$cser=mysqli_connect(""xxxx","xxxx","xxxxx","sometable"") or die("connection failed:".mysqli_error());
//fetching data in descending order (lastest entry first)

$result = mysqli_query($cser, "SELECT * FROM crud2 ORDER BY id DESC LIMIT 0,30 "); // using mysqli_query instead

        while($res = mysqli_fetch_array($result)) {  

            echo "<tr  class='yes' >";
            echo "<td>".$res['pname']."</td>";
            echo "<td>".$res['pcode']."</td>";
            echo "<td>".$res['pprice']."</td>"; 
   echo '<td>
                 <form  method="get">
                      <input type="hidden" value="'.$res['id'].'" name="id" >
                      <input type="submit" value="Delete">
                 </form>
            </td>'; 
        
//delete code  begins.
if (isset($_GET['id']) && is_numeric($_GET['id']))  
{
// get the 'id' variable from the button click.
$id = $_GET['id'];//(int)
echo $id."th Row -";
// delete record from database.
$conn=mysqli_connect("xxxx","xxxx","xxxxx","sometable") or die("connection failed:".mysqli_error());
//$delete = mysql_query( "DELETE FROM crud2 WHERE id=$id")or die(mysql_error()); //
$sqldelete = "DELETE FROM crud2 WHERE id=$id";
if (mysqli_query($conn, $sqldelete)) {
    echo "Record deleted successfully";
} else {
    echo "Error deleting record: " . mysqli_error($conn);
}
mysqli_close($conn);
}    // delete code ends 
             
        }
echo '</tr>';
echo '</table>';
mysqli_close($cser);


}
    ?>

[/insert_php]
 <!-- Styling Code -->
<style>
.round{
border-radius:5px 5px 5px 5px;
text-align:center;
}
</style>
<style>
.block{
display:inline-block;
border:1.5px solid grey;
padding:3%;
border-radius:2.5%;
background-color:;
}
.legend_ {
color:grey;
font-size:1.5em;
text-align:center;
vertical-align: middle;
box-shadow:0 0 0 2px #ddd!important;
border-radius:2.5%;
/*margin-left:20px!important;*/
}
</style>

Demo of the CODE can be watch here
http://leitung.epizy.com/wordpress1/201 ... te-button/

can any body help???

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 747
Joined: Mon Feb 22, 2016 5:52 pm

Mon Nov 05, 2018 8:19 am

You'll need to look into XMLHttpRequests to do this.

First though: (and take this the way it is meant)

I would encourage you to put your current finished page through a HTML checker first.

Look into current practices with form processing: this involves preparing SQL statements, filtering and or validating user input as a minimum.

Use external JavaScript files.

Use HTML correctly to provide a layout which is accessible and CSS to lay it out on devices.

Before starting on anything else. Get these basics right first and then build on them rather than constantly having to re-write and de-bug code.

Post Reply