为什么插入数据库时​​不带 http:// 而不带 http:// 插入

发布于 2024-12-11 03:45:32 字数 3791 浏览 0 评论 0原文

我需要插入 site_title、site_address(url)site_description 和 site_description site_category 从前端使用 jquery/json 进入我的数据库。如果我不将“http://”放入 site_address 输入字段,它会插入站点并更新前端,但只要我这样做,它就会没有插入数据库..

我有这个代码

FORM IN HOME VIEW:

<form action="" method="post" class="addsite_form">
  <input id="account_id" type="hidden" value="<?php echo $this->session->userdata('account_id');?>"/>
  <label for="site_title"><strong>Site Name:</strong></label> 
  <input id="site_title" type="text" maxLength="25" tabindex="1" name="site_title" /> 
  <label for="site_address"><strong>Site Address:</strong></label> 
  <input id="site_address" type="text" tabindex="2" name="site_address" /> 
  <label for="site_description"><strong>Site Description:</strong></label> 
  <input id="site_description" type="text" tabindex="3" name="site_description" /> 
  <label for="site_category"><strong>Site Category:</strong></label> 
  <input id="site_category" type="text" tabindex="4" name="site_category" /> 

  <input id="addsite_form_submit" type="submit" value="Add Site" name="submit" tabindex="5" /> 

</form>

CONTROLLER

public function addUserSites() {

  $account_id = $this->input->post('account_id');
  $site_title = $this->input->post('site_title');
  $site_description = $this->input->post('site_description');
  $site_address = $this->input->post('site_address');
  $site_category = $this->input->post('site_category');

  $array = array(
    'account_id' => $account_id,
    'site_title' => $site_title,
    'site_description' => $site_description,
    'site_address' => $site_address,
    'site_category' => $site_category,

  );

  echo json_encode($this->usersites_model->insertsites($array));

}

MODEL:

public function insertsites($data)  {

  $this->db->insert('usersites', $data);

  return TRUE; 

}

JQUERY

$('.addsite_form').submit(function() {
  var account_id = $("#account_id").val();
  var site_title = $("#site_title").val();
  var site_address = $("#site_address").val();
  var site_description = $("#site_description").val();
  var site_category = $("#site_category").val();

  $.post("/usersites/addusersites", { 

    "account_id" : account_id,
    "site_title" : site_title, 
    "site_address" : site_address, 
    "site_description" : site_description, 
    "site_category" : site_category, 
  },

  function(data){
  }, "json");
});

编辑 - 解决方案

首先,让我对建议encodeURIComponent的Marshall Brekka表示衷心的感谢。我现在已经阅读了它并且找到了 php 版本..

我已将 urlencode 添加到我的控制器中,它现在可以工作了..这是新控制器

    public function addUserSites() {

    $account_id = $this->input->post('account_id');
    $site_title = $this->input->post('site_title');
    $site_description = $this->input->post('site_description');
    $site_address = $this->input->post(urlencode('site_address'));//added urlencode
    $site_category = $this->input->post('site_category');

    $array = array(
        'account_id' => $account_id,
        'site_title' => $site_title,
        'site_description' => $site_description,
        'site_address' => $site_address,
        'site_category' => $site_category,

    );

  echo json_encode($this->usersites_model->insertsites($array));

}

我还可以说,我认为那些对我的问题投了反对票的人,无论他们在堆栈中的地位有多高溢出社区需要多出去走走!我是一个菜鸟,我还在学习,我负担不起去大学学习这个,我有时需要帮助..你从来没有在那个阶段吗?噗!

I need to insert a site_title, site_address (the url) site_description & site_category into my database from the front-end with jquery/json.. It inserts the site and updates the frontend fine if I do not put the "http://" in the site_address input field but as soon as I do, it will not inster to the database..

I have this code

FORM IN HOME VIEW:

<form action="" method="post" class="addsite_form">
  <input id="account_id" type="hidden" value="<?php echo $this->session->userdata('account_id');?>"/>
  <label for="site_title"><strong>Site Name:</strong></label> 
  <input id="site_title" type="text" maxLength="25" tabindex="1" name="site_title" /> 
  <label for="site_address"><strong>Site Address:</strong></label> 
  <input id="site_address" type="text" tabindex="2" name="site_address" /> 
  <label for="site_description"><strong>Site Description:</strong></label> 
  <input id="site_description" type="text" tabindex="3" name="site_description" /> 
  <label for="site_category"><strong>Site Category:</strong></label> 
  <input id="site_category" type="text" tabindex="4" name="site_category" /> 

  <input id="addsite_form_submit" type="submit" value="Add Site" name="submit" tabindex="5" /> 

</form>

CONTROLLER

public function addUserSites() {

  $account_id = $this->input->post('account_id');
  $site_title = $this->input->post('site_title');
  $site_description = $this->input->post('site_description');
  $site_address = $this->input->post('site_address');
  $site_category = $this->input->post('site_category');

  $array = array(
    'account_id' => $account_id,
    'site_title' => $site_title,
    'site_description' => $site_description,
    'site_address' => $site_address,
    'site_category' => $site_category,

  );

  echo json_encode($this->usersites_model->insertsites($array));

}

MODEL:

public function insertsites($data)  {

  $this->db->insert('usersites', $data);

  return TRUE; 

}

JQUERY

$('.addsite_form').submit(function() {
  var account_id = $("#account_id").val();
  var site_title = $("#site_title").val();
  var site_address = $("#site_address").val();
  var site_description = $("#site_description").val();
  var site_category = $("#site_category").val();

  $.post("/usersites/addusersites", { 

    "account_id" : account_id,
    "site_title" : site_title, 
    "site_address" : site_address, 
    "site_description" : site_description, 
    "site_category" : site_category, 
  },

  function(data){
  }, "json");
});

EDIT - SOLUTION

Firstly, let me say a big thank you to Marshall Brekka who suggested the encodeURIComponent.. I have now read up on it and found the php version..

I have added urlencode to my controller and it now works.. here is the new controller

    public function addUserSites() {

    $account_id = $this->input->post('account_id');
    $site_title = $this->input->post('site_title');
    $site_description = $this->input->post('site_description');
    $site_address = $this->input->post(urlencode('site_address'));//added urlencode
    $site_category = $this->input->post('site_category');

    $array = array(
        'account_id' => $account_id,
        'site_title' => $site_title,
        'site_description' => $site_description,
        'site_address' => $site_address,
        'site_category' => $site_category,

    );

  echo json_encode($this->usersites_model->insertsites($array));

}

May I also say that I think that the people who have down voted my questions, no matter how high they stand in the stack overflow community need to get out more! I am a noob, I am still learning, I cannot afford to go to uni to learn this, I sometimes need help.. Were you never at that stage? Pffft!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

久光 2024-12-18 03:45:32

这可能是因为您没有对您的值进行 url 编码,请尝试此操作。

$.post("/usersites/addusersites", { 

    "account_id" : account_id,
    "site_title" : site_title, 
    "site_address" : encodeURIComponent(site_address), 
    "site_description" : site_description, 
    "site_category" : site_category, 
},

It could be because you have not url encoded your values, try this.

$.post("/usersites/addusersites", { 

    "account_id" : account_id,
    "site_title" : site_title, 
    "site_address" : encodeURIComponent(site_address), 
    "site_description" : site_description, 
    "site_category" : site_category, 
},
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文