简体   繁体   中英

Taking Items from an Array and putting them into a database

I am still learning so sorry if this is simple... What I am trying to achieve is an online shop checkout system (dont worry it wont be used it is just for my own learning so any sloppy code wont affect a poor business owner but any pickups on sloppy code is always appreciated).

So I have had the customer choose products, these then get stored in arrays and are displyed in the basket. I then want to take the user to a checkout page so they can click 'order' which will then update these items along with their username to an 'Order' table in the database.

I am still getting to grips with arrays (possibly an identifier of what stage of learning I am at) so I am unsure how to take these items (essentially just the ISBN number and Prices) from my basket to a checkout page when I have several different arrays. What I will do is show how I created each array and and how I display it in the basket and hopefully this will be everything you may need! Thank you so much for reading and any help will be greatly appreciated.. I hope this question may help others who are at my stage as well.

Creating the array=

products.php

echo "<a href='addtolist.php?bookname=" . $bookname . "&bookauthor=" . 
     $bookauthor . "&bookpub=" . $bookpub . "&bookprice=" . $bookprice . 
     "&bookisbn=" . $bookisbn . "'>Add to basket</a>";

addtolist.php

<?php
 session_start();
 if ( !isset($_SESSION['username']) )
{
header("Location:index.php");
exit();
}

$bookarray = array();
$bookarray['bookname'] = $_GET['bookname'];
$bookarray['bookauthor'] = $_GET['bookauthor'];
$bookarray['bookpub'] = $_GET['bookpub'];
$bookarray['bookisbn'] = $_GET['bookisbn'];
$bookarray['bookprice'] = $_GET['bookprice'];

$found = false;

if (isset($_SESSION['list']))
{
  foreach ($_SESSION['list'] as $key => $another)
  {
    if ($_SESSION['list'][$key]['bookisbn'] == $_GET['bookisbn'])
        {
          $found = true;
          break;
    }
  }
}

if ($found == false)
{
  $_SESSION['list'] [] = $bookarray;
}

header('Location: ' . $_SERVER['HTTP_REFERER']);

exit();
?>

displaylist.php

<?php

if (!isset($_SESSION['list']))
{
echo "No list members selected";
}
else
{
?>
<div id="bket2">

<table>
<tr>
<td class='toprow'>Title</td>
<td class='toprow'>Author</td>
<td class='toprow'>Publisher</td>
<td class='toprow'>ISBN</td>
<td class='toprow'>Price</td>
</tr>

<?php

$totalprice=0;

foreach ($_SESSION['list'] as $key => $another)
{
?>
<tr>
        <td><?php echo $_SESSION['list'][$key]['bookname']?></td>
        <td><?php echo $_SESSION['list'][$key]['bookauthor']?></td>
    <td><?php echo $_SESSION['list'][$key]['bookpub']?></td>
    <td><?php echo $_SESSION['list'][$key]['bookisbn']?></td>
    <td><?php echo "&#163;".$_SESSION['list'][$key]['bookprice']?></td>
    <td><a href="removefromlist.php?bookisbn= <?php echo $_SESSION['list'][$key]['bookisbn']?> &location= <?php echo
    $_SERVER['PHP_SELF']?> ">[-]</a></td>
    </tr>
    <?$totalprice += $_SESSION['list'][$key]['bookprice'];?>
    <?php
    }
    }
    ?>
    </table>
    <div><h4>Total Price = &#163;<?echo $totalprice;?></h4></div>

</div>

Then in my basket div I put

<?php require "displaylist.php" ?

Thanks guys

Here's an example that should get you started.

<?php

$dbh = new PDO('mysql:host=localhost;dbname=mysql', 'username', 'password');
$stmt = $dbh->prepare('insert into orders (isbn, price) values (?, ?)');

if (isset($_SESSION['list']))
{
  foreach ($_SESSION['list'] as $item)
  {
    $stmt->execute(array($item['bookisbn'], $item['bookprice']));
  }
}
?>

Wasn't exactly sure what you were trying to do, but I assumed you wanted to insert the book values you stored in $_SESSION['list'] into your 'orders' table in mysql. Here's my take on it (assumed you had 'username' stored in a session variable somewhere):

<?php
session_start();

//connect to db
$con=mysql_connect("localhost","username","password");

if(!$con){
    die("Could not connect to the database: " .mysql_error());
};

mysql_select_db("db_name",$con);

//loop through each book in 'list' and add values to mysql table 'order'
for($i=0; $i<count($_SESSION['list']);$i++){

    $query="INSERT INTO `orders` 
                ('username',
                'isbn',
                'price',
                'timestamp') 
                VALUES 
                ('".mysql_real_escape_string($_SESSION['username'])."',
                '".mysql_real_escape_string($_SESSION['list'][$i]['bookisbn'])."',
                '".mysql_real_escape_string($_SESSION['list'][$i]['bookprice'])."',
                CURDATE()
                )";

    $result=mysql_query($query);
}

mysql_close();

    ?>

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