我对php很新,但是必须通过php将数据库链接到html作为uni课程的一部分,所以我试图在我们创建的网站上实现注册和登录功能。据我所知,这个代码应该可以工作,但每当我尝试通过html页面运行它时,它会发布代码本身而不是运行。这是登录和注册的情况。我做错了什么?
//registration
<?php
define('DB_HOST', 'xxx');
define('DB_NAME', 'xxx');
define('DB_USER','xxx');
define('DB_PASSWORD','xxx');
$con = mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die("Failed to connect to MySQL: " . mysql_error());
$db = mysql_select_db(DB_NAME,$con) or die("Failed to connect to MySQL Database: " . mysql_error());
function NewUser() {
$Name = $_POST['Name'];
$Username = $_POST['Username'];
$Email = $_POST['Email'];
$Password = $_POST['Password'];
$query = "INSERT INTO userDetails (Username,Email,Name,Password) VALUES ('$Username','$Email','$Name','$Password')";
$data = mysql_query($query) or die(mysql_error());
if($data) {
echo "YOUR REGISTRATION IS COMPLETED...";
}
}
function SignUp() {
if(!empty($_POST['Username'])) {
$query = mysql_query("SELECT * FROM userDetails WHERE Username = '$_POST[Username]' AND Password = '$_POST[Password]'") or die(mysql_error());
if(!$row = mysql_fetch_array($query) or die(mysql_error())) {
newuser();
} else {
echo "YOU ARE ALREADY A REGISTERED USER...";
}
}
}
if(isset($_POST['submit'])) {
SignUp();
}