Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- table
- <script language="JavaScript" type="text/javascript">
- function checkDelete(){
- return confirm('Are you sure?');
- }
- </script>
- <?php
- // Create connection
- $conn = new mysqli("localhost:3306", "root", "", "table1");
- // Check connection
- if ($conn->connect_error) {
- die("Connection failed: " . $conn->connect_error);
- }
- $sql = "SELECT * from sample1";
- $result = $conn->query($sql);
- if ($result->num_rows > 0) {
- // output data of each row
- //echo '<div id="tablediv">';
- echo "<table width='850' border='1' cellpadding='1' cellspacing='1'>";
- echo "<tr>";
- echo "<th>Account ID</th>";
- echo "<th>Username</th>";
- echo "<th>First Name</th>";
- echo "<th>Middle Initial</th>";
- echo "<th>Last Name</th>";
- echo "<th>Birthday</th>";
- echo "<th>Age</th>";
- echo "</tr>";
- while($row = mysqli_fetch_array($result)){
- echo "<tr>";
- echo "<td>" . $row['userID'] . "</td>";
- echo "<td>" . $row['username'] . "</td>";
- echo "<td>" . $row['fname'] . "</td>";
- echo "<td>" . $row['mi'] . "</td>";
- echo "<td>" . $row['lname'] . "</td>";
- echo "<td>" . $row['birthday'] . "</td>";
- echo "<td>" . $row['age'] . "</td>";
- echo "</tr>";
- }
- echo "</table>";
- //echo "</div>";
- mysqli_free_result($result);
- } else {
- echo "0 results";
- }
- $conn->close();
- ?>
- update
- <?php
- $host = "localhost:3306";
- $user = "root";
- $password = "";
- $database = "table1";
- $userID = "";
- $username = "";
- $pass = "";
- $fname = "";
- $mi = "";
- $lname = "";
- $birthday = "";
- $age = "";
- mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
- try{
- $connect = mysqli_connect($host, $user, $password, $database);
- }catch (Exception $ex){
- echo "Error";
- }
- function getPosts()
- {
- $posts = array ();
- $posts [0] = $_POST['userID'];
- $posts [1] = $_POST['username'];
- $posts [2] = $_POST['pass'];
- $posts [3] = $_POST['fname'];
- $posts [4] = $_POST['mi'];
- $posts [5] = $_POST['lname'];
- $posts [6] = $_POST['birthday'];
- $posts [7] = $_POST['age'];
- return $posts;
- }
- //search
- if (isset($_POST['search']))
- {
- $data = getPosts();
- $search_Query = "SELECT * FROM Sample1 WHERE userID = $data[0]";
- $search_Result = mysqli_query($connect, $search_Query);
- if($search_Result)
- {
- if(mysqli_num_rows($search_Result))
- {
- while($row = mysqli_fetch_array($search_Result))
- {
- $userID = $row ['userID'];
- $username = $row ['username'];
- $pass = $row ['pass'];
- $fname = $row ['fname'];
- $mi = $row ['mi'];
- $lname = $row ['lname'];
- $birthday = $row ['birthday'];
- $age = $row ['age'];
- }
- }else{
- echo 'No data for this ID';
- }
- }else{
- echo 'Result Error!';
- }
- }
- //Add
- if (isset($_POST['insert']))
- {
- $data = getPosts();
- $insert_Query = "INSERT INTO `sample1`(`username`, `pass`, `fname`, `mi`, `lname`, `birthday`, `age`) VALUES ('$data[1]','$data[2]','$data[3]','$data[4]','$data[5]','$data[6]','$data[7]')";
- try{
- $insert_Result = mysqli_query($connect, $insert_Query);
- if($insert_Result)
- {
- if(mysqli_affected_rows($connect) > 0)
- {
- echo 'Data Inserted!';
- }else{
- echo'Data not Inserted!';
- }
- }
- } catch (Exception $ex)
- {
- echo 'Error Insert!'.$ex->getMessage();
- }
- }
- //Delete
- if (isset($_POST['delete']))
- {
- $data = getPosts();
- $delete_Query = "DELETE FROM `sample1` WHERE `userID` = $data[0])";
- try{
- $delete_Result = mysqli_query($connect, $delete_Query);
- if($delete_Result)
- {
- if(mysqli_affected_rows($connect) > 0)
- {
- echo 'Data Deleted!';
- }else{
- echo'Data not Deleted!';
- }
- }
- } catch (Exception $ex)
- {
- echo 'Error Delete!'.$ex->getMessage();
- }
- }
- //Update
- if (isset($_POST['update']))
- {
- $data = getPosts();
- $update_Query = "UPDATE `sample1` SET `username`='$data[1]',`pass`='$data[2]',`fname`='$data[3]',`mi`='$data[4]',`lname`='$data[5]',`birthday`='$data[6]',`age`='$data[7]' WHERE `userID`= $data[0]";
- try{
- $update_Result = mysqli_query($connect, $update_Query);
- if($update_Result)
- {
- if(mysqli_affected_rows($connect) > 0)
- {
- echo 'Data Updated!';
- }else{
- echo'Data not Updated!';
- }
- }
- } catch (Exception $ex)
- {
- echo 'Error Update!'.$ex->getMessage();
- }
- }
- ?>
- <html>
- <body>
- <form action="?title=update" method="post" name="formko">
- <label>ID:</label> <input type="number" name="userID" value="<?php echo $userID; ?>"/><br><br>
- <label>User Name:</label> <input type="text" name="username" value="<?php echo $username; ?>"/><br><br>
- <label>Password:</label> <input type="password" name="pass" value="<?php echo $pass; ?>"/><br><br>
- <label>First Name:</label> <input type="text" name="fname" value="<?php echo $fname; ?>"/><br><br>
- <label>Middle Initial:</label> <input type="text" name="mi" value="<?php echo $mi; ?>"/><br><br>
- <label>Last Name:</label> <input type="text" name="lname" value="<?php echo $lname; ?>"/><br><br><br>
- <label>Birthday:</label> <input type="date" name="birthday" value="<?php echo $birthday; ?>"/><br><br><br>
- <label>Age:</label> <input type="text" name="age" value="<?php echo $age; ?>"/><br><br><br>
- <div>
- </label><input type="Submit" name="insert" value="Add"/>
- </label><input type="Submit" name="update" value="Update"/>
- </label><input type="Submit" name="delete" value="Delete"/>
- <label><input type="Submit" name="search" value="Find"/>
- </label><input type="Reset" value="Reset"/>
- </div>
- </form>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement