I'm facing an issue inserting emails with hyphens/dashes into mysql. The framework used is CodeIgniter. The project is hosted in GoDaddy (if this helps). Emails that work are:
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
Email that don't work is, but they work fine in localhost
:
first-last@domain.com, first-last@test.domain.com
Here's the form from which the email is inserted:
<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>
The autogen()
function called from '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();
var code = $('#code');
if (!email) {
alert("Email is required!");
$('#email').focus();
} else {
code.val(randomstring);
alert("Email: "+email+"
Code: "+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);
}
});
}
}
And finally the insert script from 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;
}
}
Things I have tried with no luck:
$this->db->escape($email)
mysql_real_escape_string($email)
I don't know where I'm going wrong. Or Is it something to do with GoDaddy?
I assume you are using PDO. So this example is for 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();
See http://php.net/manual/en/pdo.prepared-statements.php for more details.
I had to rework your code quite a bit but I ran this and it worked fine. First the Form. You don't need to submit the form and do AJAX, do one or the other. Since simple is always better, I chose to submit the form. Here is the form with a changed controller and method for test purposes. The controller is named email_test and the method is insert email. Also notice (very important) the Javascript call in onclick has an added "return." This is so the form will not submit without a value in the email field.
<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>
Next is the Javascript. I reduced this to just checking for an email and generating the code. Also notice the comments.
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 + "
Code: " + randomstring);
/*
* form will submit
*/
return true;
}
}
Last is the controller and model. For brevity, I skipped the model but you should not do this. Check the comments on this.
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
}
}