Author |
Message |
Kdawg
Senior WebHelper
Joined: 21 Apr 2003
Posts: 153
|
Posted:
Wed Apr 23, 2003 2:23 am (21 years, 8 months ago) |
|
Could someone write some php for me, what im trying to do is have an html form that someone can enter some baseball stats into and press submit. Then these stats would be saved in a MySQL database. I also would like it so if they went to another web page it would display these stats. |
|
|
|
|
Daniel
Team Member
Joined: 06 Jan 2002
Posts: 2564
|
Posted:
Wed Apr 23, 2003 5:38 am (21 years, 8 months ago) |
|
|
|
|
Kdawg
Senior WebHelper
Joined: 21 Apr 2003
Posts: 153
|
Posted:
Thu Apr 24, 2003 4:25 pm (21 years, 8 months ago) |
|
This is the way I store the stats to the database. Is this right, I cant tell if it is storing them there.
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"); }
$sql = "INSERT INTO Robinson_Stats (Bating) Values ($bating)";
?> |
Also how would I set up the database. If I am storing numbers how would I format it.
Like what would be the type, length/values, attributes, null, default, and extra. Also should I use Primary, Index, Unique, or Fulltext .
I dont have a lot of time and im trying to get this going so any help would be good, and if anyone knows a link to a tutorial or can show me how to retrive and display these variables out of the database I would greatly appriciate it. |
|
|
|
|
Daniel
Team Member
Joined: 06 Jan 2002
Posts: 2564
|
Posted:
Thu Apr 24, 2003 4:30 pm (21 years, 8 months ago) |
|
You're nearly there: you're just missing the code to run your SQL query, after $sql = ...., and the code to select the database you want to use. As to what format your columns should have, it depends on the content. How long are your numbers? |
________________________________
|
|
|
|
jayant
Team Member
Joined: 07 Jan 2002
Posts: 262
Location: New Delhi, India
|
Posted:
Thu Apr 24, 2003 5:19 pm (21 years, 8 months ago) |
|
|
|
|
Kdawg
Senior WebHelper
Joined: 21 Apr 2003
Posts: 153
|
Posted:
Thu Apr 24, 2003 7:29 pm (21 years, 8 months ago) |
|
the longest will probably be 5 digits. |
|
|
|
|
Daniel
Team Member
Joined: 06 Jan 2002
Posts: 2564
|
Posted:
Thu Apr 24, 2003 7:34 pm (21 years, 8 months ago) |
|
INT(5) would be the appropriate column type then, IMO. |
________________________________
|
|
|
|
Kdawg
Senior WebHelper
Joined: 21 Apr 2003
Posts: 153
|
Posted:
Thu Apr 24, 2003 9:27 pm (21 years, 8 months ago) |
|
I found this on the query but I dont think it is put together like i need it to be. It has a lot of things in there and I need help finding out what each one does.
Code: | if ($REQUEST_METHOD=="POST") {
$SQL = " INSERT INTO links ";
$SQL = $SQL . " (category, sitename, siteurl, description) VALUES ";
$SQL = $SQL . " ('$category', '$sitename','$siteurl','$description') ";
$result = mysql_db_query($db,"$SQL",$cid);
if (!$result) { echo("ERROR: " . mysql_error() . "\n$SQL\n"); }
echo ("New Link Added\n");
}
mysql_close($cid); |
|
|
|
|
|
Kdawg
Senior WebHelper
Joined: 21 Apr 2003
Posts: 153
|
Posted:
Thu Apr 24, 2003 9:37 pm (21 years, 8 months ago) |
|
I have a question, this is what my host tripod says
Quote: | Tripod uses a simplified connection method for accessing your database. You do not need any special parameters to access your data. To connect to your MySQL database using a PHP script, use the following code:
$db = mysql_connect() |
does this mean i can replace this
Code: | $usr = "username";
$pwd = "password";
$db = "database";
$host = "localhost";
$cid = mysql_connect($host,$usr,$pwd);
if (!$cid) { echo("ERROR: " . mysql_error() . "\n"); } |
with this
Code: | $db = mysql_connect() |
|
|
|
|
|
Kdawg
Senior WebHelper
Joined: 21 Apr 2003
Posts: 153
|
Posted:
Thu Apr 24, 2003 9:45 pm (21 years, 8 months ago) |
|
Ok, I think I got the storing part, here is what I have.
Code: | <?php
$bating = $_POST[bating];
$db = mysql_connect("localhost","username","password");
mysql_select_db("user_uk_db",$db);
$sql = "INSERT INTO Robinson_Stats (Bating) Values ($bating)";
$result = mysql_query("SELECT bating FROM Robinson_Stats")
or die("Invalid query: " . mysql_error());
?>
|
|
|
|
|
|
jayant
Team Member
Joined: 07 Jan 2002
Posts: 262
Location: New Delhi, India
|
Posted:
Fri Apr 25, 2003 4:59 am (21 years, 8 months ago) |
|
You are not not querring the db to insert the new record.
Code: |
$sql = "INSERT INTO Robinson_Stats (Bating) Values ($bating)";
$result = mysql_query("SELECT bating FROM Robinson_Stats")
or die("Invalid query: " . mysql_error());
|
have following instead:
Code: |
$sql = "INSERT INTO Robinson_Stats (Bating) Values ($bating)";
$result = mysql_query($sql)
or die("Invalid query: " . mysql_error());
$result = mysql_query("SELECT bating FROM Robinson_Stats")
or die("Invalid query: " . mysql_error());
|
|
________________________________ 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:
Fri Apr 25, 2003 2:41 pm (21 years, 8 months ago) |
|
I got it working, now I have another question. How do I overwrite a row, like if the row bating = 12 and the someone enters 4 it would change the 12 to 4 not put the 4 in with the 12. So the row bating would always have 1 number in it. |
|
|
|
|
Darren
Team Member
Joined: 05 Feb 2002
Posts: 549
Location: London
|
Posted:
Fri Apr 25, 2003 3:04 pm (21 years, 8 months ago) |
|
$sql = "UPDATE Robinson_Stats SET Bating = $bating";
I'm sure someone will have a better way but it might give you a clue...
BTW If there is more than one row you need to add a where clause or it will update all rows I believe. |
|
|
|
|
Kdawg
Senior WebHelper
Joined: 21 Apr 2003
Posts: 153
|
Posted:
Fri Apr 25, 2003 5:00 pm (21 years, 8 months ago) |
|
Is it possible to update multiple in 1 statement, like update bating and singles without having to put
Code: | $sql = "UPDATE Robinson_Stats SET Bating = $bating";
$sql = "UPDATE Robinson_Stats SET singles = $singles"; |
|
|
|
|
|
Daniel
Team Member
Joined: 06 Jan 2002
Posts: 2564
|
Posted:
Fri Apr 25, 2003 5:03 pm (21 years, 8 months ago) |
|
Code: | $sql = "UPDATE Robinson_Stats SET Bating = $bating, singles = $singles"; |
|
________________________________
|
|
|
|
|