2017-10-05 123 views
0

好的,我有这个注册表格,它不会将我放入它的信息发送到我的phpmyadmin数据库,并且在您读取我的代码之前,我的数据库模式如下所示:我的注册表不会将信息发送到我的数据库

Schema name:cms 
Tables:articles,users 
Users:user_id,user_name,user_password(user_id is auto increment) 

这里是我的代码:

这是的index.php文件

<?php 



    echo "<form action='signup.php' method='POST'> 
      <input type='text' name='username' placeholder='Username'> 
     <input type='password' name='password' placeholder='Password'> 
     <button type='submit'>SIGN UP</button> 
    </form>"; 
?> 

这是signup.php文件:

<?php 

include 'connection.php'; 


$username = $_POST['username']; 
$password = $_POST['password']; 




echo $sql = "INSERT INTO users (user_name, user_password) 
VALUES ('$username', '$password')"; 


$result = mysqli_query($conn, $sql); 

header("Location: index.php"); 

而且connection.php文件:

<?php 
try{ 
$pdo = new PDO('mysql:host=localhost;dbname=cms', 'root', ''); 
} catch (PDOException $e) { 
    exit('Database error.'); 

} 



?> 

下面这段代码是一个更大的代码的一部分,所以如果你需要更多的代码生病后它,我也有两个不同的文件,都有index.php名称,但他们都在不同的地方,这index.php和signup.php是在同一个文件夹,所以我不认为有一个错误there.Any帮助将非常感谢! :d

整个的index.php代码:

<?php 
 

 

 
session_start(); 
 

 
include_once('../includes/connection.php'); 
 

 
if (isset($_SESSION['logged_in'])){ 
 
\t 
 
\t 
 
\t ?> 
 
\t 
 
\t 
 
\t <html> 
 
\t <head> 
 
\t \t <title>CMS Tutorial</title> 
 
\t \t <link rel="stylesheet" href="../assets/style.css" /> 
 
\t </head> 
 
\t <body> 
 
\t \t <div class="container"> 
 
<a href="index.php" id="logo">CMS</a> 
 

 

 
\t \t <br /> 
 

 
<ol> 
 

 
    <li><a href="add.php">Add Article</a></li> 
 
\t <li><a href="delete.php">Delete Article</a></li> 
 
\t <li><a href="logout.php">Logout</a></li> 
 

 

 

 

 
</ol> 
 

 
\t \t </div> 
 
\t </body> 
 
</html> 
 
\t 
 
\t 
 
\t 
 
\t 
 
\t 
 
\t 
 
\t 
 
\t 
 
\t <?php 
 
\t 
 
\t 
 
}else{ 
 
\t 
 
\t if (isset($_POST['username'], $_POST['password'])) { 
 
\t \t $username= $_POST['username']; 
 
\t \t $password= ($_POST['password']); 
 
\t \t 
 
\t \t 
 
\t \t if(empty($username) or empty($password)) { 
 
\t \t \t 
 
\t \t \t $error = 'All fields are required!'; 
 
\t \t \t 
 
\t \t \t 
 
\t \t }else{ 
 
\t \t \t $query = $pdo->prepare("SELECT * FROM users WHERE user_name = ? AND user_password = ?"); 
 
\t \t \t 
 
\t \t \t $query->bindValue(1, $username); 
 
\t \t \t $query->bindValue(2, $password); 
 
\t \t \t 
 
\t \t \t $query->execute(); 
 
\t \t \t 
 
\t \t \t 
 
\t \t \t $num = $query->rowCount(); 
 
\t \t \t 
 
\t \t \t if ($num==1){ 
 
\t \t \t \t 
 
\t \t \t \t 
 
\t \t \t \t $_SESSION['logged_in'] = true; 
 
\t \t \t \t header('Location: index.php'); 
 
\t \t \t \t exit(); 
 
\t \t \t \t 
 
\t \t \t \t 
 
\t \t \t \t 
 
\t \t \t }else{ 
 
\t \t \t \t 
 
\t \t \t $error='Incorrect details!'; 
 
\t \t \t 
 
\t \t \t } 
 
\t \t \t 
 
\t \t } 
 
\t \t 
 
\t \t 
 
\t \t 
 
\t } 
 
\t 
 
\t 
 
\t 
 
?> 
 

 
<html> 
 
\t <head> 
 
\t \t <title>CMS Tutorial</title> 
 
\t \t <link rel="stylesheet" href="../assets/style.css" /> 
 
\t </head> 
 
\t <body> 
 
\t \t <div class="container"> 
 
<a href="index.php" id="logo">CMS</a> 
 

 

 
\t \t 
 

 

 

 
<?php if(isset($error)) { ?> 
 

 
<small style="color:#aa0000;"><?php echo $error; ?></small> 
 

 
<?php } ?> \t 
 

 

 
\t \t 
 
\t \t <form action="index.php" method="post" autocomplete="off"> 
 
        <input type="text" name="username" placeholder="Username" /> 
 
        <input type="password" name="password" placeholder="Password" /> 
 
        <input type="submit" value="Login" /> \t 
 

 

 

 
\t \t \t \t 
 
\t \t </form> 
 
\t \t <?php 
 
\t \t 
 
\t \t 
 

 
echo "<form action='signup.php' method='POST'> 
 
     <input type='text' name='username' placeholder='Username'> 
 
\t <input type='password' name='password' placeholder='Password'> 
 
\t <button type='submit'>SIGN UP</button> 
 
</form>"; 
 
?> 
 
\t \t 
 
\t \t </div> 
 
\t </body> 
 
</html> 
 

 

 
<?php 
 
\t 
 
\t 
 
\t 
 
} 
 

 

 

 
?>

+1

你混合** ** PDO和** mysqli ** –

+1

您正在访问错误的连接变量。在连接文件中声明了$ pdo,但是您使用$ conn。也做出决定。你用mysqli还是pdo? – Akintunde007

+0

哦,是的,我混淆了这两个,认为我可以同时使用,猜不是......我可以以任何方式更改注册表,所以我会使用pdo?因为我写的其他大部分代码都是用pdo编写的。 –

回答

0

基于问题的意见 - 试试这个,而不是mysqli_query:

$statement = $pdo->prepare("INSERT INTO users (username, password) 
    VALUES(:username, :password)"); 
$result = $statement->execute(array(
    "username" => $username, 
    "password" => $password 
)); 
$statement = null; 
+0

感谢但出现错误: 注意:未定义的变量:pdo在第4行的C:\ xampp \ htdocs \ phpacademy \ cms \ admin \ signup.php中 致命错误:未捕获错误:调用成员函数prepare )在C:\ xampp \ htdocs \ phpacademy \ cms \ admin \ signup.php中为null:堆栈跟踪:#0 {main}抛出C:\ xampp \ htdocs \ phpacademy \ cms \ admin \ signup.php 4 –

+0

不应该因为您在** connection.php **中定义** $ pdo **而抛出该错误。 $ pdo = new PDO('mysql:host = localhost; dbname = cms','root',''); – wast

+0

对不起,但我不明白你在第二条评论中想说什么? –

相关问题