4WebHelp
 FAQ  •  Search  •  User Groups  •  Forum Admins  •  Smilies List  •  Statistics  •  Rules   •  Login   •  Register
Toggle Navigation Menu

 Writing Variables
Post New TopicReply to Topic
View Previous Topic Print this topic View Next Topic
Author Message
Kdawg
Senior WebHelper
Senior WebHelper


Joined: 21 Apr 2003
Posts: 153

PostPosted: Mon Apr 21, 2003 1:20 am (21 years ago) Reply with QuoteBack to Top

How would I write a variable to a MySQL database using a php script. Example store the variable $bating to my MySQL database.
OfflineView User's ProfileFind all posts by KdawgSend Personal Message
Daniel
Team Member



Joined: 06 Jan 2002
Posts: 2564

PostPosted: Mon Apr 21, 2003 7:50 am (21 years ago) Reply with QuoteBack to Top

Code:
$sql = "INSERT INTO table_name SET field_name = $bating";


Question

Or do you need the PHP code to connect to a database and run the query too?

________________________________
Image
OfflineView User's ProfileFind all posts by DanielSend Personal Message
Kdawg
Senior WebHelper
Senior WebHelper


Joined: 21 Apr 2003
Posts: 153

PostPosted: Tue Apr 22, 2003 12:10 am (21 years ago) Reply with QuoteBack to Top

Im not exactly sure what i need to do. Im storing baseball stats so I can use them later to get totals.
OfflineView User's ProfileFind all posts by KdawgSend Personal Message
Kdawg
Senior WebHelper
Senior WebHelper


Joined: 21 Apr 2003
Posts: 153

PostPosted: Tue Apr 22, 2003 12:41 am (21 years ago) Reply with QuoteBack to Top

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.
OfflineView User's ProfileFind all posts by KdawgSend Personal Message
Daniel
Team Member



Joined: 06 Jan 2002
Posts: 2564

PostPosted: Tue Apr 22, 2003 6:05 am (21 years ago) Reply with QuoteBack to Top

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 Smile

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).

________________________________
Image
OfflineView User's ProfileFind all posts by DanielSend Personal Message
jayant
Team Member



Joined: 07 Jan 2002
Posts: 262
Location: New Delhi, India

PostPosted: Tue Apr 22, 2003 6:20 am (21 years ago) Reply with QuoteBack to Top

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 (Wink you missed.

________________________________
Jayant Kumar
Member of the 4WebHelp Team
Nibble Guru - Computing Queries Demystified
GZip/ Page Compression Test
OfflineView User's ProfileFind all posts by jayantSend Personal MessageVisit Poster's WebsiteYahoo MessengerMSN Messenger
Kdawg
Senior WebHelper
Senior WebHelper


Joined: 21 Apr 2003
Posts: 153

PostPosted: Tue Apr 22, 2003 9:16 pm (20 years, 12 months ago) Reply with QuoteBack to Top

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)
OfflineView User's ProfileFind all posts by KdawgSend Personal Message
drathbun
WebHelper
WebHelper


Joined: 01 Mar 2003
Posts: 69
Location: Texas

PostPosted: Tue Apr 22, 2003 10:37 pm (20 years, 12 months ago) Reply with QuoteBack to Top

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. Wink 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
OfflineView User's ProfileFind all posts by drathbunSend Personal MessageVisit Poster's Website
Display posts from previous:      
Post New TopicReply to Topic
View Previous Topic Print this topic View Next Topic


 Jump to:   




You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot edit your posts in this forum.
You cannot delete your posts in this forum.
You cannot vote in polls in this forum.


Page generation time: 0.220542 seconds :: 18 queries executed :: All Times are GMT
Powered by phpBB 2.0 © 2001, 2002 phpBB Group :: Based on an FI Theme