简体   繁体   中英

Updating mySQL table with user interface - PHP

[Sample Look]

I'm trying to make an interface where you can edit/add/remove fields of a mySQL database. This is how it looks visually, and I have all the functionality on the client side working.

My question is: How can I pass any edits/adds/removals to the server side? I'll include a link for my JSFiddle . And the code below will show how I currently great the table.

<?php
  $servername = "localhost";
  $username = "lalalal";
  $password = "lalalal";

  $link = mysqli_connect("localhost", "lalala", "lalala", "lalala");

  // Check connection
  if($link === false){
      die("ERROR: Could not connect. " . mysqli_connect_error());
  }

  $sqlStart = "SELECT `Name`, `EXT`, `Returning Time`, `Returning Date`, `Out`, `Reset`, `Booked` FROM `lalala`";
    if($result = mysqli_query($link, $sqlStart)){
        if(mysqli_num_rows($result) > 0){
            echo "<table id = contactTable>";
                echo "<tr id = row1>";
                    echo "<th id = sortTable onclick=sortTable(0)>Name &#8597;</th>";
                    echo "<th style = width:100px;>EXT</th>";
                    echo "<th style = width:300px;>Returning Time</th>";
                    echo "<th style = width:300px;>Returning Date</th>";
                    echo "<th style = width:70px;>Out</th>";
                    echo "<th style = width:100px;>Reset</th>";
                    echo "<th style = width:600px;>Booked</th>";
                echo "</tr>";
            while($row = mysqli_fetch_array($result)){
              $currentCheck = $row['Out'];
                  if ($currentCheck == 0) {
                    echo "<tr>";
                    echo "<td>" . $row['Name'] . "</td>";
                    echo "<td>" . $row['EXT'] . "</td>";

                    $currentTime = $row['Returning Time'];
                    if ($currentTime == 0) {
                      echo "<td> <form> <input type = 'time', id = 'timePickChange'> </form> </td>";
                    } else {
                      echo "<td> <form> <input type = 'time', id = 'timePickChange' value =" . $currentTime . "> </form> </td>";
                    }
                    
                    $currentDate = $row['Returning Date'];
                    echo "<td> <form> <input type = 'date', id = 'datePickChange' value =" . $currentDate . "> </form> </td>";
                    echo "<td> <form onclick = 'checkIfOutRow(this)'> <input type = 'checkbox', onclick = 'checkIfOutValue(this)'> </form> </td>";
                    echo "<td> <button onclick = 'clearForm(this)', id = buttonClear>Reset</button> </td>";
                    echo "<td> <textarea rows = '1', cols = '60'> </textarea> </td>";


                  } else if ($currentCheck == 1) {
                    echo "<tr style = 'background-color: #E2E9FD'>";
                    echo "<td>" . $row['Name'] . "</td>";
                    echo "<td>" . $row['EXT'] . "</td>";
                    $currentTime = $row['Returning Time'];
                    echo "<td> <form> <input type = 'time', id = timePickChange disabled> </form> </td>";
                    $currentDate = $row['Returning Date'];
                    echo "<td> <form> <input type = 'date', id = datePickChange disabled> </form> </td>";
                    echo "<td> <form onclick = 'checkIfOutRow(this)'> <input type = 'checkbox', onclick = 'checkIfOutValue(this)' checked> </form> </td>";
                    echo "<td> <button onclick = 'clearForm(this)', id = buttonClear>Reset</button> </td>";
                    echo "<td> <textarea rows = '1', cols = '60'> </textarea> </td>";
                  }
                echo "</tr>";
            }
            echo "</table>";
            // Free result set
            mysqli_free_result($result);
        } else{
            echo "No records matching your query were found.";
        }
    } else{
        echo "ERROR: Could not able to execute $sqlStart. " . mysqli_error($link);
    }
?>

Depending on your data validation model, you might want to control the inputs value client side before posting them to your back-end.

AFAIK, you're already adding/editing/removing your contacts on the client side, so If I understand correctly, when your user should click on Edit/Remove & confirm , it would be a confirmation of what the user has done in the browser, this doesn't really change much apart from the fact that otherwise you might need dedicated buttons/row (or any other bindable events).

For these operations what you could do is proceed to bulk delete / edit, and this could be easily done by filtering out in your JS all the modified/deleted data and sending it to your back end PHP with Ajax/jQuery in the form of a stringified array. As for insertion operation you'd submit them at the same time you add them to your table, by executing a POST operation.

And it could be done with something like this :

$.ajax({
  method: "PUT",
  url: "some.php",
  data: JSON.stringify(myUpdatedDataInAnArray) 
// you might need to stringify your array to ensure format ? 
})
  .done(function( msg ) {
    alert( "Data Updated: " + msg );
  });

In your back end php, you'd listen for POST/PUT/DELETE methods with something like that :

if (isset($_POST['add'])){
   do your thing
}
if (isset($_PUT['updated'])){
   //Since you're sending a stringified array, you must parse it with 
   $myArray = json_decode($_PUT['updated']);
   do your thing
}
if (isset($_DELETE['deleted'])){
   do your thing
}

I say Ajax because using a traditional POST/PUT/DELETE form would result in refreshing the page.

Here are some useful refs :

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM