0

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

代码:

注册.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

我花了很多时间来解决这个问题并进行了研究并查看了一些教程,但没有运气

谁能发现我的问题的根源是什么......?

4

4 回答 4

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的 in 查询被错误地写为emial.

于 2013-02-27T13:06:37.817 回答
0

错误报告是否打开?将其放在屏幕顶部:

error_reporting(E_ALL);
ini_set('display_errors', '1');
于 2013-02-27T13:09:57.137 回答
0

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

一些示例:(我将使用 mysqli,因为您使用程序代码编写了原始示例)

连接数据库.php

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

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

register.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);
  }
}
?>

这应该可以很好地工作,您需要添加的是正确的发布值并构建表单以发布它,仅此而已。

于 2013-02-27T13:55:53.997 回答
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);
  }
}
?>
于 2017-10-13T15:08:46.407 回答