View previous topic :: View next topic |
Author |
Message |
Athlon_Jedi n00b


Joined: 25 Jun 2003 Posts: 45 Location: Tifton, GA
|
Posted: Fri Apr 02, 2004 4:51 pm Post subject: php,mysql oddness |
|
|
ok , heres the situation,
first i have apache 2.0.48, installed , configured, and running good,
i also have mod_php and mysql running in conjuction to the apache server, both configured and running good,
php communicates properly with mysql through the apache server,
now this is probably more of a coding question than anything,
i have a simple script setup to allow users to join the website that generates a form with feilds for username,password ect, and the form is a bit ugly atm but is generated as expected by php.
now appearantly php is connecting to mysql, and requesting the proper database just fine, but when it comes time for the data in the form to be posted in the database , nothing is happening, ie the table in the database is not being updated by php.
here is the code for the php :
Code: |
<html>
<head>
<title>New Members
</title>
</head>
<body>
<form action="<?=$PHP_SELF?>"
method="post">
<p>New Member Sign Up<br />
<textarea name="User Name" rows="1" cols="1" wrap></textarea>
<textarea name="Password" rows"1" Cols"1" wrap></textarea>
<textarea name= "First Name" rows"1" Cols"1" wrap></textarea>
<textarea name= "Mi" rows"1" cols"1" wrap></textarea>
<textarea name= "Last Name" rows"1" cols"1" wrap></textarea>
<textarea name="Birth Date" rows"1" cols"1" wrap></textarea>
<textarea name= "Zip/Postal Code" rows"1" cols"1" wrap></textarea><br />
<input type="submit" name="submituser" value="SUBMIT" /></p>
</form>
<?php
// Connect to the database server
$dbcnx = @mysql_connect("server", "database",
"password");
if (!$dbcnx) {
echo( "<p>Sorry, the database server is " .
"on a coffie break at this time.</p>"
);
exit();
}
// Select the database
if (! @mysql_select_db("nottelling") ) {
echo( "<p>Now where did I put " .
"that database?.</p >" );
exit();
}
// If a new user has been submitted,
// add it to the database.
if ($submituser == "SUBMIT") {
$sql = "INSERT INTO users SET
User Name='$Username',
Password='$Password',
First Name='$FirstName',
MI='$MI',
Last Name='$LastName',
Birth Date='$BirthDate',
Zip/Postal Code='$Zip/CountryCode'";
if (@mysql_query($sql)) {
echo("<P>Thank you for joining Anime Realm!!!</P>" );
} else {
echo("<P>Sorry, Database Server is not paying attention at the moment: "
.
mysql_error() . "</P>" );
}
}
// When clicked, this link will load this page
// with the new user submission form displayed.
echo("<p><a href='$PHP_SELF?adduser=1'>Join Anime Realm!!!</a></p>");
//endif;
?>
</body>
</html>
|
now i have changed a few things in order to maintain security but the database and table exist in mysql and the account php is using has full permissions for mysql but it will not post the info in the table, what is wrong with my code? _________________ **** GENTOO SYSTEM SPECS ****
COMPAQ PROLIANT 6500
ATI RAGE II C GRAPHICS
INTEL XEON 450 MHZ P II x 1
780 MB PC-100 ECC REGISTERED RAM
SMARTARRAY 3200 DUAL CHANNEL RAID
9.1 GB SEAGATE BARRACUDA SCSI x 2 ( 3 MORE TO BE ADDED)
PURELY GENTOO 1.4 |
|
Back to top |
|
 |
radulucian Apprentice


Joined: 05 Jan 2004 Posts: 151 Location: Bucharest Romania
|
Posted: Fri Apr 02, 2004 9:22 pm Post subject: |
|
|
first problem that i see in your script is the "user name" field that contains a space. i think you have to use quotes in the query if you have field with spaces.
here's what i would do to debug.
install phpmyadmin and every time a query doesn't behave as expected just use echo $query in your script and the copy&paste the query from the screen to the phpmyadmin web interface.
this way you will see what your query looks like and eventually, what your query does or does NOT to your database.
another problem may come from the register_globals setting, that requires you to use $_SERVER['PHP_SELF] instead of $PHP_SELF and $_POST['posted_variable'] instead of $posted_variable.
the above pseudo-debuging method will help you recognize if that is the case.
good luck. _________________ --------------------------
i am gen2 fan too
noone clicks links in the signature
-------------------------- |
|
Back to top |
|
 |
YopWongSapn l33t


Joined: 26 Jan 2004 Posts: 627
|
Posted: Fri Apr 02, 2004 9:26 pm Post subject: |
|
|
Quote: | Code: | $dbcnx = @mysql_connect("server", "database",
"password");
if (!$dbcnx) {
echo( "<p>Sorry, the database server is " .
"on a coffie break at this time.</p>"
); |
|
Try this:
Code: | $dbcnx = @mysql_connect("server", "database", "password") or
die("<p>Sorry, the database server is " .
"on a coffie break at this time.</p>"); |
It's a little easier and shorter . My guess is that the php script is using "database" as the user name when it connects to the server.
Note: I'm not sure whether "database" is the account name you set up for your phpscript, but that's what i'm assuming.
Your problem appears to be in the definition of the $sql variable. You need to put parentheses around the values to insert into the table (you also need to add a ";" after you close the parentheses):
Code: | $sql = "INSERT INTO users SET(
User Name='$Username',
Password='$Password',
First Name='$FirstName',
MI='$MI',
Last Name='$LastName',
Birth Date='$BirthDate',
Zip/Postal Code='$Zip/CountryCode');"; |
_________________ Gentoo...it's like wiping your ass with silk. Or sandpaper. |
|
Back to top |
|
 |
YopWongSapn l33t


Joined: 26 Jan 2004 Posts: 627
|
Posted: Fri Apr 02, 2004 9:34 pm Post subject: |
|
|
radulucian wrote: | first problem that i see in your script is the "user name" field that contains a space. |
He's right. Didn't catch that one before . Field names in mysql should not have spaces. Also, instead of using SET to insert to tables, try this:
Code: | $sql = "INSERT INTO users VALUES(
\"$Username\",
\"$Password\",
\"$FirstName\",
\"$MI\",
\"$LastName\",
\"$BirthDate\",
\"Zip/CountryCode\" );" |
_________________ Gentoo...it's like wiping your ass with silk. Or sandpaper. |
|
Back to top |
|
 |
Athlon_Jedi n00b


Joined: 25 Jun 2003 Posts: 45 Location: Tifton, GA
|
Posted: Fri Apr 02, 2004 11:04 pm Post subject: ok but....... |
|
|
ok makes sense but what goes here:
Code: |
$_POST[whatgoesinhere?] |
as you can tell im new to php  _________________ **** GENTOO SYSTEM SPECS ****
COMPAQ PROLIANT 6500
ATI RAGE II C GRAPHICS
INTEL XEON 450 MHZ P II x 1
780 MB PC-100 ECC REGISTERED RAM
SMARTARRAY 3200 DUAL CHANNEL RAID
9.1 GB SEAGATE BARRACUDA SCSI x 2 ( 3 MORE TO BE ADDED)
PURELY GENTOO 1.4 |
|
Back to top |
|
 |
YopWongSapn l33t


Joined: 26 Jan 2004 Posts: 627
|
Posted: Fri Apr 02, 2004 11:32 pm Post subject: |
|
|
Quote: | ok makes sense but what goes here |
What he is saying is that in order for php to recognize the variables from the form, they must go through a process before you enter them into the databse. In your case you would use the POST method. THere are several ways to do this, but here's my way:
For each of the variables you are trying to pass into mysql, you need to do this (I will use $Username as an example)
Code: | $Username = $HTTP_POST_VARS['User_Name']; |
Note: In this example I changed "User Name" in the form to "User_Name" to prevent parse errors that would result.
Another note: I'm not entirely sure this applies to $PHP_SELF, as I don't use that method often. I've found it's easier to keep track of everything if I make a separate php script to handle the form. My guess would be that it does. _________________ Gentoo...it's like wiping your ass with silk. Or sandpaper. |
|
Back to top |
|
 |
Athlon_Jedi n00b


Joined: 25 Jun 2003 Posts: 45 Location: Tifton, GA
|
Posted: Sat Apr 03, 2004 12:10 am Post subject: ahh i see |
|
|
so basicly the way i am trying to do it is like im presenting the data but saying "you figure out what to do" because i havent told php what it needs to do with it. _________________ **** GENTOO SYSTEM SPECS ****
COMPAQ PROLIANT 6500
ATI RAGE II C GRAPHICS
INTEL XEON 450 MHZ P II x 1
780 MB PC-100 ECC REGISTERED RAM
SMARTARRAY 3200 DUAL CHANNEL RAID
9.1 GB SEAGATE BARRACUDA SCSI x 2 ( 3 MORE TO BE ADDED)
PURELY GENTOO 1.4 |
|
Back to top |
|
 |
Athlon_Jedi n00b


Joined: 25 Jun 2003 Posts: 45 Location: Tifton, GA
|
Posted: Sat Apr 03, 2004 2:24 am Post subject: ok im stuck |
|
|
a quick check with phpinfo indicates register_globals IS infact off so how do i make use of:
$_SERVER['PHP_SELF'] ? _________________ **** GENTOO SYSTEM SPECS ****
COMPAQ PROLIANT 6500
ATI RAGE II C GRAPHICS
INTEL XEON 450 MHZ P II x 1
780 MB PC-100 ECC REGISTERED RAM
SMARTARRAY 3200 DUAL CHANNEL RAID
9.1 GB SEAGATE BARRACUDA SCSI x 2 ( 3 MORE TO BE ADDED)
PURELY GENTOO 1.4 |
|
Back to top |
|
 |
Athlon_Jedi n00b


Joined: 25 Jun 2003 Posts: 45 Location: Tifton, GA
|
Posted: Sat Apr 03, 2004 3:45 pm Post subject: ok got it to werk, sorta |
|
|
ok now its working with one exception, i cant make more than one entry in the table!
im sure there is a variable that controls the number of entrys one can make into a database table but i have no idea what it is!  _________________ **** GENTOO SYSTEM SPECS ****
COMPAQ PROLIANT 6500
ATI RAGE II C GRAPHICS
INTEL XEON 450 MHZ P II x 1
780 MB PC-100 ECC REGISTERED RAM
SMARTARRAY 3200 DUAL CHANNEL RAID
9.1 GB SEAGATE BARRACUDA SCSI x 2 ( 3 MORE TO BE ADDED)
PURELY GENTOO 1.4 |
|
Back to top |
|
 |
YopWongSapn l33t


Joined: 26 Jan 2004 Posts: 627
|
Posted: Sun Apr 04, 2004 12:50 am Post subject: Re: ahh i see |
|
|
Athlon_Jedi wrote: | so basicly the way i am trying to do it is like im presenting the data but saying "you figure out what to do" because i havent told php what it needs to do with it. | Exactly. That's why you need to do the $HTTP_POST_VARS stuff.
Athlon_Jedi wrote: | a quick check with phpinfo indicates register_globals IS infact off so how do i make use of:
$_SERVER['PHP_SELF'] ? | You shouldn't have to worry about the $_SERVER part. $PHP_SELF usually defaults to $_SERVER['PHP_SELF'].
Athlon_Jedi wrote: |
Posted: Sat Apr 03, 2004 10:45 am Post subject: ok got it to werk, sorta
ok now its working with one exception, i cant make more than one entry in the table! | What exactly do you mean? More than one entry at a time? In a given session? I would try putting your code that handles the form in a different file as I suggested before. I've found that $PHP_SELF can be very screwy from time to time. _________________ Gentoo...it's like wiping your ass with silk. Or sandpaper. |
|
Back to top |
|
 |
Athlon_Jedi n00b


Joined: 25 Jun 2003 Posts: 45 Location: Tifton, GA
|
Posted: Sun Apr 04, 2004 5:11 pm Post subject: no |
|
|
what i mean is when i add one user , useing the now seperate form file , it inserts the data in the table as expected, then when i go to add another, i get this returned on the failure page:
"Sorry,the database server isnt paying attention right now: Duplicate entry for key"1"
does that mean my script only supports one user or have i forgotten something in my code to make it add multiple users to the database? _________________ **** GENTOO SYSTEM SPECS ****
COMPAQ PROLIANT 6500
ATI RAGE II C GRAPHICS
INTEL XEON 450 MHZ P II x 1
780 MB PC-100 ECC REGISTERED RAM
SMARTARRAY 3200 DUAL CHANNEL RAID
9.1 GB SEAGATE BARRACUDA SCSI x 2 ( 3 MORE TO BE ADDED)
PURELY GENTOO 1.4 |
|
Back to top |
|
 |
Athlon_Jedi n00b


Joined: 25 Jun 2003 Posts: 45 Location: Tifton, GA
|
Posted: Sun Apr 04, 2004 5:25 pm Post subject: Never Mind |
|
|
Never Mind, i figured out the trouble was in my php i had the feild name as "Username" when in my table it was "UserName" DUH lol.
Now if only i could get the default value for BirthDate right in my table lol  _________________ **** GENTOO SYSTEM SPECS ****
COMPAQ PROLIANT 6500
ATI RAGE II C GRAPHICS
INTEL XEON 450 MHZ P II x 1
780 MB PC-100 ECC REGISTERED RAM
SMARTARRAY 3200 DUAL CHANNEL RAID
9.1 GB SEAGATE BARRACUDA SCSI x 2 ( 3 MORE TO BE ADDED)
PURELY GENTOO 1.4 |
|
Back to top |
|
 |
YopWongSapn l33t


Joined: 26 Jan 2004 Posts: 627
|
Posted: Sun Apr 04, 2004 8:56 pm Post subject: Re: Never Mind |
|
|
Athlon_Jedi wrote: | Never Mind, i figured out the trouble was in my php i had the feild name as "Username" when in my table it was "UserName" DUH lol. |
Yeah, the thrick is to develop a consistent naming scheme for your php variables as well as for your mysql field names. For example, for database names I do something similar to your "UserName" field. For table fields I don't use caps: "user_name". For my php variables my scheme is the same as the field names. That makes it easier to deal with data between php and mysql.
Athlon_Jedi wrote: | Now if only i could get the default value for BirthDate right in my table lol  |
In your form handling code you can set up a quick little function to format the date as it comes in, thus establishing a standardised form for sotring the date. When you want to retrieve the data you can use the same idea to format the date into something readable, depending on how you end up storing the date in the databse. _________________ Gentoo...it's like wiping your ass with silk. Or sandpaper. |
|
Back to top |
|
 |
|
|
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
|
|