使用PHP INSERT到mysql数据库的问题

时间:2017-03-27 03:58:30

标签: php mysql

我尝试使用PHP和mySQL写入数据库,但是它给了我一条错误消息,我没有看到代码有任何问题。我使用MAMP因此需要一个端口。请帮忙!!。共有2页:signup.php& checklogin.php(数据库连接字符串所在的位置)

checklogin.php

<?
//Database connection
$server = 'localhost';
$username = 'root';
$password = 'root';
$database = 'siteproject';


try{
    $conn = new PDO("mysql:host=$server;port=8889;dbname=$database;", $username, $password);
} catch(PDOException $e){
    die("Connection Failed" .$e->getMessage()); 
    }

?>

signup.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<? 
// Header page of the entire site
include ("header.php");
require ("checklogin.php");
?>

<?
if(!empty($_POST['username']) && !empty($_POST['password'])):
    $sql = "INSERT INTO siteUsers (username, pword) VALUES (:userName, :Pass)";
    $stmt = $conn->prepare($sql);
    $stmt->bindParam(':userName', $_Post["username"]);
    $stmt->bindParam(':Pass', $_Post["password"]);
        if ($stmt->execute()):
                die('Successfully added');
        ELSE:
                echo ($_POST['username']);
                die('<h1>Data wasnt inserted</h1>');
        endif;

endif;

?>
<body>

<div align="center"><h1>Register Page</h1></div>
<form action="signup.php" method="POST">

  <div align="center">
  <input type="text" placeholder="Email" name="username">
  <input type="password" placeholder="Password" name="password">
  <input type="password" placeholder="Confirm Password" name="confirm_password">
  <input type="submit">
  </div>

</form>

</body>
</html>

谢谢你的帮助, 利

0 个答案:

没有答案