简体   繁体   English

将多个结果插入数据库PHP

[英]Insert multiple results into database PHP

This part is for gathering my data through an API. 这部分用于通过API收集我的数据。

foreach($result['List'] as $feedback)
{
    $date = date_create();
    $date_entered = $feedback['DateEntered'];
    $time = preg_replace('/[^0-9]/','',$date_entered);
    //$comment = $feedback['Text'];
    $ListingId = $feedback['ListingId'];
    $BuyNowPrice = $feedback['BuyNowPrice'];
    $max_bid = $feedback['MaximumBidAmount'];
    $SellerId = $feedback['SellerId'];
    echo '<div>' . "Seller ID: $SellerId" . " has sold one $ListingId for " . '$' . "$BuyNowPrice" . '</div>';
    echo "<div>Feedback created at " . $time . "</div>";
    echo '<br>';
}

This part is the code that I used to insert into my results directly after retrieving them. 这部分是我用来在检索结果后直接将其插入结果中的代码。

            <?php

   $dbhost = 'localhost';
   $dbuser = 'root';
   $dbpass = 'password';
   $conn = mysql_connect($dbhost, $dbuser, $dbpass);

   if(! $conn )
   {
      die('Could not connect: ' . mysql_error());
   }

   $sql = 'INSERT INTO tmfeedback '.
      '(SellerId,ListingId,BuyNowPrice) '.
      'VALUES ('.$SellerId.', '.$ListingId.', '.$BuyNowPrice.'))';

   mysql_select_db('dctdb3');
   $retval = mysql_query( $sql, $conn );

   if(! $retval )
   {
      die('Could not enter data: ' . mysql_error());
   }

   echo "Entered data successfully\n";

   mysql_close($conn);


            ?>

Only one data is being inserted into the database and it is the last data displayed. 只有一个数据正在插入数据库,它是最后显示的数据。 I was wondering how I can change my code so that I can insert all the data at the same time and not repetitive? 我想知道如何更改代码,以便可以同时插入所有数据而不重复输入? Thank you for your help. 谢谢您的帮助。

Make sure your second block of code is inside your first block of code (place your second block above the right-curly-brace). 确保第二个代码块在第一个代码块内(将第二个代码块放在右弯括号上方)。 Then it will occur for each iteration of the foreach loop (each result) and insert a record for each one. 然后,它将在foreach循环的每个迭代(每个结果)中发生,并为每个循环插入一条记录。

You cannot insert array into database hence place the query inside a loop. 您不能将数组插入数据库,因此会将查询放入循环中。 This thread may help you alot. 线程可能会帮助您很多。

Put the insertion inside the loop. 将插入内容放入循环中。 Otherwise, the variables just have the last values that were set in the last iteration of the loop. 否则,变量仅具有在循环的上一次迭代中设置的最后一个值。

<?php

$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'password';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);

if(! $conn ) {
    die('Could not connect: ' . mysql_error());
}
mysql_select_db('dctdb3');

foreach($result['List'] as $feedback) {
    $date = date_create();
    $date_entered = $feedback['DateEntered'];
    $time = preg_replace('/[^0-9]/','',$date_entered);
    //$comment = $feedback['Text'];
    $ListingId = $feedback['ListingId'];
    $BuyNowPrice = $feedback['BuyNowPrice'];
    $max_bid = $feedback['MaximumBidAmount'];
    $SellerId = $feedback['SellerId'];
    echo '<div>' . "Seller ID: $SellerId" . " has sold one $ListingId for " . '$' . "$BuyNowPrice" . '</div>';
    echo "<div>Feedback created at " . $time . "</div>";
    echo '<br>';

    $sql = 'INSERT INTO tmfeedback '.
        '(SellerId,ListingId,BuyNowPrice) '.
        'VALUES ('.$SellerId.', '.$ListingId.', '.$BuyNowPrice.'))';
    $retval = mysql_query($sql);
    if(! $retval ) {
        die('Could not enter data: ' . mysql_error());
    }

}

echo "Entered data successfully<br>";
mysql_close($conn);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM