Search code examples
phpmysqldatabasedatabase-connectivity

PHP MySQL form submission error


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


Solution

  • 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.