RE: [PHP] MySQL insert
| From: | Jason Murray | Date: | Wed, 06 Sep 2000 05:36:23 +0000 |
| Subject: | RE: [PHP] MySQL insert | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-15429@lists.php.net to get a copy of this message | ||
> 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)')";
Point 1 - I'm going to format this nicely for you, to make debugging easier:
$sql = "INSERT INTO players (fname, lname, points, rebounds, total) VALUES "
." ('$fname', '$lname', '$points', "
."'($points+($rebounds*2)+($assists*2)')";
So now we can see where PHP code should be executing, and where it should be
echoing. Remember you can't calculate inside "" marks, so then we get:
$sql = "INSERT INTO players (fname, lname, points, rebounds, total)"
."VALUES ('$fname', '$lname', '$points', "
."'".($points+($rebounds*2)+($assists*2)."')";
If your total column is a text field, this will work. If it's not a text
field, you'll need to eliminate the '' marks around the line that calculates
the total. Basically, you can't calculate inside an echo "", you need to
escape the " marks.
Jason