0

I am developing a wish list website using PHP and MySQL that displays products in a table. The user has the option to add each product to their own wish list.

I can display the products no problem but once the user clicks on the option to add to their own list I am getting nothing. The server I am running on also does not flag an error so I have no idea what I am doing wrong.

Any help would be greatly appreciated. I have included the code below.

 <?php
//connect to db
include('base.php');

//get results from db
$result = mysql_query("SELECT * FROM coffee_machines")
or die(mysql_error());

echo "<table border= '1' cellpadding='10'>";
echo "<tr> <th>ID</th> <th>Brand</th> <th>Description</th> <th>Price(£)</th> <th>image</th> </tr>";

$img_url = "http://localhost:8080/";
//loop through results of db query so items are displayed
while($row = mysql_fetch_array($result)){

    // echo out contents of each row
    echo '<tr>';
    echo '<tr>';
    echo '<td>'.$row["Product_ID"].'</td>';
    echo '<td>'.$row["Brand"].'</td>';
    echo '<td>'.$row["Description"].'</td>';
    echo '<td>'.$row["Price"].'</td>';
    echo '<td>'.'<img src="'.$img_url.$row['image'].'" /></td>';
    echo '<td>'.'<a href = "?Add To List">Add To List</a></td>';
    echo '</tr';
    echo '</tr';

    $add_to_list = "INSERT INTO andrew VALUES('".$row["Product_ID"]."', '".$row["Brand"]."', '".$row["Description"]."', '".$row["Price"]."', '".$row["image"]."')";
    if(isset($_POST['Add To List'])){
    mysql_query($add_to_list);
}
//close table
echo "</table>";
?>
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
ConorMcC
  • 3
  • 1
  • 1
    It can't work.. you wait a $_POST and your link give $_GET; – Amazone Aug 21 '16 at 18:57
  • In order to correct this should I change POST to GET? – ConorMcC Aug 21 '16 at 19:44
  • 1
    Please dont use [the `mysql_` database extension](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php), it is deprecated (gone for ever in PHP7) Specially if you are just learning PHP, spend your energies learning the `PDO` database extensions. [Start here](http://php.net/manual/en/book.pdo.php) its really pretty easy – RiggsFolly Aug 21 '16 at 22:40
  • 1
    You are a long way off the mark. Start by doing some simple PHP tutorials and also try to understand the PHP page life cycle – RiggsFolly Aug 21 '16 at 22:47
  • Thank you. I'm new to PHP so still finding my feet – ConorMcC Aug 22 '16 at 07:46

2 Answers2

1
  • You should change $_POST['Add To List'] to $_GET['Add To List'] since a click in a link generates a GET request.
  • with ?Add To List you are creating a variable, and you should not create variable names with spaces
  • you are not passing an id as a parameter or any way of identifying the specific row, so if the user clicks the link, the variable will be set and for all the elements on the loop will be an insertion in the andrew table. you should receive the id and make a new query to the db

A quick example, your link should be:

echo '<td>'.'<a href = "?id='.$row["Product_ID"].'">Add To List</a></td>'; 

then place your if out of the loop, like

if(isset($_GET['id'])){

    $result = mysql_query("SELECT * FROM coffee_machines Where id=".$_GET['id']." LIMIT 1");
    $row = mysql_fetch_array($result); 
    if ($row) {
       $add_to_list = "INSERT INTO andrew VALUES('".$row["Product_ID"]."', '".$row["Brand"]."', '".$row["Description"]."', '".$row["Price"]."', '".$row["image"]."')";

      mysql_query($add_to_list);
    }
}

but as have been said, you will try to look soon for a replacement for mysql_* functions

Carlos Robles
  • 10,828
  • 3
  • 41
  • 60
  • Thanks that's very helpful. The products are being added in but as you say all are being added rather than just distinct ones. I have been trying to get the id using a select statement but it doesn't seem to do anything. Is there an obvious way I seem to be missing? – ConorMcC Aug 21 '16 at 21:38
  • Carlos - can't thank you enough you helped me out big time there. I have started using MySQLi instead so I changed your example accordingly and it worked a treat. The site runs off the localhost and is a hypothetical site for a project so it won't be published. However, I take on board all the advice about MySQL functions. – ConorMcC Aug 22 '16 at 20:09
1

you can try to do "SELECT id from coffe_machine"; and then fetch the result also please don't use mysql functions they aren't safe

flex_
  • 679
  • 2
  • 8
  • 26