2017-07-12 6 views
0

イムで、0を返すにおけるlastInsertIdにおけるlastInsertId(もmysqli_insert_idで起こっ)が、それは常に0は::も、機能のPDOを使用して永続的な接続

を返すだ私はすでに問題を見て、これはべきだったことを見ましたphpmyadminの「config.inc.phpファイル」ファイル内persistentConnectionsをオンにしてMySQL: LAST_INSERT_ID() returns 0 が、問題はまだ

私のテーブルの予約が 'はAUTO_INCREMENTs主キーを持っている...残り:問題を修正しました。ここ

は私のコードです:私はこのJavaScriptコードを呼び出して自分のサイト上のボタンだ

:私のMySQLサーバ上で

function sonderbuchung() 
{ 
    setReservationType(); 
    getSonderbuchungID(); 
} 


function getSonderbuchungID() { 
    $.ajax({ 
     url:'sonderbuchungEditID.php', 
     complete: function (response) { 
      $('#output').html(response.responseText); 
     }, 
     error: function() { 
      $('#output').html('Bummer: there was an error!'); 
     } 
    }); 
    return false; 
} 

function setReservationType() 
{ 
    $.ajax({ 
    url: "reservationType.php", 
    type: "POST", 
    data: 'reservationtype=sonderbuchung', 
    success: function(data) { 
     $('#output').html(data); 
    }, 
    error: function(data) { 
     $('#output').html(data.responseText) 
    }, 
}); 

} 

を今そこに挿入起こった後に生成されたランダムな文字列であり、私は最後の挿入されたIDを見て、ランダムなStringを取ることによってランダムなStringを取得したい。 (この問題の「原因は明らかにまだ実装されていません)

sonderbuchungEditID.php:

<?php 
require_once('bdd.php'); //Database connection 
echo($bdd->lastInsertID()); 
?> 

reservationType.php(ちょうどすべてのコードのためにすべての微細加工、)

<?php 
require_once('bdd.php'); 

if(isset($_POST['reservationtype'])){ 

$reservationtype = $_POST['reservationtype']; 

$sql = "INSERT INTO reservations(reservationtype) values ('$reservationtype')"; 

$query = $bdd->prepare($sql); 
if ($query == false) { 
    file_put_contents('LOGname.txt', print_r($bdd->errorInfo(), true)); 

    die ('Error prepairing'); 

    } 
    $sth = $query->execute(); 
    if ($sth == false) { 
    file_put_contents('LOGname.txt', print_r($query->errorInfo(), true)); 
    die ('Error executing'); 
    } 

} 


?> 
+0

あなたは何も挿入せず、最後のインサートIDを取得呼んでいます。 insert文が実行された直後にのみ動作します。 –

+0

私はlastInsertIDを使用し、それを "reservationType.php"に直接エコーする必要がありますか? –

+0

下記の私の答えを参照してください。 –

答えて

0

ます行が挿入されたときにIDを取得して格納する必要があります。

sonderbuchungEditID.php:

<?php 
    echo isset($_SESSION['last_id']) ? $_SESSION['last_id'] : "-1"; 
?> 

reservationType.php:

<?php 
require_once('bdd.php'); 

if(isset($_POST['reservationtype'])){ 
    $reservationtype = $_POST['reservationtype']; 
    $sql = "INSERT INTO reservations(reservationtype) values ('$reservationtype')"; 
    $query = $bdd->prepare($sql); 
    if ($query == false) { 
     file_put_contents('LOGname.txt', print_r($bdd->errorInfo(), true)); 
     die ('Error prepairing'); 
    } 
    $sth = $query->execute(); 
    if ($sth == false) { 
     file_put_contents('LOGname.txt', print_r($query->errorInfo(), true)); 
     die ('Error executing'); 
    } else { 
     // Remember the last ID inserted. 
     $_SESSION['last_id'] = $bdd->lastInsertID(); 
    } 
} 


?> 
+0

ありがとうございました! –

+1

他の誰かのための簡単な情報として:まずこれを動作させるためにセッションを開始する必要があります! –

+0

これがうまくいく場合は、その答えに同意してください。 –

関連する問題