(または行う必要があります)次のスクリプトが何をするか:それが有効かどう PHP/MySQLは挿入してエコーしませんか?
- が
- は5桁のコードを作成するための機能を備えてDBに接続し
- ユーザーは、自分のメールアドレス
- をチェック入りメール
- 挿入[メール]欄にメール
- チェックした電子メールがすでに存在する場合はそうならば、ユーザーが知っていると壊れるスクリプト
- が既に存在する場合はそうである場合、すべてが有効である5桁のコード作成機能からループ
- 場合、、、フォームを非表示(表示チェック
- 「UNIQUE_CODE」カラムを5桁のコードを作成するための機能を実行別のJSからアヤックス)あなたが
- は、すべてが実行さ
が、しかしUNIQUE_CODEは、DBに挿入されていないと、それは「ありがとうないときに表示されていないユーザーに固有のコードを表示しDIV感謝します! 」。
何を私が間違っているのと何を変更する必要がある?
ありがとう!
コード
<?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__を使用しようとしていますか? –
@マーク・ベイカー:それは私のindex.phpのための私の完全なコードです。それをクラス内で使用する例? – jeremycollins
@マーク・ベイカー:プライベートを使用せずにコードを更新し、機能しないようにしてください。 – jeremycollins