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 ";

編集 : Oracle の場合は、これを使用します

    $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

上記の 2 つのクエリは、異なるデータ型 (int v string) を使用するため、同じではありません。

最初は文字列としてキャストし、2 番目は 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 に答える