Php 如何检查id是否已经存在-codeigniter
我试图检查数据库中是否已经存在一个id,如果不存在,则只插入该id,而不插入存在的其他id 我曾尝试使用where语句检查数据库中是否存在他们的id,但即使他们是新信息,也不会将其插入数据库 我在这里迷路了 如有任何指导,将不胜感激 ps我不想更新行我想插入一个不存在的新更新行Php 如何检查id是否已经存在-codeigniter,php,codeigniter,Php,Codeigniter,我试图检查数据库中是否已经存在一个id,如果不存在,则只插入该id,而不插入存在的其他id 我曾尝试使用where语句检查数据库中是否存在他们的id,但即使他们是新信息,也不会将其插入数据库 我在这里迷路了 如有任何指导,将不胜感激 ps我不想更新行我想插入一个不存在的新更新行 $this->db->where('id',$id); $q = $this->db->get('testing'); if($q) { //Do nothing } else {
$this->db->where('id',$id);
$q = $this->db->get('testing');
if($q)
{
//Do nothing
}
else
{
$this->db->set('id', $id);
$this->db->set('message', $message);
$query= $this->db->insert('testing');
}
您需要在MYSQL表中选择要检查的id,然后计算行数。如果行计数为0,则id不存在
$query = mysql_query("SELECT * FROM your_table WHERE id='$id'");
$count = mysql_num_rows($query);
If($count!=0){
// id exists
} else {
// id doesn't exist
}
通常,“id”字段设置为自动增量,并设置为主,这是唯一且不可重复的。因此,不必担心存在的问题 然而,在您的情况下,我认为您没有将其用作“唯一字段” 让我给你举个例子 这里我有一个表名“水果”
++++++++++++++++++++++++++++++++++++
ငfruit_id | int (primary)
name | text
id | int
++++++++++++++++++++++++++++++++++++++
在您的模型中
function checkId($id)
{
$query=$this->db->get_where('fruits',array('id'=>$id)); //check if 'id' field is existed or not
if($query!=null) // id found stop
{
return FALSE;
}
else // id not found continue..
{
$data = array(
'fruit_id' => $fruit_id ,
'name' => $name ,
'id' => $id
);
$this->db->insert('fruits', $data);
}
}
这应该可以做到。您的代码有一个需要修复的逻辑问题 在代码中,将查询结果保存为
$q=$this->db->get('testing')
,
无论返回的行数是多少,$q
将始终计算为true
您需要使用$query->num_rows()>0检查行数,然后检查其余行数
所有代码都将按预期的方式运行
有关更多详细信息,请参阅:型号
<?php
class Fruits_model extends CI_Model
{
function __construct()
{
parent::__construct();
$this->load->database();
}
function check()
{
$query = null; //emptying in case
$id = $_POST['id']; //getting from post value
$name = $_POST['name'];
$query = $this->db->get_where('fruits', array(//making selection
'id' => $id
));
$count = $query->num_rows(); //counting result from query
if ($count === 0) {
$data = array(
'name' => $name,
'id' => $id
);
$this->db->insert('fruits', $data);
}
}
}
?>
要检查数据库CI中不存在的Id或任何列值,请为其设置验证规则
现场观看:
规则:是唯一的
如果表单元素对于参数中的表和字段名不是唯一的,则返回FALSE。注意:此规则要求启用查询生成器才能工作
示例:是唯一的[table.field]
$this->form_validation->set_rules(
'username', 'Username',
'required|min_length[5]|max_length[12]|is_unique[users.username]',
array(
'required' => 'You have not provided %s.',
'is_unique' => 'This %s already exists.'
)
);
要更高级地使用验证,可以使用数组添加所有验证设置规则
$this->form_validation->set_rules(
'username', 'Username',
'required|min_length[5]|max_length[12]|is_unique[users.username]',
array(
'required' => 'You have not provided %s.',
'is_unique' => 'This %s already exists.'
)
);
$config = array(
'your_rule_name' => array(
array(
'username', 'Username',
'required|min_length[5]|max_length[12]|is_unique[users.username]',
array(
'required' => 'You have not provided %s.',
'is_unique' => 'This %s already exists.'
)
)
),
array(
'field' => 'email',
'label' => 'Email',
'rules' => 'required'
)
);
$this->form_validation->set_rules($config);
你应该这样做:
public function record_exists(){
$exists = $this->db->get_where('table_name', array('id' => $id));
if($exists->num_rows() > 0 ){
echo "Some message";
return false;
}else{
// Insert your data into the database...
}
}
运行此操作时,您是否检查过$q是什么?$q检查数据库中是否存在任何ID,因为它不会向数据库中插入任何新信息,但我会验证$q实际上是false
。尝试if($q->num_rows()>0){…}
,然后在if
中做一个echo
,这样你就可以确定发生了什么。我在if和else语句中做了一个echo,它打印在两个决策上我不知道为什么$fruit\u id,$name,$id是从formi中发布的值。我尝试了您的代码,但它将所有结果返回为false,即使它们是新的idokay。如果可以使用,则必须使用| num_rows();检查我的下一个答案以获得完整的解决方案为此,您可以始终使用表单验证规则is_unique[table.field_name]来检查唯一值
public function record_exists(){
$exists = $this->db->get_where('table_name', array('id' => $id));
if($exists->num_rows() > 0 ){
echo "Some message";
return false;
}else{
// Insert your data into the database...
}
}