[英]How to insert emails containing hyphen/dash in mysql from CodeIgniter?
我在將帶有連字符/破折號的電子郵件插入mysql時遇到問題。 使用的框架是CodeIgniter 。 該項目托管在GoDaddy中 (如果有幫助)。 起作用的電子郵件是:
name @ domain.com,name @ test.domain.com,first.last @ domain.com,first.last @ test.domain.com,name.123 @ domain.com,first_last @ domain.com,first_last @ test。 domain.com
無效的電子郵件是,但是在localhost
可以正常工作:
first-last @ domain.com,first-last @ test.domain.com
這是插入電子郵件的表格:
<form method="post" action="<?php echo base_url('index.php?/codes');?>">
<div class="form-group">
<label>Email</label>
<input type="text" name="email" id="email" placeholder="Email" class="form-control">
</div>
<div class="form-group">
<label>Waiver Code</label>
<input type="text" name="code" id="code" placeholder="Code" class="form-control">
</div>
<button class="btn btn-sm btn-success" onclick="autogen()" name="saveCode">Generate</button>
</form>
從'javascript'調用的autogen()
函數:
function autogen() {
var randomstring = Math.random().toString(36).slice(-6);
var date = new Date().getFullYear();
randomstring = date+randomstring;
randomstring = randomstring.toUpperCase();
var email = $('#email').val();
var code = $('#code');
if (!email) {
alert("Email is required!");
$('#email').focus();
} else {
code.val(randomstring);
alert("Email: "+email+"\nCode: "+randomstring);
$.ajax({
url: "<?php echo base_url('index.php?/genCode/"+email+"/"+randomstring+"');?>",
data: ({'email': email, 'code': randomstring}),
type: "post",
success: function(response, textStatus, jqXHR){
location.reload();
alert('Code added');
},
error: function(jqXHR, textStatus, errorThrown){
console.log("The following error occured: "+
textStatus, errorThrown);
}
});
}
}
最后是來自CodeIgniter的插入腳本
class GenCode extends CI_Controller {
public function index($email="", $code="")
{
//$data = array('email' => $this->db->escape($email), 'code' => $code, 'user' => $this->session->userdata('username'));
//$query = $this->db->insert('codes', $data);
$query = $this->db->query("insert ignore into codes(email, code, user) values('".$this->db->escape($email)."', '".$code."', '".$this->session->userdata('username')."');");
if ($query == TRUE)
return TRUE;
else
return FALSE;
}
}
我沒有運氣嘗試過的事情:
$ this-> db-> escape($ email)
mysql_real_escape_string($ email)
我不知道我要去哪里錯了。 還是與GoDaddy有關?
我假設您正在使用PDO。 因此,此示例適用於PDO:
$stmt=$this->db->prepare("insert ignore into codes(email, code, user) ".
" values(:email, :code, :user)");
$stmt->bindParam(":email",$email);
$stmt->bindParam(":code",$code);
$stmt->bindParam(":user",$user);
$stmt->execute();
有關更多詳細信息,請參見http://php.net/manual/zh/pdo.prepared-statements.php 。
我不得不對您的代碼進行很多修改,但是我運行了它,並且效果很好。 首先是表格。 您無需提交表單並進行AJAX,也不需要進行另一項操作。 因為簡單總是更好,所以我選擇提交表單。 這是用於測試目的的控制器和方法已更改的表格。 控制器名為email_test,方法為插入電子郵件。 還要注意(非常重要)onclick中的Javascript調用添加了“返回”。 這樣一來,在電子郵件字段中沒有任何值的情況下,表單將無法提交。
<form method="post" action="email_test/insert_email">
<div class="form-group">
<label>Email</label>
<input type="text" name="email" id="email" placeholder="Email" class="form-control">
</div>
<div class="form-group">
<label>Waiver Code</label>
<input type="text" name="code" id="code" placeholder="Code" class="form-control">
</div>
<button class="btn btn-sm btn-success" onclick="return autogen();" name="saveCode">Generate</button>
</form>
接下來是Javascript。 我將其簡化為僅檢查電子郵件並生成代碼。 另請注意評論。
function autogen() {
var randomstring = Math.random().toString(36).slice(-6);
var date = new Date().getFullYear();
randomstring = date + randomstring;
randomstring = randomstring.toUpperCase();
var email = $('#email').val();
/*
* This checks for a value, but it does not mean its an email.
* You need a valid email check here too.
*/
if (!email) {
alert("Email is required!");
$('#email').focus();
/*
* form will not submit
*/
return false;
} else {
$('#code').val(randomstring);
console.log("Email: " + email + "\nCode: " + randomstring);
/*
* form will submit
*/
return true;
}
}
最后是控制器和模型。 為簡便起見,我跳過了該模型,但您不應這樣做。 檢查對此的評論。
class Email_test extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function index() {
}
public function insert_email() {
/*
* TODO here...
* check for ajax
* check for being sent from yourself
* clean your input
* return error and handle if bad input
*/
// load your model here
// call your model here which has the following code
//all this code should be in a model, put here for an example.
$query = $this->db->query("insert into codes(email, code, user) values(" . $this->db->escape($email) . ", '$code.', '" . $this->session->userdata('username') . "');");
if ($this->db->affected_rows() > 0) {
return TRUE;
} else {
return FALSE;
}
//handle the return from the model here
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.