Domanda

I've inserted into databases before but never used the 'where' feature. For some reason, it is not inserting, but dieing instead.

<?php
$member=$_SESSION['member'];

 $SQL = "INSERT into members where name='$member'(money) VALUES ('100')"; 
      mysql_query($SQL) or die("Could not insert money");

      print "Money successfully inserted";
?>
È stato utile?

Soluzione

This is not valid SQL:

INSERT into members where name='$member'(money) VALUES ('100')

I would assume something like this:

update `members` set `money`=100 where `name`='$member'; 

Rationale: (money) is a field and 100 is the value for money (since those 2 make the most sense from a INSERT INTO members (field) VALUES (value) syntax point of view).

Altri suggerimenti

Never die() with a fixed error message, especially when you can output the actual reason: ... or die(mysql_error()).

But yes, your problem is a syntax error. INSERT queries do NOT have a WHERE clause - where is used to filter records already in the database table. This makes no sense for a new record, because it's not IN the table to filtered in the first place.

You query should basically be just

INSERT into members (name, money) VALUES ('$member', '100')

And note that you are vulnerable to SQL injection attacks, and are using a deprecated/obsolete database interface.

If you want to change existing data, use the update command instead of insert.

You can't use WHERE clause with INSERT command

http://dev.mysql.com/doc/refman/5.0/en/insert.html

You have to do an update

<?php
$member=$_SESSION['member'];

 $SQL = "UPDATE `members` SET `money`='100' WHERE `name`='$member'; "; 
      mysql_query($SQL) or die("Could not insert money");

      print "Money successfully inserted";
?>

For inserting :

$SQL = "INSERT INTO members(money) VALUES ('100')";

MySQL INSERT Syntax does not support the WHERE clause. MySQL.com Insert Info

Are you actually trying to insert a new row, or update an existing 'member'? If update, then try:

UPDATE members SET money = 100, WHERE name='$member';
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top