簡體   English   中英

將數據插入mysql數據庫(php,mysql)

[英]Inserting data in to mysql database (php, mysql)

嗨,我正在使用 php 和 mysql 建立一個在線乳制品。 我嘗試注冊以便可以將用戶信息存儲到數據庫中,但我收到以下錯誤消息:“注意:未定義的變量:第 85 行 C:\wamp64\www\dairy\index.php 中的錯誤”我不知道如何解決這個問題。 請我需要幫助...

這是代碼:

<?php 

if (array_key_exists("submit", $_POST)) {

    $connection = mysqli_connect("localhost","root","","dairy_database");

    if(mysqli_connect_error()) {

    die ("There was an error connection to the database");
    }

    $error = "";

    if (!$_POST['email']) {

        $error .= "An email address is required <br>";
    }

    if (!$_POST['password']) {

        $error .= "A password is required <br>";
    }

    if ($error != "") {

        $error = "<p> There were error(s) in your form:</p>".$error;
    }
    else{

        $query = "SELECT Id FROM users WHERE email = '".mysqli_real_escape_string($connection, $_POST['email'])."' LIMIT 1";
        $result = mysqli_query($connection, $query);

        if (mysqli_num_rows($result) > 0) {

            $error = "That email address is already taken.";
        }
        else{

            $query = "INSERT INTO users ('email', 'password') VALUES ('".mysqli_real_escape_string($connection, $_POST['email'])."', '".mysqli_real_escape_string($connection, $_POST['password'])."')";

                    if (mysqli_query($connection, $query)) {

                        $error = "<p>Could not sign you up, Please try again later.</p>";
                    }
                    else{

                        $query = "UPDATE users SET password = '".md5(md5(mysqli_insert_id($connection)).$_POST['password'])."' WHERE Id = ".mysqli_insert_id($connection)." LIMIT 1";

                        mysqli_query($connection, $query);

                        echo "Sign up successful";
                    }

        }
    }
}

?>





<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width">
        <meta name="author" content="Bachir Amadou">
        <link href="https://fonts.googleapis.com/css?family=Abel|Comfortaa" rel="stylesheet">
        <link rel="stylesheet" href="../carousel/css/style.css" type="text/css">
        <link rel="stylesheet" href="coverflow/css/style.css">
        <title>Dairy</title>
        <script src="https://code.jquery.com/jquery-3.2.1.min.js"> </script>
        <script type="text/javascript" src=""></script>
    </head>
    <body>
        <div id="wrapper">
            <div id="top">
                <img class="log-img" src="./img/notebook.png" alt="notebook">
                <h5>Online Dairy Management System</h5>
                <h1>Web Base System</h1>
            </div><!--END OF TOP-->

            <div id="error"> <?php echo $error; ?></div>

            <div id="signup">
                <form id="form-signup" method="post">

                    <input type="email" class="user" name="email" placeholder="Email..." 
                    required><br>

                    <input type="password" class="user" name="password" placeholder="Password" required><br>


                    <input type="submit"  name="submit" class="sign-submit" value="Sign Up">

                </form> 
            </div><!--END OF LOG-->

        </div><!--END OF WRAPPER-->
    </body>
  </html>

這是因為變量$error是在條件中聲明的。 您可以使用isset()函數來檢查變量是否設置。 一種替代解決方案是。

將此行更改為

<div id="error"> <?php echo $error; ?></div>

這個。

<?php if(isset($error)){ ?> <div id="error"> <?php  echo $error;  ?></div><?php } ?>

或者

在條件之前聲明變量,例如

    <?php
        $error = '';
        if (array_key_exists("submit", $_POST)) { 


         ....STATEMENTS..... 

       }
    ?>

<div id="error"> <?php echo $error; ?></div>

您在插入時的條件語句中也有一些錯誤。 我已經更新了下面的查詢。

PHP

<?php 

$error = ""; // declare it here
if (array_key_exists("submit", $_POST)) {

    $connection = mysqli_connect("localhost","root","","dairy_database");

    if(mysqli_connect_error()) {

    die ("There was an error connection to the database");
    }

    if (!$_POST['email']) {

        $error .= "An email address is required <br>";
    }

    if (!$_POST['password']) {

        $error .= "A password is required <br>";
    }

    if ($error != "") {

        $error = "<p> There were error(s) in your form:</p>".$error;
    }
    else{

        $query = "SELECT Id FROM users WHERE email = '".mysqli_real_escape_string($connection, $_POST['email'])."' LIMIT 1";
        $result = mysqli_query($connection, $query);

        if (mysqli_num_rows($result) > 0) {

            $error = "That email address is already taken.";
        }
        else{

            $query = "INSERT INTO `users` (`email`, `password`) VALUES ('".mysqli_real_escape_string($connection, $_POST['email'])."', '".md5(md5(mysqli_real_escape_string($connection, $_POST['password'])))."');";

                    if (!mysqli_query($connection, $query)) {

                        $error = "<p>Could not sign you up, Please try again later.</p>";
                    }
                    else{

                        $query = "UPDATE  `users` SET `password` = '".md5(md5(mysqli_insert_id($connection)).$_POST['password'])."' WHERE Id = ".mysqli_insert_id($connection)." LIMIT 1;";

                        mysqli_query($connection, $query);

                        echo "Sign up successful";
                    }

        }
    }
}

?>

HTML

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width">
        <meta name="author" content="Bachir Amadou">
        <link href="https://fonts.googleapis.com/css?family=Abel|Comfortaa" rel="stylesheet">
        <link rel="stylesheet" href="../carousel/css/style.css" type="text/css">
        <link rel="stylesheet" href="coverflow/css/style.css">
        <title>Dairy</title>
        <script src="https://code.jquery.com/jquery-3.2.1.min.js"> </script>
        <script type="text/javascript" src=""></script>
    </head>
    <body>
        <div id="wrapper">
            <div id="top">
                <img class="log-img" src="./img/notebook.png" alt="notebook">
                <h5>Online Dairy Management System</h5>
                <h1>Web Base System</h1>
            </div><!--END OF TOP-->

            <div id="error"> <?php echo $error; ?></div>

            <div id="signup">
                <form id="form-signup" method="post">
                <input type="email" class="user" name="email" placeholder="Email..." 
                required><br>

                <input type="password" class="user" name="password" placeholder="Password" required><br>


                <input type="submit"  name="submit" class="sign-submit" value="Sign Up">

            </form> 
        </div><!--END OF LOG-->

    </div><!--END OF WRAPPER-->
</body>

您需要在 if 條件之前聲明錯誤變量。 像這樣:

<?php 

$error = ""; // declare it here

if (array_key_exists("submit", $_POST)) {

    $connection = mysqli_connect("localhost","root","","dairy_database");

    if(mysqli_connect_error()) {

    die ("There was an error connection to the database");
    }

    if (!$_POST['email']) {

        $error .= "An email address is required <br>";
    }

    if (!$_POST['password']) {

        $error .= "A password is required <br>";
    }

    if ($error != "") {

        $error = "<p> There were error(s) in your form:</p>".$error;
    }
    else{

        $query = "SELECT Id FROM users WHERE email = '".mysqli_real_escape_string($connection, $_POST['email'])."' LIMIT 1";
        $result = mysqli_query($connection, $query);

        if (mysqli_num_rows($result) > 0) {

            $error = "That email address is already taken.";
        }
        else{

            $query = "INSERT INTO users ('email', 'password') VALUES ('".mysqli_real_escape_string($connection, $_POST['email'])."', '".mysqli_real_escape_string($connection, $_POST['password'])."')";

                    if (mysqli_query($connection, $query)) {

                        $error = "<p>Could not sign you up, Please try again later.</p>";
                    }
                    else{

                        $query = "UPDATE users SET password = '".md5(md5(mysqli_insert_id($connection)).$_POST['password'])."' WHERE Id = ".mysqli_insert_id($connection)." LIMIT 1";

                        mysqli_query($connection, $query);

                        echo "Sign up successful";
                    }

        }
    }
}

?>





<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width">
        <meta name="author" content="Bachir Amadou">
        <link href="https://fonts.googleapis.com/css?family=Abel|Comfortaa" rel="stylesheet">
        <link rel="stylesheet" href="../carousel/css/style.css" type="text/css">
        <link rel="stylesheet" href="coverflow/css/style.css">
        <title>Dairy</title>
        <script src="https://code.jquery.com/jquery-3.2.1.min.js"> </script>
        <script type="text/javascript" src=""></script>
    </head>
    <body>
        <div id="wrapper">
            <div id="top">
                <img class="log-img" src="./img/notebook.png" alt="notebook">
                <h5>Online Dairy Management System</h5>
                <h1>Web Base System</h1>
            </div><!--END OF TOP-->

            <div id="error"> <?php echo $error; ?></div>

            <div id="signup">
                <form id="form-signup" method="post">

                    <input type="email" class="user" name="email" placeholder="Email..." 
                    required><br>

                    <input type="password" class="user" name="password" placeholder="Password" required><br>


                    <input type="submit"  name="submit" class="sign-submit" value="Sign Up">

                </form> 
            </div><!--END OF LOG-->

        </div><!--END OF WRAPPER-->
    </body>
  </html>

你也應該使用准備好的語句而不是 mysqlihttps://www.w3schools.com/php/php_mysql_prepared_statements.asp

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM