Re: MySQL insert

From: Date: Wed, 06 Sep 2000 05:47:46 +0000
Subject: Re: MySQL insert
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-15447@lists.php.net to get a copy of this message
In article <NBBBLBKOKPHDPDJHDNMJAEELCGAA.scott@hostiq.com>, scott@hostiq.com ("Scott Webb") wrote: > I want to take a html form input for lname, fname, points, rebounds and > assists. I want to insert these values into the table. I want the total > field to be a calculation of points, rebounds and assists and insert it into > the total field in the players table. Here is my code that is not working: > > $sql = "INSERT INTO players (fname, lname, points, rebounds, total) > VALUES ('$fname', '$lname', '$points', > '($points+($rebounds*2)+($assists*2)')"; Apostrophes in SQL indicate text, date, or time data (and other stuff -- basically anything that ain't a number). Numbers should not have apostrophes. MySQL lets you slide, and takes number in apostrophes, but any other SQL engine will puke on you for it. Break the habit now. Meanwhile, your real problem is then that while MySQL will take '42' and turn it into 42, it is not going to take '12+(5*2)+(10*2)' and turn that into 42 -- It knows all that crap isn't a number, so figures it must be a string, but you can't put a string into a number field. Lose the apostrophes. For future reference, after mysql_query() fails, mysql_error() will return a real nifty message with a reasonably intelligible explanation of what made MySQL not like your SQL. So replace die("Couldn't add record.") with die(mysql_error() . " Couldn't add record.") In the long run, you don't really want mysql_error() output to the end user, but for now, it's invaluable for you to figure out what you're doing. -- Richard Lynch | If this was worth $$$ to you, buy a CD US Customer Support Director | from one of the artists listed here: Zend Technologies USA | http://www.L-I-E.com/artists.htm http://www.zend.com | (this has nothing to do with Zend, duh!)

« previous php.general (#15447) next »