Database - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.



Your result can be seen below.

Result of php executing





Full code of Database.php

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.     
  5. <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
  6.     JobID: <input type="int" name="id"><br>
  7.     Title: <input type="text" name="name"><br>
  8.     Description: <input type="text" name="description"><br>
  9.     Date: <input type="text" name="Date"><br>
  10.     <input type="submit"><br>
  11. </form>
  12.  
  13. <?php
  14. // Your code here!
  15. $servername = "localhost";
  16. $username = "username";
  17. $password = "password";
  18. $conn = new mysqli($servername,$username,$password);
  19. if (mysqli_connect_error()){
  20.     die("Connection Failed: ".mysqli_connect_error());
  21. }
  22.  
  23. if (!function_exists('mysqli_init') && !extension_loaded('mysqli')) {
  24.     echo "We don't have mysqli!!!";
  25.  
  26. $sql="CREATE DATABASE PNM";
  27. if($conn->query($sql)=== TRUE){
  28.     echo "Database Created";
  29. }
  30. else{
  31.     echo "Error Creating Database";
  32.     $conn->error;
  33. }
  34.  
  35. $conn = new mysqli($servername,$username,$password,$sql);
  36.  
  37. if (mysqli_connect_error()){
  38.     die("Connection: ".mysqli_connect_error());
  39. }
  40.  
  41.  
  42.  
  43. $table1 = "CREATE TABLE jobs(JobID INT(6) UNSIGNED PRIMARY KEY,
  44. Title VARCHAR(45) NOT NULL, Description VARCHAR(255) NOT NULL,
  45. JobDate DATE(11) NOT NULL";
  46.  
  47. if($conn->query($table1) === TRUE){
  48.     echo "Table Created";
  49. }
  50. else{
  51.     echo "Error Creating Table: ".$conn->error;
  52. }
  53.  
  54. $values = "Insert Into jobs(JobID,Title,Description,JobDate)
  55. VALUES (?.?.?.?)";
  56. $stmt -> bind_param("issb",$jobId,$jobTitle,$jobDescription,$date);
  57. $jobId = $_POST["id"];
  58. $jobTitle = $_POST["name"];
  59. $jobDescription = $_POST["description"];
  60. $date = $_POST["JobDate"];
  61. $stmt->execute();
  62.  
  63. if(mysqli_query($conn,$values)){
  64.     echo "New record added";
  65. }
  66. else{
  67.     echo "Error ".$values. "<br>" .mysqli_error($conn);
  68. }
  69.  
  70. mysqli_close();
  71. ?>
  72. </body>
  73. </html>
File Description
  • Database
  • PHP Code
  • 07 Aug-2019
  • 1.75 Kb
You can Share it: