<?php
session_start();

if(isset($_SESSION['is_user_logged_in']))
{
$x=mysql_connect("localhost","root","");
$y=mysql_select_db("online_book_shop",$x);
$n=$_SESSION['id'];

echo "<html>
<body background=modern-background-design.jpg>
<div align=center>
<h1 style=text-align:center;font-family:fantasy;color:red;font-size:36px> COMPLETE BOOK 

DATABASE</h1></br>";
$z=mysql_query("select * from books order by serial asc");

echo"</br><br/><br/><br/><table border = 4 bgcolor=white align=center>";

echo"<tr> <td> Serial number</td> <td> Name </td> <td> Price </td> <td> Quantity remaining</td> 
<td> Purchase</td> <td> Add to cart </td> <td> Add to wishlist </td> </tr> ";

while($r=mysql_fetch_array($z))
{
echo"<tr>";
echo"<td>";
echo $r[0];
echo "</td>";
echo "<td>";
echo $r[1];
echo"</td>";
echo "<td>";
echo $r[2];
echo "</td>";
echo"<td>";
echo $r[3];
echo"</td>";
echo"<td>";
echo "<html> <form name=\"f1\" method=\"post\" action=\"purchase.php\"><p align=\"center\" ><input 

type=\"submit\" value=\"P u r c h a s e \" name=\"b1\" style=

\"height:25px;width:100px;color:#C35617;font-family:impact\" /></p></form> </html>";
echo"</td>";
echo"<td>";
echo "<html> <form name=\"f1\" method=\"post\" action=\"purchase.php\"><p align=\"center\" ><input 

type=\"submit\" value=\"A d d   to  C a r t \" name=\"b2\" style=

\"height:25px;width:100px;color:#C35617;font-family:impact\" /></p></form> </html>";
echo"</td>";
echo"<td>";
echo "<html> <form name=\"f1\" method=\"post\" action=\"purchase.php\"><p align=\"center\" ><input 

type=\"submit\" value=\" W i s h   L i s t \" name=\"b3\" style=

\"height:25px;width:100px;color:#C35617;font-family:impact\" /></p></form> </html>";
echo"</td>";
echo"</tr>";
}
echo"</table>";

if ($_POST)
{
$x=mysql_connect("localhost","root","");
$y=mysql_select_db("ONLINE_BOOK_SHOP",$x);
if ($_POST['b1'])
{
$z1=mysql_query("update user set Purchase ='$r[1]' where id=$n");
echo "<br/>";
}
if ($_POST['b2'])
{
$z2=mysql_query("update user set Cart='$r[1]' where id=$n");
echo "<br/>";
}
if ($_POST['b3'])
{
$z3=mysql_query("update user set  Wishlist='$r[1]' where id=$n");
echo "<br/>";
}
}


echo "<h3 style=text-align:center><a href=mainpage.php style=color: #43C6DB; text-decoration: 

none;target=_blank> <br/><br/><br/>Return to the main page </a> </h3>";
echo "<br/><br/><br/><br/>";
}
?>

Basically I'm trying to update the "purchase", "cart" and "wishlist" columns in the "user" table in the "online_book_shop" database on the click of a button next to the book (depending on which button is clicked). The page loads but the functionality of the buttons is not working.

I think it'll be very difficult for you to write the code that way.
I'd write something like this :

<?php
session_start();
if(isset($_SESSION['is_user_logged_in']))
{
$x=mysql_connect("localhost","root","");
$y=mysql_select_db("online_book_shop",$x);
$n=$_SESSION['id'];
?>

<html>
<body background=modern-background-design.jpg>
<div align=center>
<h1 style=text-align:center;font-family:fantasy;color:red;font-size:36px> COMPLETE BOOK
DATABASE</h1></br>
<?
$z=mysql_query("select * from books order by serial asc");
?>
</br><br/><br/><br/><table border = 4 bgcolor=white align=center>

So, when you say "What is wrong with this code" What exactly do you mean?? Are you getting an error when you try to run it?

There seem to be no syntax errors but this does not guarantee your code works. So make sure you have your php.ini set to display errors and post the errors that you get. Other tips:

  • Use meaningful names for variables; $x, $y etc do not tell much but are likely to confuse you
  • before using the array values that you have no guarantee they exist, chech if they are set using isset() function; this goes for the $_SESSION, $_POST (or $_GET), etc;
  • if($_POST) is meaningless check, check each element using isset(): if(isset($_POST['b1']))
  • Avoid using inline styles unless really necessary
  • create a function to connect to the database and put this function in safe area where it can not be read by web users and non privileged people; you do not want your password exposed
  • switch to mysqli or PDO since mysql is soon going to be history (deprecated)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.