1

I am creating a website for book rental system. Here I do the work of librarian where inserting the book details are done. So here i want to update the availability of the book if the name of the book is already inserted. My query goes like this. And whenever I try to update the availability of the book the value again gets inserted.

$name=$_POST['bname'];
$avail=$_POST['avail'];
$q=mysql_query("select * from table where name='$name'");
if($name==$q){
  $n=("update table where name='$name' set avail='$avail'+1")
}
Igor Borisenko
  • 3,806
  • 3
  • 34
  • 49
Aysha Azura
  • 179
  • 1
  • 2
  • 15

3 Answers3

3

Should be,

UPDATE table 
SET `avail`= `avail` + 1
WHERE name = '$name' 

Warning: Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Rikesh
  • 26,156
  • 14
  • 79
  • 87
1

Your update query is wrong . Use this

"update table set avail = avail+1 where name = '$name'";
Deepu Sasidharan
  • 5,193
  • 10
  • 40
  • 97
1

I think the first query must be change like this:

'SELECT name FROM table WHERE name='.$name

and second one:

'UPDATE table SET avail = avail+1 WHERE name ='.$name
Morteza Tourani
  • 3,506
  • 5
  • 41
  • 48