Display in forms of data inserted into the database

I created this table, which partially takes data from the user and stores it in the database.

I am trying to make the data placed by the user in tags will be displayed. Basically, the input form becomes like an I / O source. Any help on how to do this?

switch ($selected){
    case 'University':
        $stmt = $conn->prepare("SELECT employees.afnumber,employees.name,employees.actualpost,university.brevet FROM employees,university WHERE employees.status='Employed' AND employees.afnumber=university.afnumber ORDER BY employees.afnumber DESC LIMIT :start,:end");
        $stmt->bindParam(':start', $pages->limit_start, PDO::PARAM_INT);
        $stmt->bindParam(':end', $pages->limit_end, PDO::PARAM_INT);
        $stmt->execute();
        $result = $stmt->fetchAll();
        $selectedtable  = "<form method='post' action=''>\n";
        $selectedtable .= "<table class='sortable'>\n<tr><th>Description</th><th>A</th><th>B</th><th>C</th><th>D</th></tr>\n";
        foreach($result as $row) {
            $selectedtable .= "<tr><th>Brevet</th><td><input type='text' name='Brevet1' style=' padding: 10px; margin-top:1px; border: solid 2px #c9c9c9; width:50px; height:2px;'></td><td>".$row[0]."</td><td>".$row[2]."</td><td>".$row[3]."</td></tr>
                       <tr><th>Baccalaureat/BT</th><td><input type='text' name='Baccalaureatbt' style=' padding: 10px; font-size:16px; margin-top:1px; border: solid 2px #c9c9c9; width:50px; height:2px;'></td><td>".$row[1]."</td><td>$row[2]</td><td>$row[3]</td></tr>
                       <tr><th>License/TS</th><td><input type='text' name='Licensets' style=' padding: 10px; margin-top:1px; border: solid 2px #c9c9c9; width:50px; height:2px;'></td><td>".$row[1]."</td><td>".$row[2]."</td><td>$row[3]</td></tr>
                       <tr><th>M1</th><td><input type='text' name='M1' style=' padding: 10px; margin-top:1px; border: solid 2px #c9c9c9; width:50px; height:2px;'></td><td>".$row[1]."</td><td>".$row[2]."</td><td>".$row[3]."</td></tr>
                       <tr><th>Master Degree</th><td><input type='text' name='Mastersdegree' style=' padding: 10px; margin-top:1px; border: solid 2px #c9c9c9; width:50px; height:2px;'></td><td>".$row[1]."</td><td>".$row[2]."</td><td>".$row[3]."</td></tr>
                       <tr><th>PHD</th><td><input type='text' name='Phd' style=' padding: 10px; margin-top:1px; border: solid 2px #c9c9c9; width:50px; height:2px;'></td><td>".$row[1]."</td><td>".$row[2]."</td><td>".$row[3]."</td></tr>";

    }
    $selectedtable .= "</table>\n"; 
    $selectedtable .= "<input type='submit' name='submit' value='Submit' style='width:80px; height:30px; text-align:center; padding:0px;'>\n";
    $selectedtable .= "</form>\n";

        if(isset($_POST['submit']))
    {   $brevet1 = $_POST['Brevet1'];
        $baccalaureatbt = $_POST['Baccalaureatbt'];
        $licensets = $_POST['Licensets'];
        $m1 = $_POST['M1'];
        $mastersdegree = $_POST['Mastersdegree'];
        $phd = $_POST['Phd'];


     $sql1="SELECT Brevet1,Baccalaureatbt,Licensets,M1,Mastersdegree,Phd FROM university";
if ($result=mysqli_query($con,$sql1))
  {

  $rowcount=mysqli_num_rows($result);
  }
    if($rowcount==0)
     {
 $sql="INSERT INTO university(Brevet1,Baccalaureatbt,Licensets,M1,Mastersdegree,Phd) VALUES('$brevet1','$baccalaureatbt','$licensets','$m1','$mastersdegree','$phd')";
 $result = mysql_query($sql);
     }
     else
     {
 $sql2 = "UPDATE university SET Brevet1 = '$brevet1' , Baccalaureatbt = '$baccalaureatbt', Licensets = '$licensets', M1 = '$m1', Mastersdegree = '$mastersdegree', Phd = '$phd'";
 $result2 = mysql_query($sql2);
     }

    }


    break;

      

+3


source to share


3 answers


You can set the value for each input like this:

<input type='text' name='Brevet1' value="<?php echo $_POST['brevet_1']; ?>">

With a triple condition: echo (!empty($_POST['brevet_1']) ? $_POST['brevet_1'] : '');



EDIT

Fetch data from database and set input value as above

+2


source


This is what I have always done in situations like this. Let's assume your page name mypage.php

. Now you open mypage.php with id

(for example mypage.php?id=1

and you can get id as $_GET['id']

). This id

will be the value of the auto_increment

table to be inserted / updated.

Now, on mypage.php

, write this code: -

    <form action="someEndFile.php" method="post">
        <?php echo $command; ?>
        <?php if($command == 1)
        {
$userData = fetch via $_GET['id'];
            ?>
            Your input Tags Here
            Submit Button Here
            <input type='text' name='somename' value='<?php echo $userdata->valueFromTheDatabase; ?>'>
            <?php
        }
        else
        {
            ?>
            <input type='text' name='somename'>
            <?php
        }
        ?>

    </form>

      

In the block, if(data_exists)

check if any record exists in the table with the id id

(which you got as $ _GET ['id']). If the entry exists, it will go inside the block if

, and command value

will be 1

, which means update

. If the id didn't exist, it would go to else

, and the value would be 2

what it means insert new

.



Now, in your form, echo $command

and put all the fields. Check the value here $command

and change the value accordingly inside the input tags.

Now in the file someEndFile.php

enter code like this: -

<?php
$command = isset($_POST['command']) ? $_POST['command'] : '';
switch($command)
{
    case 1:
    //update case
    write your update query here;
    break;
    case 2:
    //insert case
    write your insert query here;
    break;
}
?>

      

Now you can fulfill your required request. This is what I always do in situations like this.

+1


source


Here's an example where the input form data is displayed when the form is submitted. As a bonus, it also shows some basic PHP form validation:

w3schools Example of displaying a form record on the same page as in a post

highlight the variables for the values ​​entered on the page. You can put echo statements as needed in the tag formatting as you see fit.

Example: if your form contains:

Name: <input type="text" name="name">

      

Then display a tag containing "name" anywhere on the page, for example:

<?php
//check if name has changed:
if ($_SERVER["REQUEST_METHOD"] == "POST") 
{
    $name = test_input($_POST["name"]);
}
?>


<?php
//echo the new field entry
echo $name;
?>

      

+1


source







All Articles