Create, edit, User Form with PHP MySQL
This post will show you how to create a sample form which enable User to perform the tasks below.
- Select a Company Name
- List out all personnel associate to the Company Selected
- Enable User , to add new User
- Enable User to edit User Info
Files involve
1.This sample contains 3 Files (1)selectCompanyEditPeople.php (2)peopleEditForm.php (3)peopleInsert.php
2.selectCompanyEditPeople.php -> The Script will display the list personnel associate to the Selected Company
-> Link to peopleEditForm .php
-> Link to peopleInsert.php
3. peopleEditForm -> This file will display the Selected User info in side the Form
-> User will be able to edit the text box and save the Value back to mySQL Database
4. peopleInsert -> This File will enable User to add new users
-> When Edit done , the Script will call ” selectCompanyEditPeople.php ” and relist table.
Prior to that you need to complete the items listed Below
- Mamp or xampp with phpMyAdmin install into your Computer , check out here
- A Sample Database created Check out Here
- New User Created . Check out Here
- A New table created , check out Here
- Insert Data into the Table Here
selectCompanyEditPeople.php
<?php /* * File: SelectCompanyOutputPeople */ $hostName ="localhost"; $userName ="jane"; $userPassword ="jane"; $database ="janedb"; $comArray = array(); $dbConnectionStatus = new mysqli($hostName, $userName, $userPassword,$database); //Connection Error if ($dbConnectionStatus->connect_error){ die("Connection failed: " . $dbConnectionStatus->connect_error); } // Connected to Database JaneDB // Object oriented -> pointing if($dbConnectionStatus->query("SELECT DATABASE()")){ $dbSuccess =true; // $result = $dbConnectionStatus->query("SELECT DATABASE()"); $row = $result->fetch_row(); printf("Default database is %s.\n", $row[0]); $result->close(); } $thisScriptName = "selectCompanyEditPeople.php"; // DB Connect Successful if ($dbSuccess) { // Check whetehr the User Select Anything // Check whther select anything // Check whether any Get Valuein Httpp if (isset($_POST["infoID"]) or isset($_GET["companyName"]) ){ if(isset($_POST["infoID"])){$companyName = $_POST["infoID"];} if(isset($_GET["companyName"])){$companyName = $_GET["companyName"];} // Get the details of the company selected $selectData = "SELECT * FROM testtable WHERE companyname = '$companyName' "; // Send Select Query $selectData_Query = mysqli_query($dbConnectionStatus,$selectData ); //-------------------------------------------------------------------------- // Declare Array // Get all the User data and save that in an array $arrayData = array(); while($rows=mysqli_fetch_assoc($selectData_Query)){ $arrayData[] = $rows; } //-------------------------------------------------------------------------- // Free Queries mysqli_free_result($selectData_Query ); //Output Table echo '<div style=" font-family: arial, helvetica, sans-serif; ">'; echo '<div style="margin-left: 100; ">'; echo '<table border="1" padding="5">'; echo '<tr> <td>id</td> <td>firstname</td> <td>lastname</td> <td>email</td> <td>company</td> <td>Edit</td> </tr> '; // Output Data Row by row foreach($arrayData as $data){ $thisID = $data['id']; $personEditLink = '<a href="peopleEditForm.php?personID='.$thisID.'">Edit</a>'; echo'<tr>'; // Search through the array print out value if see the Key eg: 'id', 'firstname ' etc. echo'<td>'.$data['id'].'</td>'; echo'<td>'.$data['firstname'].'</td>'; echo'<td>'.$data['lastname'].'</td>'; echo'<td>'.$data['email'].'</td>'; echo'<td>'.$data['companyname'].'</td>'; echo"<td>".$personEditLink."</td>"; echo'</tr>'; } echo '</table>'; // END: Output section // --------------------------Form to Insert Peoplen Table---------------------------------------------------------- //Declare HTML Field Name $fld_FirstName = '<input type="text" name="firstNames" placeholder="First Name" size="10" maxlength="50"/>'; $fld_LastName = '<input type="text" name="lastNames" placeholder="Last Name" size="10" maxlength="50"/>'; $fld_email = '<input type="text" name="emailss" placeholder="Email" size="10" maxlength="50"/>'; $fld_company = '<input type="text" name="theCompanys" placeholder="Company Name" size="10" maxlength="50"/>'; echo '<form name="insertPeople" action="peopleInsert.php" method="post">'; //echo '<input type="hidden" name="infoID" value="'.$companyName.'" />'; echo '<table>'; echo '<tr> <td colspan="5"></td> </tr> '; echo '<tr> <td colspan="5"><hr /></td> </tr> '; echo '<tr> <td colspan="5"></td> </tr> '; echo '<tr> <td>'.$fld_FirstName.'</td> <td>'.$fld_LastName.'</td> <td>'.$fld_email.'</td> <td>'.$fld_company.'</td> <td><input type="submit" value="Add" /></td> </tr> '; echo '</table>'; echo '</form>'; echo '</div>'; echo '</div>'; //----------------------------Populate Selection Form------------------------------------------------------------- } else { // Assign Query $selectDataOption = " SELECT * FROM testtable"; // Send Select Query $selectDataOption_Query = mysqli_query($dbConnectionStatus,$selectDataOption); echo '<form action="selectCompanyEditPeople.php" method="post">'; echo '<select name="infoID">'; echo '<option value="" label="" selected="selected">..select company..</option>'; //------------------------------------------------------------------------------- if (mysqli_num_rows($selectDataOption_Query)>0){ // while there is still data in row // assign selected mySQL data into 'arrayData' array while($rowss=mysqli_fetch_assoc($selectDataOption_Query)){ // Before insert Check whether the value inside Array $ID = $rowss['id']; $companyNames =$rowss['companyname']; $firstName =$rowss['firstname']; // Loop Through array and Check // If the Value is not inside the Array if(! in_array($companyNames,$comArray)){ array_push($comArray,$companyNames); echo '<option value="'. $companyNames.'">'.$companyNames.'</option>'; } } //Populate the Option mysqli_free_result($selectDataOption_Query ); } echo '</select>'; echo '<input type="submit" />'; echo '</form>'; // END: Select company from dropdowm } // END: if ($dbSuccess) } unset($companyName); echo "<br /><hr /><br />"; echo '<a href="SelectCompanyEditPeople.php">Select Another</a>'; echo ' '; echo '<a href="/">Back to LocalHost</a><br />'; ?>
peopleEditForm.php
<?php /* * File: personEditForm.php * * *===================================== */ $hostName ="localhost"; $userName ="jane"; $userPassword ="jane"; $database ="janedb"; $dbConnectionStatus = new mysqli($hostName, $userName, $userPassword,$database); //Connection Error if ($dbConnectionStatus->connect_error){ die("Connection failed: " . $dbConnectionStatus->connect_error); } // Connected to Database JaneDB // Object oriented -> pointing if($dbConnectionStatus->query("SELECT DATABASE()")){ $dbSuccess =true; // $result = $dbConnectionStatus->query("SELECT DATABASE()"); $row = $result->fetch_row(); printf("Default database is %s.\n", $row[0]); $result->close(); } $thisScriptName = "peopleEditForm.php"; if ($dbSuccess) { if (isset($_POST["saveClicked"])){ // SAVE button being clicked // get Post Data $firstName = $_POST["FirstName"]; $lastName = $_POST["LastName"]; $emails = $_POST["Email"]; $companyName = $_POST["CompanyName"]; $ID = $_POST["theID"]; // assign Query $inname__select = "UPDATE testtable SET firstname = '$firstName' , lastname= '$lastName' , email ='$emails' ,companyname ='$companyName' WHERE id = $ID "; //------------------------------------------------------------------------------ // Send Query $inname__select_Query= mysqli_query($dbConnectionStatus,$inname__select); if (empty($firstName)) { echo '<span style="color:red; ">You Must Select First Name</span><br /><br />'; } else{ if ($inname__select_Query) { echo 'used to Successfully update Info.<br /><br />'; // If Sucessful Update back to , main page echo header("Location: selectCompanyEditPeople.php"); } else { echo '<span style="color:red; ">FAILED to update Info.</span><br /><br />'; } } mysqli_free_result($inname__select_Query ); } else{ // Get the details of the person selected http from slectCompanyEditPeople.php file $personID = $_GET["personID"]; $cname__select = "SELECT * FROM testtable WHERE id = $personID"; // Execute Query $cname__select_Query= mysqli_query($dbConnectionStatus,$cname__select); // While there is info in row while($rows=mysqli_fetch_assoc($cname__select_Query)){ $c_Id = $rows['id']; $c_firstName = $rows['firstname']; $c_lastName = $rows['lastname']; $c_emails = $rows['email']; $c_companyName = $rows['companyname']; } mysqli_free_result($cname__select_Query ); echo '<h2 style="font-family: arial, helvetica, sans-serif;" > Person EDIT Form </h2>'; // FORM postPerson echo '<form name="postPerson" action="'.$thisScriptName.'" method="post">'; echo '<input type="hidden" name="theID" value="'.$c_Id.'"/>'; echo '<input type="hidden" name="saveClicked" value="1"/>'; echo ' <table> <tr> <td>FirstName</td> <td><input type="text" name="FirstName" value="'.$c_firstName.'"/></td> </tr> <tr> <td>LastName</td> <td><input type="text" name="LastName" value="'.$c_lastName.'"/></td> </tr> <tr> <td>Email</td> <td><input type="text" name="Email" value="'.$c_emails.'"/></td> </tr> <tr> <td>Company Name</td> <td><input type="text" name="CompanyName" value="'.$c_companyName.'"/></td> </tr> <tr> <td></td> <td align="right"><input type="submit" value="Save" /></td> </tr> </table> '; echo '</form>'; // END: FORM postPerson } echo "<br /><hr /><br />"; echo '<a href="/">Back to LocalHost</a><br />'; } ?>
peopleInsertForm.php
<?php /* * File: peopleInsert.php *===================================== */ $hostName ="localhost"; $userName ="jane"; $userPassword ="jane"; $database ="janedb"; $comArray = array(); $dbConnectionStatus = new mysqli($hostName, $userName, $userPassword,$database); //Connection Error if ($dbConnectionStatus->connect_error){ die("Connection failed: " . $dbConnectionStatus->connect_error); } // Connected to Database JaneDB // Object oriented -> pointing if($dbConnectionStatus->query("SELECT DATABASE()")){ $dbSuccess =true; // $result = $dbConnectionStatus->query("SELECT DATABASE()"); $row = $result->fetch_row(); printf("Default database is %s.\n", $row[0]); $result->close(); } if ($dbSuccess) { $firstNames = $_POST["firstNames"]; $lastNames = $_POST["lastNames"]; $emailss = $_POST["emailss"]; $theCompanys = $_POST["theCompanys"]; //Assign SQL $user_SQLinsert = "INSERT INTO testtable ("; $user_SQLinsert .= "firstname, "; $user_SQLinsert .= "lastname, "; $user_SQLinsert .= "email , "; $user_SQLinsert .= "companyname"; $user_SQLinsert .= ") "; $user_SQLinsert .= "VALUES ("; $user_SQLinsert .= "'".$firstNames."', "; $user_SQLinsert .= "'".$lastNames."', "; $user_SQLinsert .= "'".$emailss."', "; $user_SQLinsert .= "'".$theCompanys."' "; $user_SQLinsert .= ") "; if (empty($firstNames )) { echo '<span style="color:red; ">Please Enter Your Name</span><br /><br />'; } else { echo '<span style = "text-decoration: underline;"> SQL statement:</span> <br />'.$user_SQLinsert.'<br /><br />'; if ($dbConnectionStatus->query($user_SQLinsert)) { echo 'Register Successful<br /><br />'; //Passing String to URL for selectCompanyEditPeople.php to retrieve Value header("Location: selectCompanyEditPeople.php?companyName=".$theCompanys); } else { echo '<span style="color:red; ">FAILED to Register.</span><br /><br />'; } } } ?>