0

I'm starting to build a very simple product display system, mainly to build my own skills, but also for use on my website for work.

List.php

<html>
<head>
<title>Retrieve data from the database</title>
<?php
$username='';
$password='';
$database='';
?>
</head>
<body>

<ul>

<?php
// Connect to database server
mysql_connect(localhost,$username,$password); 


// Select database
@mysql_select_db($database) or die( 'Unable to select database');  

// SQL query
$sql = "SELECT * FROM products WHERE category = 30";

// Execute the query (the recordset $rs contains the result)
$result = mysql_query($sql);

// Loop the recordset $rs
while($row = mysql_fetch_array($result)) {

   // Name of the person
  $strName = $row['make'];  
       // Create a link to person.php with the id-value in the URL
   $strLink = "<a href = 'product.php?id = " . $row['ID'] . "'>" . $strName . "</a>";

    // List link
   echo "<li>" . $strLink . "</li>";

  }

// Close the database connection
mysql_close();
?>

</ul>
</body>
</html>

Product.php

<html>
<head>
<title>Retrieve data from database</title>
</head>
<body>

<?php

$username="";
$password="";
$database="";
// Connect to database server
mysql_connect(localhost,$username,$password); 


// Select database
@mysql_select_db($database) or die( "Unable to select database");
// Get data from the database depending on the value of the id in the URL
$sql = mysql_query('SELECT * FROM products WHERE ID=' . $_GET["ID"]);

$result = mysql_query($sql); 

if(!$result)
    die(mysql_error());

// Loop the recordset 
while($row = mysql_fetch_array($result)) {

    // Write the data of the product
    echo $row['category'];
    echo "<p>";
    echo $row["make"];
    echo "<p>";
    echo $row["description"];
    echo "<p>";
    echo $row["picture"];
}

// Close the database connection
mysql_close();
?>

<p><a href="list.php">Return to the list</a></p>

</body>

</html>

Can be seen messing up HERE

If someone can help get this working for me I'd be very grateful!

DavidPostill
  • 7,734
  • 9
  • 41
  • 60
ZackUnity
  • 25
  • 5
  • 1
    Obligatory [stop using the deprecated mysql_* functions](https://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) and you have an SQL injection vulnerability comment. – Alexander O'Mara Mar 21 '15 at 02:40
  • 1
    You should have 'localhost' quoted in your `mysql_connect` statement. – Noah Heck Mar 21 '15 at 03:04

2 Answers2

0

Try this way, but not fully sure about this stubborn issue. You can try by changing the case of all keys of the $_GET array to lowercase using the array_change_key_case()

$get = array_change_key_case($_GET);
$id = $get['id'];

$sql = mysql_query('SELECT * FROM products WHERE ID=' . $id);

NB: Make first sure SELECT * FROM products WHERE ID=your_row_id return results or not?

A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103
  • Now, i've changed some code around, and now if I specify which ID I want it will display the product as I want. But the second I add . $_GET["ID"]); I receive the following error.... Warning: mysql_query() expects parameter 1 to be string, resource given in /home/haslemer/public_html/test/product.php on line 21 I've also just noticed that in the URL instead of ?id= I get ?id% Could that be affecting something? Thank you for all of your help. Edit: Line 21 is empty? – ZackUnity Mar 21 '15 at 19:30
0

I turned out that the link that the page 'list.php' was generating was incorrectly formatted.

Changed this:

$strLink = "<a href = 'product.php?id = " . $row['ID'] . "'>" . $strName . "</a>";

To

$strLink = "<a href = 'product.php?id=".$row['ID']."'>" . $strName . "</a>";

And it's now working exactly as it should!

ZackUnity
  • 25
  • 5