簡體   English   中英

如何遍歷數組並將其保存到數據庫 (PHP)?

[英]How do I loop through my array and save it to database (PHP)?

我的數據庫中有一個表 select,希望用戶只更新其中的一列。 我使用在每行末尾添加的用戶輸入框將行回顯到表中。 我嘗試了 foreach 循環並以錯誤結束“致命錯誤:未捕獲錯誤:class mysqli_result 的 Object 無法轉換為字符串...”當我打印用戶輸入的數組時,它會顯示,但我是努力在准備好的陳述中使用它。

<?php
 if (isset($_POST['save'])){
  

if(!empty($_POST['newbid'])) {
    $biduserID = $_SESSION['id'];
    $itemID = $_GET['ItemId'];
    $bidprice = ($_POST['newbid']);
$getcurrentround = "SELECT `Round` FROM `RoundCounter` WHERE `ItemID` =".$_GET['ItemId']."";
$currentroundresult= $db->query($getcurrentround);
$currentround = $currentroundresult->fetch_assoc();
$currentround1 = $currentround['Round'];
$biddername = $_SESSION["id"];
$count = $_POST['count'];
$newbid = $_POST['newbid']; // check empty and check if interger
print_r($newbid);
$getusername = "SELECT `Username` FROM `User` WHERE `UserID` = `$biddername`";
$username1= $db->query($getusername);
$getbandname = "SELECT `BandName` FROM `BidTables` WHERE `ItemID` =" .$_GET['ItemId']."";
$bandname= $db->query($getbandname);
$getnumberlots = "SELECT numberlots FROM `Item` WHERE `ItemID` =".$_GET['ItemId']."";
    $numberlots= $db->query($getnumberlots);

$bid = 1;
   
    foreach($_POST as $bid => $value) {


    

    $sql4 = "INSERT INTO BidTables (`BandName`,`BidderID`, `ItemID`, `BidPrice`, `Round`, `Username`) VALUES
    (?,?,?,?,?,?)";
    $stmt = $db->prepare($sql4);
    echo $db->error;
    
        $stmt->bind_param("siiiis", $bandname, $biduserID, $itemID, $bid ,$currentround1, $username1 );
$stmt->execute();
}
}
?>

這是帶有表格數據的提交按鈕:

<form action="" method="POST">
        <table class="table table-hover">
                <thead class="thead">
                <tr class="header">
                    
                    
                <th>ID</th>
                    <th>Band</th>
            <th>Current Price</th>
               
                </tr>
                <?php 
                $sql = "SELECT * FROM BidTables WHERE ItemID = ".$_GET['ItemId']." ORDER BY `Round` DESC";
                $resultSQL= mysqli_query($db, $sql);
                   
                   if(mysqli_num_rows($resultSQL) > 0){
                   
                    }
                   
                   ?>
                </thead>
                <tbody>
                <!-- PHP CODE TO FETCH DATA FROM ROWS -->
                
                <tr>
               
    <?php
    // LOOP TILL END OF DATA
    while($row = $resultSQL->fetch_assoc()) { 
   ?>
      <tr>
        <td><?=$row['bidtable']?></td> 
        <td><?=$row['BandName']?></td>
        <td><?=$row['BidPrice']?></td>
        <td><input type="number" name="newbid[]" size="10" /></td> 
      </tr>
    <?php } ?>
 </table>
 <input type="hidden" name="count" value="<?=$resultSQL->num_rows?>" /> 
 <button class="btn btn-primary btn-lg" name="save">Submit</button>
</form>

我感謝任何幫助/指點

編輯 1:我已經為每個 SELECT 使用了准備好的語句,我在 INSERT function 中收到了每個語句的錯誤:“警告:數組到字符串的轉換......”

這是准備好的語句的代碼:

<?php

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

if(!empty($_POST['newbid'])) {
    $biduserID = $_SESSION['id'];
    $itemID = $_GET['ItemId'];
    $bidprice = ($_POST['newbid']);
    
$count = $_POST['count'];
$newbid = $_POST['newbid']; // check empty and check if interger
print_r($newbid);


$sql6 = "SELECT `Round` FROM `RoundCounter` WHERE `ItemID` =?"; // SQL with parameters
$stmt6 = $db->prepare($sql6); 
$stmt6->bind_param("i", $itemID);
$stmt6->execute();
$result6 = $stmt6->get_result(); // get the mysqli result
$round = $result6->fetch_assoc(); // fetch data   



$sql7 = "SELECT `Username` FROM `User` WHERE `UserID` = ?"; // SQL with parameters
$stmt7 = $db->prepare($sql7); 
$stmt7->bind_param("i", $biduserID);
$stmt7->execute();
$result7 = $stmt7->get_result(); // get the mysqli result
$username = $result7->fetch_assoc(); // fetch data   



$sql8 = "SELECT `BandName` FROM `BidTables` WHERE `ItemID` =?"; // SQL with parameters
$stmt8 = $db->prepare($sql8); 
$stmt8->bind_param("i", $itemID);
$stmt8->execute();
$result8 = $stmt8->get_result(); // get the mysqli result
$bandname = $result8->fetch_assoc(); // fetch data   




    $sql9 = "SELECT numberlots FROM `Item` WHERE `ItemID` =?"; // SQL with parameters
$stmt9 = $db->prepare($sql9); 
$stmt9->bind_param("i", $itemID);
$stmt9->execute();
$result9 = $stmt9->get_result(); // get the mysqli result
$numberlots = $result9->fetch_assoc(); // fetch data 

   
    foreach($_POST as $bid => $value) {
      
    $sql4 = "INSERT INTO BidTables (`BandName`,`BidderID`, `ItemID`, `BidPrice`, `Round`, `Username`) VALUES
    (?,?,?,?,?,?)";
    $stmt = $db->prepare($sql4);
    echo $db->error;
    
        $stmt->bind_param("siiiis", $bandname, $biduserID, $itemID, $bid ,$round, $username );
$stmt->execute();
}       
}
 }
?>

有人好心地幫我找到了問題,我錯誤地准備了我的桌子。 最后的循環看起來像這樣:

foreach ($newBidIds as $id){
    $insertStmt = $db->prepare("INSERT INTO BidTables (`BandName`,`BidderID`, `ItemID`, `BidPrice`, `Round`, `Username`) VALUES (?,?,?,?,?,?) ;");
    $insertStmt->bind_param("siiiis", $bandname1, $bidUserID, $itemID, $id,$round1, $username);
    $insertStmt->execute();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM