Author |
Message |
Kdawg
Senior WebHelper
Joined: 21 Apr 2003
Posts: 153
|
Posted:
Mon Apr 21, 2003 1:20 am (21 years ago) |
|
How would I write a variable to a MySQL database using a php script. Example store the variable $bating to my MySQL database. |
|
|
|
|
Daniel
Team Member
Joined: 06 Jan 2002
Posts: 2564
|
Posted:
Mon Apr 21, 2003 7:50 am (21 years ago) |
|
Code: | $sql = "INSERT INTO table_name SET field_name = $bating"; |
Or do you need the PHP code to connect to a database and run the query too? |
________________________________
|
|
|
|
Kdawg
Senior WebHelper
Joined: 21 Apr 2003
Posts: 153
|
Posted:
Tue Apr 22, 2003 12:10 am (21 years ago) |
|
Im not exactly sure what i need to do. Im storing baseball stats so I can use them later to get totals. |
|
|
|
|
Kdawg
Senior WebHelper
Joined: 21 Apr 2003
Posts: 153
|
Posted:
Tue Apr 22, 2003 12:41 am (21 years ago) |
|
This is what I have:
form.html
Code: | <html>
<head>
</head>
<body>
<FORM METHOD=post ACTION="save.php">
<INPUT TYPE=text NAME=bating>
<INPUT TYPE=submit>
</FORM>
</body>
</html> |
save.php
Code: | <?php
$bating = $_POST[bating]
$sql = "INSERT INTO Robinson_Stats SET Batting = $bating";
?> |
and I get this error
Code: | Parse error: parse error in save.php on line 3 |
Im trying to store $bating to a MySQL database.
Do i need to create the table somehow? if so please let me know how. |
|
|
|
|
Daniel
Team Member
Joined: 06 Jan 2002
Posts: 2564
|
Posted:
Tue Apr 22, 2003 6:05 am (21 years ago) |
|
I suggest you read the following tutorial before you start:
http://www.blazonry.com/scripting/linksdb/index.php
Then you'll be able to get a good idea of what you're heading for. It's not as simple as you might like it to be
Just as a hint, yes, you do need to create the table, and you need to connect to your database (http://www.php.net/manual/en/function.mysql-connect.php) as well as select it (http://www.php.net/manual/en/function.mysql-select-db.php). |
________________________________
|
|
|
|
jayant
Team Member
Joined: 07 Jan 2002
Posts: 262
Location: New Delhi, India
|
Posted:
Tue Apr 22, 2003 6:20 am (21 years ago) |
|
Kdawg wrote: | This is what I have:
form.html
Code: | <html>
<head>
</head>
<body>
<FORM METHOD=post ACTION="save.php">
<INPUT TYPE=text NAME=bating>
<INPUT TYPE=submit>
</FORM>
</body>
</html> |
save.php
Code: | <?php
$bating = $_POST[bating]
$sql = "INSERT INTO Robinson_Stats SET Batting = $bating";
?> |
and I get this error
Code: | Parse error: parse error in save.php on line 3 |
Im trying to store $bating to a MySQL database.
Do i need to create the table somehow? if so please let me know how. |
Parse error is due to :
$bating = $_POST[bating];
notice the semi-colon ( you missed. |
________________________________ Jayant Kumar
Member of the 4WebHelp Team
Nibble Guru - Computing Queries Demystified
GZip/ Page Compression Test |
|
|
|
Kdawg
Senior WebHelper
Joined: 21 Apr 2003
Posts: 153
|
Posted:
Tue Apr 22, 2003 9:16 pm (21 years ago) |
|
This is what I have and im geting a parse error an line 10. What is the problem?
Code: |
<?php
$bating = $_POST[bating];
$usr = "username";
$pwd = "password";
$db = "database";
$host = "localhost";
$cid = mysql_connect($host,$usr,$pwd);
if (!$cid) { echo("ERROR: " . mysql_error() . "\n"); }
INSERT INTO Robinson_Stats
(bating) VALUES
($bating);
?> |
(not acutal database name, username, and pass) |
|
|
|
|
drathbun
WebHelper
Joined: 01 Mar 2003
Posts: 69
Location: Texas
|
Posted:
Tue Apr 22, 2003 10:37 pm (21 years ago) |
|
Kdawg wrote: | save.php
Code: | <?php
$bating = $_POST[bating]
$sql = "INSERT INTO Robinson_Stats SET Batting = $bating";
?> |
|
Your insert statement is not formatted correctly. Assuming your table name is correct, you would do this:
Code: | INSERT INTO Robinson_Status (Batting) Values ($bating); |
However, that doesn't provide for any key values or any other elements of your table. I think you need to learn some SQL first. Once you learn that, it's going to be much easier to write your php pages.
Dave |
________________________________ Dave
Photography Site :: Query Tools Forum :: Weekend Fun |
|
|
|
|
Page generation time: 0.133813 seconds :: 18 queries executed :: All Times are GMT
Powered by
phpBB 2.0
© 2001, 2002 phpBB Group :: Based on an FI Theme