我遇到了将带有连字符/破折号的电子邮件插入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;
}
}
我尝试过没有运气的事情:
$这 - &GT; DB-&GT;逃逸($电子邮件)
mysql_real_escape_string($电子邮件)
我不知道我哪里出错了。或者它与GoDaddy有关吗?
答案 0 :(得分:0)
我假设你正在使用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/en/pdo.prepared-statements.php。
答案 1 :(得分:0)
我不得不重新修改你的代码但是我运行它并且它工作正常。首先是表格。您不需要提交表单和执行AJAX,执行其中一个操作。由于简单总是更好,我选择提交表格。这是带有更改的控制器和方法的表单,用于测试目的。控制器名为email_test,方法是插入电子邮件。另请注意(非常重要)onclick中的Javascript调用添加了&#34;返回。&#34;这样,如果没有电子邮件字段中的值,表单将不会提交。
<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
}
}