新增一個專用來連線的 php 檔案
<?php $server_name = 'localhost'; $username = 'pikaju'; $password = '1234'; $db_name = 'pikaju'; $conn = new mysqli($server_name, $username, $password, $db_name); if($conn->connect_error) { die('資料庫連線錯誤:' . $conn->connect_error); } ?>
在data.php的檔案 引入連線的 php
<?php require_once ('conn.php'); if (empty($_POST['name']) || empty($_POST['age'])) { echo "請確認資料無誤<br>"; exit(); } echo "Hello " . $_POST['name'] .'<br>'; echo "Your age is " . $_POST['age'] . '<br>'; ?>