test - 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 test.php

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>METANIT.COM</title>
  5. <meta charset="utf-8" />
  6. </head>
  7. <body>
  8. <?php
  9. if (isset($_POST["username"]) && isset($_POST["userage"])) {
  10.      
  11.     $username = $_POST["username"];
  12.     $userage = $_POST["userage"];
  13.     try {
  14.         $conn = new PDO("mysql:host=localhost;dbname=testdb1", "root", "mypassword");
  15.         $sql = "INSERT INTO Users (name, age) VALUES ('$username', $userage)";
  16.         $affectedRowsNumber = $conn->exec($sql);
  17.         // если добавлена как минимум одна строка
  18.         if($affectedRowsNumber > 0 ){
  19.             echo "Data successfully added: name=$username  age= $userage";  
  20.         }
  21.     }
  22.     catch (PDOException $e) {
  23.         echo "Database error: " . $e->getMessage();
  24.     }
  25. }
  26. ?>
  27. <h3>Create a new User</h3>
  28. <form action="" method="post">
  29.     <p>User Name:
  30.     <input type="text" name="username" id="username" /></p>
  31.     <p>User Age:
  32.     <input type="number" name="userage" id="userage" /></p>
  33.     <input type="submit" name="submit" value="Save">
  34. </form>
  35. </body>
  36. </html>
File Description
  • test
  • PHP Code
  • 07 Jun-2021
  • 1.04 Kb
You can Share it: