2

我测试并成功使用下面的 SQL 代码将数据输入数据库。

INSERT INTO hraps (id, firstname, lastname, gender, year_of_1st_rappel, count_offset_proficiency, count_offset_operational, spotter) values(111111, 'World', 'Hello', 'Male', '2007', '1', '2', '0')

现在我正在尝试将它集成到 PHP 中,如下所示:

 $query = "INSERT INTO hraps (firstname, lastname, gender, year_of_1st_rappel, count_offset_proficiency, count_offset_operational, spotter) "
."values('".$this->firstname."','".$this->lastname."','".$this->gender."','".$this->year_of_1st_rappel."',".$this->count_offset_proficiency.",".$this->count_offset_operational.",".$this->spotter.") returning id into :id";

$dbid = "";
$binds = array();
$binds[] = array("name" => ":id", "value" => &$dbid, "length" => 128);
//echo $query;              
$result = mydb::cxn()->query($query, $binds);
$this->id = $dbid;

但是什么都没有插入,我也没有收到任何错误。唯一的区别是,在这一个中,我将 id 定义为 $dbid,并且在我将其硬编码到查询的“值”部分之前。

有人可以指出为什么这段代码不能成功运行吗?谢谢你。

4

2 回答 2

1

只是删除"."之前VALUES。尝试这个

你错过了mysqli_query()

   $query= "INSERT INTO hraps (firstname, lastname, gender, year_of_1st_rappel, count_offset_proficiency, count_offset_operational, spotter) 
  values ('".$this->firstname."','".$this->lastname."','".$this->gender."','".$this->year_of_1st_rappel."',".$this->count_offset_proficiency.",".$this->count_offset_operational.",".$this->spotter.") returning id into :id ";

编辑:如果是甲骨文,那么使用这个

    $compiled = oci_parse($db, $query); //-- $db is your connection to database variable
    oci_bind_by_name($compiled, ':id', $id);  // --your id
    oci_execute($compiled);
于 2013-02-08T22:27:26.667 回答
0

上面的两个查询不相等,因为它们使用不同的数据类型(int v string)。

第一个转换为字符串,第二个转换为 int。将 INSERT 查询更改为:

$query = "INSERT INTO hraps (firstname, lastname, gender, year_of_1st_rappel, count_offset_proficiency, count_offset_operational, spotter) "
."values('".$this->firstname."','".$this->lastname."','".$this->gender."','".$this->year_of_1st_rappel."','".$this->count_offset_proficiency."','".$this->count_offset_operational."','".$this->spotter."') returning id into :id";`
于 2013-02-08T22:32:29.500 回答