簡體   English   中英

PHP / MySQL不會INSERT和回應它應該嗎?

[英]PHP/MySQL won't INSERT and echo as it should?

以下腳本的作用(或應該如何):

  • 連接到DB
  • 包括用於創建5位數代碼的功能
  • 用戶輸入其電子郵件地址
  • 檢查它是否是有效的電子郵件
  • 將電子郵件插入“電子郵件”列
  • 檢查電子郵件是否已存在,如果是,請讓用戶知道並破壞腳本
  • 運行用於創建5位數代碼的功能
  • 檢查'unique_code'列是否已存在,如果已存在,則從5位數代碼創建函數循環
  • 如果一切都有效,隱藏表格並顯示(來自單獨的JS的ajax)謝謝div
  • 向用戶顯示唯一代碼

一切都在運行,但是unique_code沒有插入到DB中,並且在“謝謝!”時不會顯示。

我做錯了什么,需要修改什么?

謝謝!

    <?php

    require "includes/connect.php";

    function generateCode($length = 5) {

    $characters = 'bcdfghjkmnpqrstvwxyz';

    $string = '';
    for ($i = 0; $i < $length; $i++) {
        $string .= $characters[rand(0, strlen($characters) - 1)];
    }

    return $string;

}


$msg = '';

if($_POST['email']){

    // Requested with AJAX:
    $ajax = ($_SERVER['HTTP_X_REQUESTED_WITH']  == 'XMLHttpRequest');

    try{
        //validate email
        if(!filter_input(INPUT_POST,'email',FILTER_VALIDATE_EMAIL)){
            throw new Exception('Invalid Email!');
        }

        //insert email
        $mysqli->query("INSERT INTO coming_soon_emails
                        SET email='".$mysqli->real_escape_string($_POST['email'])."'");

        //if already exists in email column
        if($mysqli->affected_rows != 1){
            throw new Exception('You are already on the notification list.');
        }

        if($ajax){
            die('{"status":1}');
        }

        //start creating unique 5 digit code
        $unique_code = "";
        $inserted = false;

        // Keep looping until we've inserted a record
        while(!$inserted) {

        // Generate a code
        $unique_code = generateCode();

        // Check if it exists
        if ($result = $mysqli->query("SELECT unique_code FROM coming_soon_emails WHERE unique_code = '$unique_code'")) {

        // Check no record exists
        if ($result->num_rows == 0) {

            // Create new record
            $mysqli->query("INSERT INTO coming_soon_emails (email,unique_code) VALUES ('" . $mysqli->real_escape_string($_POST['email']) . "','$unique_code')");

            // Set inserted to true to ext loop
            $inserted = true;

            // Close the result object
            $result->close();

        }
        } else {

        // Quit if we can't check the database
        die('Something went wrong with select');
    }   
}

    }

    catch (Exception $e){

        if($ajax){
            die(json_encode(array('error'=>$e->getMessage())));
        }

        $msg = $e->getMessage();        
    }
}
?>


<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>example</title>

<link rel="stylesheet" type="text/css" href="css/styles.css" />

</head>

<body>

<div id="container">

    <form id="form" method="post" action="">
        <input type="text" id="email" name="email" value="<?php echo $msg?>" />
        <input type="submit" value="Submit" id="submitButton" />
    </form>

    <div id="thankyou">
    Thank you! <?php echo $unique_code;?></p>
    </div>


</div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
<script src="js/script.js"></script>
</body>
</html>

您似乎在類定義之外使用private關鍵字,這是不允許的。

你在coming_soon_emails表中的'email'上有一個主鍵嗎? 由於您已經為給定的電子郵件地址插入了一個條目,因此可以防止您插入具有唯一值的第二個條目。

一旦確定了唯一密鑰,為什么不進行UPDATE而不是INSERT?

暫無
暫無

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

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