简体   繁体   中英

Insert multiple results into database PHP

This part is for gathering my data through an 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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