如何使用指定的id(代码点火器)将数据更新到数据库

时间:2011-04-10 10:23:50

标签: php codeigniter

这是我在view / company / companyview.php下的查看页面 我对PHP&笨

我写了下面的代码并执行。没有结果,也没有更新。我哪里错了?。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<title>Welcome to Home page</title>
<link rel="stylesheet" type="text/css" href="<?php echo base_url();?>/css/topmenu.css">
</head>
<body>
<div class="container">
<br>
<p></p>
<div class="left">
<?php $this->load->view('company/sidemenu.php');?>
</div>
<div class="content">
<!-- <table border="1" cellpadding="3" cellspacing="0" bgcolor="white" id="table_style"> -->
<table id="table_style" border="1" cellpadding="3" cellspacing="0">
  <tr>
    <th>ID</th>
    <th>code</th>
    <th>name</th>    
    <th>address</th>    
    <th>tel</th>    
    <th>fax</th>
    <th>email</th>
  </tr>
<?php
$this->load->library('table');

foreach ($query as $row) 
{
    echo "<tr>";
    echo "<td>" . $row->id . "</td>";
    echo "<td>" . $row->code . "</td>";
    echo "<td>" . $row->name . "</td>";
    echo "<td>" . $row->address . "</td>";
    echo "<td>" . $row->tel . "</td>";
    echo "<td>" . $row->fax . "</td>";
    echo "<td>" . $row->email . "</td>";
    echo "<td>" . anchor('company/inputco/' . $row->id, 'Edit') . "</td>";
    echo "<td>" . anchor('company/del/' . $row->id, 'Delete') . "</td>";  // delete function OK
    //echo anchor('books/input/'. $row->id,'Edit');
    echo "</tr>";
}
?>
</table>
</div>
</div>


</body>
</html>

然后它会调用

是CI_Controller

function inputco($id)
{
        $this->load->helper('form');
        $this->load->library('table');
        $this->load->helper('html');
        $this->load->view('menu_top.php');
        $this->load->model('company_model');

        $query = $this->company_model->get($id);


        $data['id']['value'] = $query['id'];
        $data['code']['value'] = $query['code'];
        $data['name']['value'] = $query['name'];
        $data['address']['value'] = $query['address'];
        $data['tel']['value'] = $query['tel'];
        $data['fax']['value'] = $query['fax'];
        $data['email']['value'] = $query['email'];
        $data['business']['value'] = $query['business'];
        $data['url']['value'] = $query['url'];
        $data['rocno']['value'] = $query['rocno'];

        $this->load->view('company/companyinput', $data);
}

视图/公司/ companyinput.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">


<head>
<link rel="stylesheet" type="text/css" href="<?php echo base_url();?>/css/topmenu.css">
<title>company input</title>
</head>

<body>
<div class="container">
<br>
<p></p>
<div class="left">
<?php $this->load->view('company/sidemenu.php');?>
</div>
<div class="content">
<?php //echo form_open('company/input'); ?>
<?php echo form_open('company/updateco');?>
<?php echo form_hidden('id', $id['value']);?>

<table id="table_style">
<tr>
    <td>ID:</td>
    <td><input size="10"<?php echo form_input($id);?></td> 
</tr>
<tr>
    <td>Code:</td>
    <td><input size="10"<?php echo form_input($code);?></td>
</tr>
<tr>
    <td>Name:</td>
    <td><input size="30"<?php echo form_input($name);?></td>
</tr>
<tr>
    <td>Address:</td>
    <td><input size="50"<?php echo form_input($address);?></td>
</tr>
<tr>
    <td>Tel:</td>
    <td><input size="30"<?php echo form_input($tel);?></td>
</tr>
<tr>
    <td>Fax:</td>
    <td><input size="30"<?php echo form_input($fax);?></td>
</tr>
<tr>
    <td>Email:</td>
    <td><input size="30"<?php echo form_input($email);?></td>
</tr>
<tr>
    <td>Business:</td>
    <td><input size="30"<?php echo form_input($business);?></td>
</tr>
<tr>
    <td>URL:</td>
    <td><input size="30"<?php echo form_input($url);?></td>
</tr>
<tr>
    <td></td>
    <td><input size="30"<?php echo form_submit('mysubmit', 'Update');?></td>
</tr>
<?php echo form_close(); ?>
</table>
</Div>
</Div>
</body>
</html>

更新代码

function updateco()
{
    $this->load->helper('form');
    $this->load->library('form_validation');
    $this->load->helper('html');
    //$this->load->library('Activerecord');
    $this->load->model('company_model');
    if ($this->input->post('mysubmit'))
    {   
        if ($this->input->post('id')) 
        {
            $this->load->database();   
            $data = array
            (
                'code' => $this->input->post('$code'), 
                'name' => $this->input->post('$name'), 
                'address' => $this->input->post('$address'), 
                'tel' => $this->input->post('$tel'), 
                'fax' => $this->input->post('$fax'), 
                'email' => $this->input->post('$email'),
                'business' => $this->input->post('$business'), 
                'url' => $this->input->post('$url'),
                'rocno' => $this->input->post('$rocno')
            );
            $this->db->where('id', $this->input->post('id'));

            $this->db->update('companyinfo', $data);
            $this->load->view('menu_top.php'); 

          }
    } 

1 个答案:

答案 0 :(得分:1)

我不是100%确定你的问题,

但是在这样的view/company/companyinput.php行:

<input size="10"<?php echo form_input($id);?>

不正确。

您可以阅读更多here,但您的代码实际上会输出类似

的内容
  <input size="10"<input type="text" name="1" value=""  />

要输出带有大小的文本框,您应该使用类似这样的内容

<?php echo form_input(array( 'name' => $id, 'size' => '10'));?>

//编辑 实际上仔细检查你的错误是你将$id传递给form_input()方法。当视图中$ id的值实际上是一个包含奇怪值的数组时。

我认为您的问题出在Controller中,我无法在您的模型中看到,但我想它会返回一个这样的数组{'id'=>1, 'code'=>'ABC123','name'=>'Toms Hardware' ... }

但是当你这样做时:

$data['id']['value'] = $query['id'];

它写入存储在数组$ data ['id']中的'value'索引,该索引不是数组..它是一个数字或字符串或其他东西。我建议您查看HTML表单的来源,我认为它在输入框中有一些字符串查找名称。

我认为你真的想要这样的东西:

<?php echo form_input(array( 'name' => '', 'value'=> $id, 'size' => '10'));?>