2013-02-27 104 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