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?