2015-05-01 49 views
0

我剛剛切換到PDO,我仍然遇到PHP表單的問題。 表格由國家用戶註冊,每個國家可以輸入最少2名和最多3名選手。我創建了一個表單,默認情況下爲3名玩家,第三種形式是可選的,以防他們需要添加玩家。PDO插入3個表格值到4個表格

但是,我是新來的PDO,我會告訴你整個腳本,所以你可以幫助我解決腳本的故障。

腳本和窗體位於同一個文件中,我運行一個測試,頁面變成白色,沒有錯誤。

現在我想要做的是插入至少2 /最多3個團隊球員數據到MySQL。

這裏是我的PHP代碼:

<?php 
try 
{ 
require_once 'connection_pdo.php'; 

    $championships = 'SELECT * FROM champs 
         WHERE champ_status = 1 
         ORDER BY champ_name ASC'; 
    $championships_results = $db->query($championships); 

    $users = 'SELECT userid, username FROM users'; 
    $users_results = $db->query($users); 

    $colname_numRows = "-1"; 
    if (isset($_SESSION['MM_UserGroup'])) { 
     $colname_numRows = $_SESSION['MM_UserGroup']; 
    } 

    if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) { 

    $stmt = $db->prepare("INSERT INTO players (playerid, FirstName, LastName, Dob, NationalRanking, PhoneNumber, EmailAddress, CountryID) 
    VALUES (:playerid, :FirstName, :LastName, :Dob, :NationalRanking, :PhoneNumber, :EmailAddress, :CountryID)"); 
    //player 1 
    $stmt->bindParam(':FirstName', $_POST["playerFirstName1"]); 
    $stmt->bindParam(':LastName', $_POST["playerLastName1"]); 
    $stmt->bindParam(':Dob', $_POST['year1']."-".$_POST['month1']."-".$_POST['day1']); 
    $stmt->bindParam(':Ranking', $_POST["playerNationalRanking1"]); 
    $stmt->bindParam(':playerPhoneNumber', $_POST["playerPhoneNumber1"]); 
    $stmt->bindParam(':playerEmailAddress', $_POST["playerEmailAddress1"]); 
    $stmt->bindParam(':playerCountryID', $_POST["playerCountryID1"]); 
    $stmt->execute(); 
    $last_ID_of_P1 = $db->lastInsertId(); 
    //player 2 
    $stmt->bindParam(':FirstName', $_POST["playerFirstName2"]); 
    $stmt->bindParam(':LastName', $_POST["playerLastName2"]); 
    $stmt->bindParam(':Dob', $_POST['year2']."-".$_POST['month2']."-".$_POST['day2']); 
    $stmt->bindParam(':Ranking', $_POST["playerNationalRanking2"]); 
    $stmt->bindParam(':playerPhoneNumber', $_POST["playerPhoneNumber2"]); 
    $stmt->bindParam(':playerEmailAddress', $_POST["playerEmailAddress2"]); 
    $stmt->bindParam(':playerCountryID', $_POST["playerCountryID2"]); 
    $stmt->execute(); 
    $last_ID_of_P2 = $db->lastInsertId(); 
    //player 3 
    $stmt->bindParam(':FirstName', $_POST["playerFirstName3"]); 
    $stmt->bindParam(':LastName', $_POST["playerLastName3"]); 
    $stmt->bindParam(':Dob', $_POST['year3']."-".$_POST['month3']."-".$_POST['day3']); 
    $stmt->bindParam(':Ranking', $_POST["playerNationalRanking3"]); 
    $stmt->bindParam(':playerPhoneNumber', $_POST["playerPhoneNumber3"]); 
    $stmt->bindParam(':playerEmailAddress', $_POST["playerEmailAddress3"]); 
    $stmt->bindParam(':playerCountryID', $_POST["playerCountryID3"]); 
    $stmt->execute(); 
    $last_ID_of_P3 = $db->lastInsertId(); 

    $stmt2 = $db->prepare("INSERT INTO participants (playerID, idparticipants, idChampionships, idUsers, created_at, status, booking_status) VALUES($last_ID_of_P1, :idparticipants, :idChampionships, :idUsers, :created_at, 1, 0)"); 

    //participant player 1 
    $stmt2->bindParam(':idparticipants', $_POST["idparticipants1"]); 
    $stmt2->bindParam(':idChampionships', $_POST["idChampionships1"]); 
    $stmt2->bindParam(':idUsers', $_POST['idUsers1']); 
    $stmt2->bindParam(':created_at', $_POST["created_at1"]); 
    $stmt2->execute(); 
    $last_ID_of_participant_1 = $db->lastInsertId(); 

    //participant player 2 
    $stmt2->bindParam(':idparticipants', $_POST["idparticipants2"]); 
    $stmt2->bindParam(':idChampionships', $_POST["idChampionships2"]); 
    $stmt2->bindParam(':idUsers', $_POST['idUsers2']); 
    $stmt2->bindParam(':created_at', $_POST["created_at2"]); 
    $stmt2->execute(); 
    $last_ID_of_participant_2 = $db->lastInsertId(); 

    //participant player 3 
    $stmt2->bindParam(':idparticipants', $_POST["idparticipants3"]); 
    $stmt2->bindParam(':idChampionships', $_POST["idChampionships3"]); 
    $stmt2->bindParam(':idUsers', $_POST['idUsers3']); 
    $stmt2->bindParam(':created_at', $_POST["created_at3"]); 
    $stmt2->execute(); 
    $last_ID_of_participant_3 = $db->lastInsertId(); 

    $stmtTeam = $db->prepare("INSERT INTO teamPlayers (TeamID, idparticipant, idUsers, Idplayer1, Idplayer2, Idplayer3) VALUES(TeamID, 111021, $last_ID_of_P1, $last_ID_of_P2, $last_ID_of_P3)"); 
    $stmtTeam->execute(); 

    //upload script 1 
    if(!is_dir("upload/".$_SESSION['MM_Username'])) { 
    //do we need to make the uploads directory for the files? 
    mkdir("upload/".$_SESSION['MM_Username']); 
    //make the rest of the script safe, though this will only be done once 
    if(is_uploaded_file($_FILES['uploadedfile']['tmp_name'])){ 
    $folder = "upload/".$_SESSION['MM_Username']; 
    $file = "/" .sha1(rand())."-" .basename($_FILES['uploadedfile']['name']); 
    //basename($_FILES['uploadedfile']['name']); 
    $full_path = $folder.$file; 

     $idPlayer  = $last_ID_of_P1; 
     $IP    = $_SERVER['REMOTE_ADDR']; 
     $FileLocation = $full_path; 
     $idUsers  = $_SESSION['MM_UserGroup']; 

     if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $full_path)){ 

      $sql_image ="INSERT INTO files(Time, FileLocation, IP, idUsers, idparticipant)values(NOW(),:FileLocation,:IP,:idUsers,:idparticipant)"; 
      $qry=$db->prepare($sql_image); 
      $success = $qry->execute(array(':FileLocation'=>$full_path,':IP'=>$IP,':idUsers'=>$idUsers,':idparticipant'=>$idPlayer)); 
     }else{ 
      $sql_image ="UPDATE files SET Time=?, FileLocation=?, IP=?, idUsers=?, idparticipant=? where idUsers=?"; 
      $qry=$db->prepare($sql_image); 
      $success = $qry->execute(array(NOW(), $full_path, $IP, $idUsers, $idPlayer)); 
     } 

     if($success){ 
      echo "<script language='javascript' type='text/javascript'>alert('Successfully Saved!')</script>"; 
      echo "<script language='javascript' type='text/javascript'>window.open('listfiles.php','_self')</script>"; 
     }else{ 
      echo 'db transaction failed'; 
     } 
    } else { 
     echo "upload received! but process failed"; 
      } 
    } 
    else{ 
    echo "upload failure ! Nothing was uploaded"; 
     } 
    } 
}//upload script2 

catch (PDOException $e) 
{ 
    //$error = $e->getMessage();  
    echo "Error: " . $e->getMessage(); 
} 
?> 

正如你所看到的,我不停地重複3個插件,以獲得最後插入的ID在每個表在其他表引用。

HTML表單位於我的上一個問題中,但與mysql_ *函數相同。

在此先感謝。

PHP MySQL inserting 3 different form values to 4 different tables

+0

你可以使用** **交易我 –

+0

需要的指導,請明確的方向給我這麼我可以前進。 –

+0

你能在這裏打印你的連接字符串嗎?如何與您的本地主機連接?你有沒有設置** PDO :: ATTR_ERRMODE **選項? –

回答

0

一定要仔細閱讀PDO實例:

Complete guide and error debugging

檢查調試模式是

的error_reporting(E_ALL);

$stmt = $db->prepare("INSERT INTO table(field1,field2,field3,field4,field5) VALUES(:field1,:field2,:field3,:field4,:field5)"); 
$stmt->execute(array(':field1' => $field1, ':field2' => $field2, ':field3' => $field3, ':field4' => $field4, ':field5' => $field5)); 
$affected_rows = $stmt->rowCount(); 

在一次只有一個執行語句一個準備聲明能可能

+0

謝謝@Ajeet庫馬爾 –