Jump to content

Forms in PHP and MySql


Recommended Posts


I am trying to write code for an online form in php and mysql so that the form information will be collected in a database in phpmyadmin.

The code is working locally (when I use localhost) but it is not working online when I upload the form onto my website. Can you please tell me what the problem is?

Thank you very much.


Form php code:

           $name = $_POST['name'];
   $company = $_POST['company'];
   $phone = $_POST['phone'];
   $email = $_POST['email'];
   $address = $_POST['address'];

   mysql_connect("localhost", "root", "my_password") or die('Error: '.mysql_error());

   $query="INSERT INTO reseller_pack (name,company,phone,email,address) VALUES ('$_POST[name]','$_POST[company]','$_POST[phone]','$_POST[email]','$_POST[address]')";

   mysql_query($query) or die('Error updating database');
   echo "Database updated";


When I try to run the page on my website, I am getting the following error:


Warning: mysql_connect(): Access denied for user 'root'@'localhost' (using password: YES) in /home/exquip/www/olympicsupport.com/resellerform.php on line 63

Error: Access denied for user 'root'@'localhost' (using password: YES)

Link to comment
Share on other sites

Your issue is exactly what the error message is telling you... you have your username or password incorrect. Make sure your database is set up correctly, and that you are using the correct username/password to access it.


Also, remember to never trust input from the user. You really should be checking to make sure the user hasn't passed any invalid/malicious data into your form. At the very least, you should be using something like htmlspecialchars() (http://php.net/manual/en/function.htmlspecialchars.php) to help protect yourself.

Link to comment
Share on other sites

Thank you for your reply.


It might be a silly question but how is the form working on localhost if the username and password are wrong? Am I missing something basic?


It is updating the database in phpmyadmin when I run the form on localhost but giving error after uploading the form onto my website.


Any help is very much appreciated. Thanks.

Link to comment
Share on other sites

how is the form working on localhost if the username and password are wrong?


It's working on localhost because it is using the correct username/password for your local database setup. Your usernames/passwords for your local machine and your web hosting will be different. I would highly doubt that your hosting gives you a database username of "root".

Link to comment
Share on other sites

I am trying to collect data entered on my website (in php) into the sql databases and tables located on my local phpmyadmin.

Or is there any other correct procedure to collect online form data?

Can anyone please let me know what is the procedure to do this.

Thank you.

Link to comment
Share on other sites

If you want to update the database on your local machine, then you can get the web page form to feed the database on your host's server as it normally would, then export to your own computer's database (manually, I don't know if it can be automated).


If you set up your own computer as the host, then of course the form data will go straight into your computer's database, but I have no idea how to do this and you would no doubt have to set up special arrangements with your ISP because of all the traffic.

Link to comment
Share on other sites

So will the user details be the same as the login details that I use to connect to my website using ftp?? If not, then where will I find the username and password for this code to work?

Thanks a lot.


Ah, I see your confusion. No, you won't be able to use a form on your website to connect to your database on your local computer (unless your local computer is a server and is always connected to the internet). You'll need to create a database on your hosting (usually done through your hosting control panel) and have all the form data stored there. If you want it accessible on your local machine, you'll have to export it from your online database and import it on your local machine (most likely using the Import/Export feature of PHPMyAdmin)

Link to comment
Share on other sites


Thank you once again for all your replies.

I am still struggling with the forms and databases...

How do I create a database on my host? (My website is currenly hosted by a company called Verio). I checked the control panel for the website but I cannot seem to find any database creation tool on it. :(

Is there any other method to create a database for an online form?

Sorry about these basic questions - I am new to all this php/mysql so struggling with the concepts.

Also are there any tutorials that can show the step by step process.

Thanks a lot.

Link to comment
Share on other sites

It looks as if you may need to change your hosting service. Check the emails or letters you got when you signed up; it should mention how many email addresses, domain names, sub-domains, parked domains, databases (if any), upload/download limits it allows and also programs like PHP that it supports.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...