简体   繁体   English

使用CodeIgniter进行SQL查询时遇到问题

[英]Trouble with SQL Query Using CodeIgniter

Answer 回答

* $this->db->set('page_routes','pageid', $newid);* * $ this-> db-> set('page_routes','pageid',$ newid); *

Should have been 本来应该

$this->db->update('page_routes', array('pageid' => $newid)); $ this-> db-> update('page_routes',array('pageid'=> $ newid));

I am trying to create a page where, you can submit a form and it update values based on what you select, I cannot see what I am doing wrong but at some point, I think I am erring with regards to the MySQL command. 我正在尝试创建一个页面,您可以在该页面中提交表单,并根据您选择的内容更新值,我看不到我做错了什么,但是在某些时候,我认为我对MySQL命令有误。

The idea is you select a page(friendly name) on the top option-select, and on the bottom you set the ID to the value of another pageID. 这个想法是您在顶部选项中选择一个页面(友好名称),然后在底部将ID设置为另一个pageID的值。

The effect it should be achieving is that the user can change change what content is displayed on a particular page. 它应该实现的效果是用户可以更改更改特定页面上显示的内容。

Question: According to the query return, the update runs, but nothing is changed in my page_routes table 问题:根据查询返回,更新运行,但是我的page_routes表中没有任何更改

My View, the values are pulled from the database, they populate correctly. 我的视图,这些值是从数据库中提取的,它们正确填充。

<form class="form-horizontal" action="<?php echo site_url('/view/updatepageid'); ?>" method="post">
<fieldset>

<!-- Form Name -->
<legend>Form Name</legend>

<!-- Select Basic -->
<div class="control-group">
  <label class="control-label" for="friendly_name">Page</label>
  <div class="controls">
            <select id="friendlyname" name="friendlyname" class="input-xlarge">
        <?php foreach($pageroutes as $key): ?>
            <option value="<?php echo $key['friendly_name']; ?>"><?php echo $key['friendly_name']; ?></option>
        <?php endforeach; ?>
    </select>
  </div>
</div>

<!-- Select Basic -->
<div class="control-group">
  <label class="control-label" for="selectbasic">Change to:</label>
  <div class="controls">
        <select  onchange="this.form.submit();" id="newid" name="newid">
        <?php foreach($page_data as $key): ?>
            <option value="<?php echo $key['id']; ?>"><?php echo $key['pagetitle']; ?></option>
        <?php endforeach; ?>
        </select>
  </div>
</div>

</fieldset>
</form>

Which submits to here 哪个提交到这里

function updatepageid(){
    $this->load->model('view_model');
    $friendlyname = $this->input->post('friendlyname');
    $newid = $this->input->post('newid');


    $query = $this->view_model->setpageid($friendlyname, $newid);

    if($query == TRUE){
        echo 'updated';
    }else { echo 'failed'; }

    var_dump($query);


}

and my Model view_model setpageid function 和我的模型view_model setpageid函数

function setpageid($friendlyname, $newid){
    $this->db->where('friendly_name', $friendlyname);
    $query = $this->db->set('page_routes','pageid', $newid);
    return $query;
}

If I var_dump($query) it returns 如果我var_dump($ query)它返回

Object(CI_DB_mysql_driver)[14]
  public 'dbdriver' => string 'mysql' (length=5)
  public '_escape_char' => string '`' (length=1)
  public '_like_escape_str' => string '' (length=0)
  public '_like_escape_chr' => string '' (length=0)
  public 'delete_hack' => boolean true
  public '_count_string' => string 'SELECT COUNT(*) AS ' (length=19)
  public '_random_keyword' => string ' RAND()' (length=7)
  public 'use_set_names' => boolean false
  public 'ar_select' => 
    array (size=0)
      empty
  public 'ar_distinct' => boolean false
  public 'ar_from' => 
    array (size=0)
      empty
  public 'ar_join' => 
    array (size=0)
      empty
  public 'ar_where' => 
    array (size=1)
      0 => string '`friendly_name` =  'aboutus'' (length=28)
  public 'ar_like' => 
    array (size=0)
      empty
  public 'ar_groupby' => 
    array (size=0)
      empty
  public 'ar_having' => 
    array (size=0)
      empty
  public 'ar_keys' => 
    array (size=0)
      empty
  public 'ar_limit' => boolean false
  public 'ar_offset' => boolean false
  public 'ar_order' => boolean false
  public 'ar_orderby' => 
    array (size=0)
      empty
  public 'ar_set' => 
    array (size=1)
      '`page_routes`' => string ''pageid'' (length=8)
  public 'ar_wherein' => 
    array (size=0)
      empty
  public 'ar_aliased_tables' => 
    array (size=0)
      empty
  public 'ar_store_array' => 
    array (size=0)
      empty
  public 'ar_caching' => boolean false
  public 'ar_cache_exists' => 
    array (size=0)
      empty
  public 'ar_cache_select' => 
    array (size=0)
      empty
  public 'ar_cache_from' => 
    array (size=0)
      empty
  public 'ar_cache_join' => 
    array (size=0)
      empty
  public 'ar_cache_where' => 
    array (size=0)
      empty
  public 'ar_cache_like' => 
    array (size=0)
      empty
  public 'ar_cache_groupby' => 
    array (size=0)
      empty
  public 'ar_cache_having' => 
    array (size=0)
      empty
  public 'ar_cache_orderby' => 
    array (size=0)
      empty
  public 'ar_cache_set' => 
    array (size=0)
      empty
  public 'ar_no_escape' => 
    array (size=0)
      empty
  public 'ar_cache_no_escape' => 
    array (size=0)
      empty
  public 'username' => string 'root' (length=4)
  public 'password' => string '' (length=0)
  public 'hostname' => string 'localhost' (length=9)
  public 'database' => string 'cms' (length=3)
  public 'dbprefix' => string '' (length=0)
  public 'char_set' => string 'utf8' (length=4)
  public 'dbcollat' => string 'utf8_general_ci' (length=15)
  public 'autoinit' => boolean true
  public 'swap_pre' => string '' (length=0)
  public 'port' => string '' (length=0)
  public 'pconnect' => boolean true
  public 'conn_id' => resource(50, mysql link persistent)
  public 'result_id' => resource(57, mysql result)
  public 'db_debug' => boolean true
  public 'benchmark' => float 0.00099992752075195
  public 'query_count' => int 1
  public 'bind_marker' => string '?' (length=1)
  public 'save_queries' => boolean true
  public 'queries' => 
    array (size=1)
      0 => string 'SELECT *
FROM (`sessions`)
WHERE `session_id` =  '93072e3813e9ac20216c8cf6affc1d1b'
AND `user_agent` =  'Mozilla/5.0 (Windows NT 6.1; rv:26.0) Gecko/20100101 Firefox/26.0'' (length=171)
  public 'query_times' => 
    array (size=1)
      0 => float 0.00099992752075195
  public 'data_cache' => 
    array (size=0)
      empty
  public 'trans_enabled' => boolean true
  public 'trans_strict' => boolean true
  public '_trans_depth' => int 0
  public '_trans_status' => boolean true
  public 'cache_on' => boolean false
  public 'cachedir' => string '' (length=0)
  public 'cache_autodel' => boolean false
  public 'CACHE' => null
  public '_protect_identifiers' => boolean true
  public '_reserved_identifiers' => 
    array (size=1)
      0 => string '*' (length=1)
  public 'stmt_id' => null
  public 'curs_id' => null
  public 'limit_used' => null
  public 'stricton' => boolean false

If I have missed anything relevant to helping trouble shoot, please let me know, Thank you for your time , I believe it something simple I am missing, as I am self taught and my understanding isn't thorough as I'd like it to be. 如果我错过了与解决问题有关的任何事情,请告诉我, 谢谢您的时间 ,我认为这很简单,因为我是自学成才的,我的理解不够透彻是。

Change your function to this: 将功能更改为此:

function setpageid($friendlyname, $newid)
{
    $this->db->where('friendly_name', $friendlyname);
    $this->db->update('page_routes', array('pageid' => $newid));

    return $this->db->affected_rows()>0 ? TRUE : FALSE;
}

This will update the page_routes table, specifically the pageid column, with the value of $newid where friendly_name = $friendlyname . 这将更新page_routes表,尤其是pageid列,其值为$newid ,其中friendly_name = $friendlyname

It will then return either boolean true or false, whether any rows were affected. 然后,无论是否有任何行受到影响,它将返回布尔值true或false。

I hope this is what you mean? 我希望这是你的意思?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM