2013-02-27 105 views
0

我正在试验PHP和Mysql。我使用xampp在mu localhost创建了一个数据库和表。我也创建了一个假设通过执行查询来填充我的表的文件,但奇怪的是,我没有得到任何错误,但同时没有数据插入到我的数据库:PHP将数据插入到Mysql

CODE:

register.php:

<?php 

session_start(); 

if(isset($_POST['submitted'])){ 

    include('connectDB.php'); 

    $UserN = $_POST['username']; 
    $Upass = $_POST['password']; 
    $Ufn = $_POST['first_name']; 
    $Uln = $_POST['last_name']; 
    $Uemail = $_POST['email']; 

    $NewAccountQuery = "INSERT INTO users (user_id,username, password, first_name, last_name, emial) VALUES ('$UserN','$Upass', '$Ufn', '$Uln', '$Uemail')"; 

    if(!mysql_query($NewAccountQuery)){ 

     die(mysql_error()); 

    }//end of nested if statment 


    $newrecord = "1 record added to the database"; 

}//end of if statment 

?> 



<html> 
<head> 

<title>Home Page</title> 
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" /> 
<link href="style.css" rel="stylesheet" type="text/css" /> 
</head> 
<body> 

    <div id="wrapper"> 
     <header><h1>E-Shop</h1></header> 


     <article> 
     <h1>Welcome</h1> 

      <h1>Create Account</h1> 

     <div id="login"> 

       <ul id="login"> 

       <form method="post" action="register.php" > 
        <fieldset> 
         <legend>Fill in the form</legend> 

         <label>Select Username : <input type="text" name="username" /></label> 
         <label>Password : <input type="password" name="password" /></label> 
         <label>Enter First Name : <input type="text" name="first_name" /></label> 
         <label>Enter Last Name : <input type="text" name="last_name" /></label> 
         <label>Enter E-mail Address: <input type="text" name="email" /></label> 
        </fieldset> 
         <br /> 

         <input type="submit" submit="submit" value="Create Account" class="button"> 

       </form> 




       </div> 
      <form action="index.php" method="post"> 
      <div id="login"> 
       <ul id="login"> 
        <li> 
         <input type="submit" value="Cancel" onclick="index.php" class="button"> 
        </li> 
       </ul> 
      </div>  



     </article> 
<aside> 
</aside> 

<div id="footer">This is my site i Made coppyrights 2013 Tomazi</div> 
</div> 

</body> 
</html> 

我也有一个包括文件,该文件是connectDB:

<?php 


    session_start(); 

     $con = mysql_connect("127.0.0.1", "root", ""); 
     if(!$con) 
      die('Could not connect: ' . mysql_error()); 

     mysql_select_db("eshop", $con) or die("Cannot select DB"); 

?> 

数据库结构:

数据库名称:eshop; DB中只有一个表:用户;

用户表包括:

user_id: A_I , PK 
username 
password 
first_name 
last_name 
email 

我花了大量时间来工作了这一点做了大量的研究,看了一些教程,但没有运气

谁能找出什么是根我的问题...?

+0

您正在使用[an **过时的**数据库API](http://stackoverflow.com/q/12859942/19068)并应使用[现代替换](http://php.net) /manual/en/mysqlinfo.api.choosing.php)。你也**易受[SQL注入攻击](http://bobby-tables.com/)**,现代的API会使[防御]更容易(http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php)自己从。 – Quentin 2013-02-27 13:05:53

+0

你确定没有错误吗?你的'INSERT'语句指定的列多于它的值。 – David 2013-02-27 13:06:24

+1

我敢打赌,你有错误关闭。 – L0j1k 2013-02-27 13:07:16

回答

2

这是因为if(isset($_POST['submitted'])){

你没有输入字段名submitted给提交按钮名称submitted

<input name="submitted" type="submit" submit="submit" value="Create Account" class="button">

检查插入查询你比你的价值更多的领域

更改:

$NewAccountQuery = "INSERT INTO users (user_id,username, password, first_name, last_name, email) VALUES ('$UserN','$Upass', '$Ufn', '$Uln', '$Uemail')";

到:

$NewAccountQuery = "INSERT INTO users (user_id,username, password, first_name, last_name, email) VALUES ('','$UserN','$Upass', '$Ufn', '$Uln', '$Uemail')";

考虑user_id是自动递增领域。

您的email在查询中被错误地写为emial

+0

仍然无法正常工作:/其驱使我疯狂嘿嘿 – Tomazi 2013-02-27 13:11:35

+0

尝试呼应里面的东西,如果条件 – 2013-02-27 13:13:35

+0

好吧,你刚刚排序了我很多thx。在修改了你建议的修改后,我也有一个错误:“好吧你让我走了,但现在我得到一个错误是”注意:会话已经开始 - 忽略C:\ xampp \ htdocs \ eshop中的session_start \ connectDB.php在第4行“”所以我也hadd删除session_start()fromy我的register.php文件 – Tomazi 2013-02-27 13:18:43

0

错误报告是否打开? 将这个在屏幕的顶部:

error_reporting(E_ALL); 
ini_set('display_errors', '1'); 
+0

这不是答案。这也可以在评论中告诉 – 2013-02-27 13:11:34

+0

我在这个问题下没有添加评论按钮。 – HarryFink 2013-02-27 13:15:20

+0

有“添加/显示更多评论”点击它你会得到评论框 – 2013-02-27 13:16:54

0

一些很好的答案上面,但我也建议你使用新的MySQLi/PDO,而不是过时的2002年的MySQL API的。

一些例子:(我会用mysqli的,因为你在程序代码中写道原来的例子)

connectDB.php

<?php 
$db = mysqli_connect('host', 'user', 'password', 'database'); 

if (mysqli_connect_errno()) 
    die(mysqli_connect_error()); 
?> 

寄存器。PHP的 - 我只是写出一个例子PHP的一部分,让你做休息

<?php 
//i'll always check if session was already started, if it was then 
//there is no need to start it again 
if (!isset($_SESSION)) { 
    session_start(); 
} 

//no need to include again if it was already included before 
include_once('connectDB.php'); 

//get all posted values 
$username = $_POST['username']; 
$userpass = $_POST['password']; 
$usermail = $_POST['usermail']; 
//and some more 

//run checks here for if fields are empty etc? 
//example check if username was empty 
if($username == NULL) { 
    echo 'No username entered, try <a href="register.php">again</a>'; 
    mysqli_close($db); 
    exit(); 
} else { 
    //if username field is filled we will insert values into $db 
    //build query 
    $sql_query_string = "INSERT INTO _tablename_(username,userpassword,useremail) VALUES('$username','$userpass','$usermail')"; 
    if(mysqli_query($db,$sql_query_string)) { 
    echo 'Record was entered into DB successfully'; 
    mysqli_close($db); 
    } else { 
    echo 'Ooops - something went wrong.'; 
    mysqli_close($db); 
    } 
} 
?> 

这应该工作很好地和所有你需要补充的是您的正确的价值观发布和建立的形式张贴,就这样。

0
<?php 
$db = mysqli_connect('host', 'user', 'password', 'database'); 

if (mysqli_connect_errno()) 
    die(mysqli_connect_error()); 
?> 
register.php -- i'll just write out an example php part and let you do the rest 

<?php 
//i'll always check if session was already started, if it was then 
//there is no need to start it again 
if (!isset($_SESSION)) { 
    session_start(); 
} 

//no need to include again if it was already included before 
include_once('connectDB.php'); 

//get all posted values 
$username = $_POST['username']; 
$userpass = $_POST['password']; 
$usermail = $_POST['usermail']; 
//and some more 

//run checks here for if fields are empty etc? 
//example check if username was empty 
if($username == NULL) { 
    echo 'No username entered, try <a href="register.php">again</a>'; 
    mysqli_close($db); 
    exit(); 
} else { 
    //if username field is filled we will insert values into $db 
    //build query 
    $sql_query_string = "INSERT INTO _tablename_(username,userpassword,useremail) VALUES('$username','$userpass','$usermail')"; 
    if(mysqli_query($db,$sql_query_string)) { 
    echo 'Record was entered into DB successfully'; 
    mysqli_close($db);`enter code here` 
    } else { 
    echo 'Ooops - something went wrong.'; 
    mysqli_close($db); 
    } 
} 
?> 
+1

欢迎来到SO!用一些总结或结论而不是评论来解释你在这里使用的想法可能是一个好主意,所以你可以用更加用户友好的方式来扩展它们。 – 2017-10-13 15:14:12