0

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.

3 Answers3

0

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.

John Doe
  • 905
  • 8
  • 9
0

You cannot insert array into database hence place the query inside a loop. This thread may help you alot.

Community
  • 1
  • 1
Muhammad Muazzam
  • 2,810
  • 6
  • 33
  • 62
0

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);
Barmar
  • 741,623
  • 53
  • 500
  • 612