确认成功插入行时出现PHP错误

时间:2013-02-28 23:34:42

标签: php mysql

概述我正在创建一个用于学习目的的虚拟网站,因此其功能主义者是基本的和安全的,不在议程上。

我遇到了这个我无法解决的小问题。所以为什么我要做的是添加一个新帐户并回显一条消息,说插入是悬疑,但我在一个地方我得到一条错误信息,我想添加一个新的记录,说该变量,假设持有消息未定义。

还有一个:所以当我在wbesite的主页并点击一个注册按钮时,弹出错误信息但当我实际提交查询以添加一个错误消息更改为消息的帐户时想表现出来。

<?php



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

    include('connect_mysql.php');


    $username= $_POST['username'];
    $password = $_POST['password'];
    $firstname = $_POST['first_name'];
    $lastname = $_POST['last_name'];
    $email = $_POST['email'];

    $NewAccountQuery = "INSERT INTO users (username, password, first_name, last_name, email) VALUES ('$username','$password', '$firstname', '$lastname', '$email')";

    if(!mysql_query($NewAccountQuery)){

        die(mysql_error());

    }//end of nested if statment

    if($NewAccountQuery){
         echo $confirm = "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 name="submited" 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="back" onclick="index.php" class="button">   
                    </li>
                </ul>
            </div>      
            </form>


        </article>
<aside>

                <?php print $confirm; ?>


</aside>

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

</body>
</html>

这里是我提交查询之前的图片: enter image description here

提交查询后的图片: enter image description here

5 个答案:

答案 0 :(得分:1)

echo $confirm = "1 record added to the database";

应该是:

$confirm = "1 record added to the database";
echo $confirm;

看起来你不需要那里的回声,因为你在其他地方回应它。如果您只想将$confirm分配给1 record added to the database,那么您只需要分配它,而不需要回声。 Echo基本上以与打印相同的方式将其输出到html。

答案 1 :(得分:1)

我认为错误信息非常清楚。 $confirm未定义。您只能在“成功查询”部分中定义它。一个简单的解决方案是在整个if(isset($_POST['submited'])){块之前定义它。只需放$confirm = '';,就不会打印出任何内容。


一些注意事项:

由于你正在学习并且这是全新的,你应该停止立即使用ext / mysql和use PDO or mysqli作为你的数据库引擎(我更喜欢前者)。

此外,if($NewAccountQuery){不是很有意义。它始终是真的,因为它只是您之前定义的字符串。如果您切换到PDO,则可以查看PDOStatement::rowCount

答案 2 :(得分:1)

基本上问题是当你第一次加载主页时 - 它不会进入if(isset($_POST['submited']))状态,因为没有设置$ _POST ['submitted']。因为你在这个循环中设置了$ confirm,所以这个语句<?php print $confirm; ?>将找不到$ confirm变量集。因此它会发出PHP NOTICE ..所以就像有人说这里要么在条件之前设置$confirm = '';,要么你可以在打印时做这样的事情

<?php 
if(isset($confirm))
   print $confirm; 
?>

答案 3 :(得分:0)

如果您只是回显或打印字符串,则无需先将其分配给变量。这些都可以起作用:

echo '1 record added to the database';

OR

$confirm = '1 record added to the database';
echo $confirm;

答案 4 :(得分:0)

<?php
if(isset($_POST['submited'])){

    include('connect_mysql.php');

    $username= $_POST['username'];
    $password = $_POST['password'];
    $firstname = $_POST['first_name'];
    $lastname = $_POST['last_name'];
    $email = $_POST['email'];

    $NewAccountQuery = "INSERT INTO users VALUES ('$username','$password', '$firstname', '$lastname', '$email')"; // simplified query if those are only 5 cols in order
    $result = mysql_query($NewAccountQuery);

    if($result){
       $confirm = "1 record added to the database";
    } else {
        exit('error inserting row');
    }
}
?>