doujia4759 2015-07-29 15:37
浏览 47

如何从CodeIgniter中插入mysql中包含连字符/破折号的电子邮件?

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?

  • 写回答

2条回答 默认 最新

  • douliu1092 2015-07-29 15:52
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教