-2

I need help with this simple PHP/MySQL Form Submission.Following is the code and error i'm getting. Everything's fine with PHP Script. It seems Mysql database connection is giving me problems.

   mysql_connect("localhost","root","admin");//database connection
   mysql_select_db("employees");

   //inserting data order
   $order = "INSERT INTO data_employees (name, address)
        VALUES ('$name','$address')";

   //declare in the order variable
   $result = mysql_query($order);   //order executes

   if($result)
   {
      echo("<br>Input data is succeed");

   }   else{
       echo("<br>Input data is fail");
   }

Following is the ERROR Message i get: Parse error: syntax error, unexpected '?' in C:\xampp\htdocs\Input.php on line 12.

Once I fill in the form details and submit it sometimes i get errors saying "Warning: mysql_connect(): Access denied for user 'root'@'localhost' (using password: YES) in C:\xampp\htdocs\form_input.php on line 4."

"Notice: Undefined variable: name & address in C:\xampp\htdocs\form_input.php on line 11 Input data is fail".

Your solution, help is appreciated. Thank You

ZZ-bb
  • 2,157
  • 1
  • 24
  • 33
user2306388
  • 11
  • 1
  • 4
  • Where are the variables $name and $address coming from? – fullybaked Apr 22 '13 at 08:13
  • Well user will give input from HTML form & this variables will hold those values. For your reference here is the link:http://www.phpeveryday.com/articles/PHP-MySQL-Creating-Form-Insert-Data-P280.html – user2306388 Apr 22 '13 at 09:11
  • A brief glance at that page looks as though it expects register globals to be on (an old setting in PHP that has long been a known security issue and has been turned off and removed in newer versions). Check out some of the answers below for more information. – fullybaked Apr 22 '13 at 09:26
  • Oh thank you so much for the information, i had given everything,I've checked this over 100 times and for the life of me i cannot figure out what the problem was. So how do i go ahead to make this small script & html form get working ? – user2306388 Apr 22 '13 at 10:07

3 Answers3

0

For Parse error

Change your echo statement as following in both condition:

echo "<br>Input data is succeed" ;

For Notice: Undefined variable: name & address is very much clear from message.

$name & $address is not defined anywhere.

May be you would like to take it from request.

$name = $_REQUEST['name']; //Name of element in the form
$address = $_REQUEST['address'];

mysql_connect(): Access denied for user

Check your connection string contains correct username & password.


Note: Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Rikesh
  • 26,156
  • 14
  • 79
  • 87
  • Well i think i haven't set any Username , Password in MySQL because i believe bydefault its blank & doesn't hold any value, right ? OR can you guide me how to set it in PHPMyAdmin ? – user2306388 Apr 22 '13 at 09:15
  • I tried this code as per your message still getting same Error on Line 12:-> $name = $_REQUEST['name']; //Name of element in the form $address = $_REQUEST['address']; $order = "INSERT INTO data_employees (name, address) VALUES ('$name','$address')"; – user2306388 Apr 22 '13 at 09:30
  • SUCCESS !! Finally executed this form submission with PHP & MySQL database connectivity issue is resolved. SOLUTION: I CREATED new user and assigned them new Username,Password & had given all privileges just to test it.And i updated MySQL with MySQLi_Connect & fired queries & entered data in HTML Form and data gets added in MySQL :) Thanks for all your valuable efforts & inputs.Thank you very much :) – user2306388 Apr 22 '13 at 12:03
  • @user2306388 - Glad to help you. Please don't forget to accept my answer by clicking the checkmark to the left of it. – Rikesh Apr 22 '13 at 12:03
0

Your problem is here

$order = "INSERT INTO data_employees (name, address)
    VALUES ('$name','$address')";

you have not defined

$name and $address variables.

please define them.

chandresh_cool
  • 11,753
  • 3
  • 30
  • 45
  • Well i think i haven't set any Username , Password in MySQL because i believe bydefault its blank & doesn't hold any value, right ? OR can you guide me how to set it in PHPMyAdmin ? – user2306388 Apr 22 '13 at 09:14
  • I tried adding those 2 lines by defining "name & address" but getting the same Error on LINE 12:---> $name = $_REQUEST['name']; //Name of element in the form $address = $_REQUEST['address']; $order = "INSERT INTO data_employees (name, address) VALUES ('$name','$address')"; – user2306388 Apr 22 '13 at 09:32
  • There are too many mistakes in your code. And I tried to mark out as much as possible. Cleaned that up & debug the same agin. – Rikesh Apr 22 '13 at 09:34
  • SUCCESS !! Finally executed this form submission with PHP & MySQL database connectivity issue is resolved. SOLUTION: I CREATED new user and assigned them new Username,Password & had given all privileges just to test it.And i updated MySQL with MySQLi_Connect & fired queries & entered data in HTML Form and data gets added in MySQL :) Thanks for all your valuable efforts & inputs.Thank you very much :) – user2306388 Apr 22 '13 at 12:03
  • Cheers and Happy coding. Can u upvote for answer if u found it helpful. Thanks in advance. :) – chandresh_cool Apr 22 '13 at 12:04
0

Looking at your MySQL error

Warning: mysql_connect(): Access denied for user 'root'@'localhost' (using password: YES) in C:\xampp\htdocs\form_input.php on line 4

This is simply referring that you have the wrong username/password combination to access the database.

Also noting your other error regarding variables

Notice: Undefined variable: name & address in C:\xampp\htdocs\form_input.php on line 11

This is referring that the variable has not been declared. If these are what is being posted by your form, it is better to use the $_POST superglobal over having the PHP config allow setting POST data as normal variables (ie. $_POST["name"] vs $name). This allows greater flexibility with naming variables as you won't have conflicts between the posted values and normal variables in your PHP code.

It would be useful to see "Input.php" of your code too for the parse error that you mentioned.

Useful links:

PHP Docs on $_POST

PHP Docs on mysql_connect function

Turnerj
  • 4,258
  • 5
  • 35
  • 52
  • Well i think i haven't set any Username , Password in MySQL. I think bydefault its blank & doesn't hold any value, right ? OR can you guide me how to set it in PHPMyAdmin ? – user2306388 Apr 22 '13 at 09:13
  • I tried adding those 2 lines by defining "name & address" but getting the same Error on LINE 12:---> $name = $_REQUEST['name']; //Name of element in the form $address = $_REQUEST['address']; $order = "INSERT INTO data_employees (name, address) VALUES ('$name','$address')"; – user2306388 Apr 22 '13 at 09:32
  • SUCCESS !! Finally executed this form submission with PHP & MySQL database connectivity issue is resolved. SOLUTION: I CREATED new user and assigned them new Username,Password & had given all privileges just to test it.And i updated MySQL with MySQLi_Connect & fired queries & entered data in HTML Form and data gets added in MySQL :) Thanks for all your valuable efforts & inputs.Thank you very much :) – user2306388 Apr 22 '13 at 12:04
  • Great job. Make sure to post another question if you have a programming issue that you are struggling with. – Turnerj Apr 22 '13 at 13:10