我正面临一个问题插入电子邮件与连字符/破折号到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→逃脱(电子邮件)
mysql_real_escape_string(电子邮件)
我不知道我哪里错了。还是跟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();
我不得不重新工作你的代码相当多,但我运行这个,它工作得很好。首先是表格。您不需要提交表单和执行AJAX,只执行其中一个即可。因为简单总是更好,所以我选择提交表单。下面是为了测试而更改了控制器和方法的表单。控制器名称为email_test,方法为insert email。还要注意(非常重要)onclick中的Javascript调用有一个附加的"return"。这样,如果email字段中没有值,表单就不会提交。
<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
}
}