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.
Result of php executing
Full code of test.php
- <!DOCTYPE html>
- <html>
- <head>
- <title>METANIT.COM</title>
- <meta charset="utf-8" />
- </head>
- <body>
- <?php
- if (isset($_POST["username"]) && isset($_POST["userage"])) {
- $username = $_POST["username"];
- $userage = $_POST["userage"];
- try {
- $conn = new PDO("mysql:host=localhost;dbname=testdb1", "root", "mypassword");
- $sql = "INSERT INTO Users (name, age) VALUES ('$username', $userage)";
- $affectedRowsNumber = $conn->exec($sql);
- // если добавлена как минимум одна строка
- if($affectedRowsNumber > 0 ){
- echo "Data successfully added: name=$username age= $userage";
- }
- }
- catch (PDOException $e) {
- echo "Database error: " . $e->getMessage();
- }
- }
- ?>
- <h3>Create a new User</h3>
- <form action="" method="post">
- <p>User Name:
- <input type="text" name="username" id="username" /></p>
- <p>User Age:
- <input type="number" name="userage" id="userage" /></p>
- <input type="submit" name="submit" value="Save">
- </form>
- </body>
- </html>