Jump to content

Archived

This topic is now archived and is closed to further replies.

Pudgemeister

*SOLVED* Noob Needs Help

Recommended Posts

Hi All

This is the second time now that i have wrote this out as i accidentally closed the webpage before completing it last time grrrrrrrrrrr.

I am having problems getting info from a form to be posted into my database table.

The database table is called "users".

The fields in the table are "username", "password", "first name", "last name", and "money".

I do not want anything being entered into the money field so ignore that one-unless it is something to d with that which is stopping me from getting infomation posted into the table.

username, password, first name, and last name, are all varchar(20).

they are all in that order with the money field at the end.

These are the three files that have all the code in for posting into these fields:

[b]dbinfo.inc.php[/b]

[!--coloro:#3333FF--][span style=\"color:#3333FF\"][!--/coloro--]<?
$connect=mysql_connect ("localhost", "username", "password") or die ('You cannot connect to the database because: ' . mysql_error());
mysql_select_db ("database");

$disconnect=mysql_close();
?>[!--colorc--][/span][!--/colorc--]


[b]test_mysql.php[/b]

[!--coloro:#3333FF--][span style=\"color:#3333FF\"][!--/coloro--]<?
include("dbinfo.inc.php");

$connect;

echo '<center>Game Testing</center><br><br><br>';
echo '<form action="process.php" method="post">';
echo 'First Name:<input type="text" name="first name"><br>';
echo 'Last Name:<input type="text" name="last name"><br>';
echo 'Username:<input type="text" name="username"><br>';
echo 'Password:<input type="text" name="password"><br>';
echo '<input type="Submit"></form>';
?>[!--colorc--][/span][!--/colorc--]


[b]process.php[/b]

[!--coloro:#3333FF--][span style=\"color:#3333FF\"][!--/coloro--]<?
include ("dbinfo.inc.php");

$connect;

$first=$_POST['first name'];
$last=$_POST['last name'];
$username=$_POST['username'];
$password=$_POST['password'];

$query = "INSERT INTO users VALUES('$username','$password','$first','$last','')";
mysql_query($query);
$disconnect;
?>[!--colorc--][/span][!--/colorc--]


I do know my username and password wtc. but I have removed them for obvious reasons. I have checked them all three times to make sure this isn't the problem.

All three files above are in the same place on the server and yes the server has php and mysql installed obviously.

one thing i noticed while looking in the table "users" is that there is no index defined-could someone explain what this is all about and do I need an index defined?

If Any More Info Is Needed I will Say.

Please Help Soon

[img src=\"style_emoticons/[#EMO_DIR#]/smile.gif\" style=\"vertical-align:middle\" emoid=\":smile:\" border=\"0\" alt=\"smile.gif\" /] Pudgemeister [img src=\"style_emoticons/[#EMO_DIR#]/smile.gif\" style=\"vertical-align:middle\" emoid=\":smile:\" border=\"0\" alt=\"smile.gif\" /]

Share this post


Link to post
Share on other sites
First, you shouldn't use $connect = .... And then simply write $connect. This is likely to result in error, and also strings are not executed (unless you use eval(), but I'd not recommend this

Simply put mysql_connect(...). And remove the $disconnect thing. Each time use require dbinfo.inc.php it will automatically connect to the database, no need to call anything.

Then, remove the $connect and $disconnect from all files. TO disconect, use mysql_close() directly instead.

Also, modify your query to mysql_query($query) or die(mysql_error()) so you can know if anything went wrong.

As for the index, it's some kind of "id" for the tables. People often create an id field, "INT AUTO_INCREMENT PRIMARY KEY".

[a href=\"http://www.mysqlfreaks.com/mysql/manual_MySQL_Optimisation.html#IDX927\" target=\"_blank\"]More information here[/a]

Share this post


Link to post
Share on other sites
Hi Again

I would firstly like to say thank you very much for the fast reply! I never expected to have a reply today.

ANYWAY

I have edited a few things in accordance with your post and have ended up with the files now looking like this:

[b]dbinfo.inc.php[/b]

[!--coloro:#3333FF--][span style=\"color:#3333FF\"][!--/coloro--]<?
mysql_connect ("localhost", "username", "password") or die ('You cannot connect to the database because: ' . mysql_error());
mysql_select_db ("database");
?>[!--colorc--][/span][!--/colorc--]


[b]test_mysql.php[/b]

[!--coloro:#3333FF--][span style=\"color:#3333FF\"][!--/coloro--]<?
echo '<center>Game Testing</center><br><br><br>';
echo '<form action="process.php" method="post">';
echo 'First Name:<input type="text" name="first name"><br>';
echo 'Last Name:<input type="text" name="last name"><br>';
echo 'Username:<input type="text" name="username"><br>';
echo 'Password:<input type="text" name="password"><br>';
echo '<input type="Submit"></form>';
?>[!--colorc--][/span][!--/colorc--]


[b]process.php[/b]

[!--coloro:#3333FF--][span style=\"color:#3333FF\"][!--/coloro--]<?
include ("dbinfo.inc.php");

$first=$_POST['first name'];
$last=$_POST['last name'];
$username=$_POST['username'];
$password=$_POST['password'];

$query = "INSERT INTO users VALUES('$username','$password','$first','$last','')";
mysql_query($query) or die(mysql_error());
mysql_close();
?>[!--colorc--][/span][!--/colorc--]


Now I am getting shown this in big bold letters "no database selected". This happens when i have pressed the select button, and I am taken to process.php.

take a look-the information is there-and I have got all the database info correct-i checked twice again.

sorri to keep asking about such a simple piece of code-what have I done wrong this time?

Pudgemeister

Share this post


Link to post
Share on other sites
Maybe it's not selecting the database. Try to use
mysql_select_db('database') or die(mysql_error())

This will tell you if an error happens when you tried to use mysql_select_db

Share this post


Link to post
Share on other sites
Ok thanx for that-It is now telling me what is wrong lol

the error I am currently getting is:

Access denied for user: username@localhost' to database 'database'

password, username, and database name are all correct!

I don't understand

Pudgemeister

Share this post


Link to post
Share on other sites
Probably because "username" has no privileges or maybe there's not even a user called "username".

If you are using a server package to test on localhost [your own computer], you should try "root" as username and "root" as password (or sometimes leave the password blank).

Share this post


Link to post
Share on other sites
It sounds like your issues are database related. I would recommend that you install phpmyadmin. It is an easy-to-use php app used for managing mysql. You can test your queries outside of the php and make sure that they are all working correctly.

Share this post


Link to post
Share on other sites
Hi All

I would just like to say thank you to you all for your help.

Alothough i Did check my username etc. over and over again-the username was wrong. [img src=\"style_emoticons/[#EMO_DIR#]/unsure.gif\" style=\"vertical-align:middle\" emoid=\":unsure:\" border=\"0\" alt=\"unsure.gif\" /]

I thought I had created Another User-I had only done so PARTIALLY

I was being helped by my host for the whole time when he had a look and gave me the message that The username wasn't totally insstalled.

It is now installed and my database is recieving the information.

TYVM EVERYONE!

Pudgemeister

Share this post


Link to post
Share on other sites
ok I still have a prob with this lol

using the last copies of the files I have posted above could you possible tell me why the database is only recieving the username and password but not first name and last name?

thanx again

Pudgemeister

Share this post


Link to post
Share on other sites
Try to change "first name" to "first_name" and "last name" to "last_name". Avoid using spaces.

Share this post


Link to post
Share on other sites
that cleared the final problem up and it is all now working fine.

Thank you very much everyone!

Pudgemeister

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.