1

I have created a form in html and I would like that the dataentered in the form is sent to a mysql database in XAMMP. I created the database, the table and the connectivity.php file that manages the connection to the database and data entry, but when I try I get the following error:

"Fatal error: Uncaught Error: Call to undefined function mysql_connect() in C:\xampp\htdocs\example\connectivity.php:8 Stack trace: #0 {main} thrown in C:\xampp\htdocs\example\connectivity.php on line 8"

I post all the code that I wrote. Does anyone know where I'm wrong?

here is the form index.html

    <!DOCTYPE HTML>
<html>

<head>
    <title>Contact Us</title>
    <link rel="stylesheet" type="text/css" href="style.css">

    <?php include("connectivity.php"); ?>

</head>

<body>
    <div id="contact">
        <h3>Contact Us For Any Query</h3>
        <form method="POST" action="connectivity.php">
            Name
            <br>
            <input type="text" name="name">
            <br> Email
            <br>
            <input type="text" name="email">
            <br> Message
            <br>
            <textarea rows="10" cols="50" maxlength="100" name="message"></textarea>
            <br>
            <input type="submit" value="Send Message">
        </form>
    </div>
</body>

</html>

Then the code used to define database and the table:

CREATE DATABASE practice;
USE practice;

CREATE TABLE contact
(
contactID INT(9) NOT NULL auto_increment,
contactName VARCHAR(40) NOT NULL,
contactEmail VARCHAR(40) NOT NULL,
message VARCHAR(250) NOT NULL,
PRIMARY KEY(contactID)
);

Finally the connectivity.php file:

<?php
//connecting to the database
define('DB_HOST', 'localhost');
define('DB_NAME', 'practice');
define('DB_USER','root');
define('DB_PASSWORD','');

$con=mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or die("Failed to connect to MySQL: " . mysql_error());
$db=mysql_select_db(DB_NAME,$con) or die("Failed to connect to MySQL: " . mysql_error());
//inserting Record to the database
$name = $_POST['name'];
$email = $_POST['email'];
$message =  $_POST['message'];

$query = "INSERT INTO contact(contactName,contactEmail,message)VALUES('$name','$email','$message')";
$result = mysql_query($query);
if($result)
    {
        echo "Successfully updated database";
    }
    else
    {
     die('Error: '.mysql_error($con));
    }
    mysql_close($con);
?>

P.S: I installed the latest version of XAMMP (5.6.15)

$con=mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD,##TABLE NAME##) or die("Failed to connect to MySQL: " . mysql_error());

$query = "INSERT INTO contact(contactName,contactEmail,message)VALUES('$name','$email','$message')"; $result = mysqli_query($con,$query);

3
  • 1
    mysql functions are insecure and depracated, use mysqli or PDO insead Commented Mar 5, 2016 at 11:41
  • 1
    You are probably running new version of PHP, where mysql_ functions have been removed. Question already answered here: stackoverflow.com/questions/12859942/… Commented Mar 5, 2016 at 11:41
  • 1
    you must enable php_mysql module in php.ini Commented Mar 5, 2016 at 12:03

2 Answers 2

1

If you are using one of the latest version of xampp therefore you have to use PDO or MySQLi .

Your have to change your codes to something like this.

Your connectivity page

<?php


    $db = new PDO('mysql:host=localhost;dbname=practice;charset=utf8', 
                  'root', 
                  '',
                  array(PDO::ATTR_EMULATE_PREPARES => false,
                  PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));


?>
<?php

if (isset($_POST['name'])) {

    $name = $_POST['name'];
    $email = $_POST['email'];
    $message =  $_POST['message'];



    $stmt = $db->prepare("INSERT INTO `contact` (contactName,contactEmail,message)
    VALUES (:name, :email, :message)");
    $stmt->bindParam(':name', $name);
    $stmt->bindParam(':email', $email);
    $stmt->bindParam(':message', $message);

    $stmt->execute();

    echo 'added';

}

?>

Your home page

<!DOCTYPE HTML>
<html>

<head>
<title>Contact Us</title>
<link rel="stylesheet" type="text/css" href="style.css">

</head>

<body>
    <div id="contact">
        <h3>Contact Us For Any Query</h3>
        <form method="POST" action="connectivity.php">
            Name
            <br>
            <input type="text" name="name">
            <br> Email
            <br>
            <input type="text" name="email">
            <br> Message
            <br>
            <textarea rows="10" cols="50" maxlength="100" name="message"></textarea>
            <br>
            <input type="submit" value="Send Message">
        </form>
    </div>
</body>

</html>

Hope this helps

Sign up to request clarification or add additional context in comments.

3 Comments

I tried to use your code but the system gave me this error: Fatal error: Uncaught PDOException: SQLSTATE[HY000] [2019] Unknown character set in C:\xampp\htdocs\example\connectivity.php:7 Stack trace: #0 C:\xampp\htdocs\example\connectivity.php(7): PDO->__construct('mysql:host=loca...', 'root', '', Array) #1 {main} thrown in C:\xampp\htdocs\example\connectivity.php on line 7 I verified that in the php.ini file the string extension=php_pdo.dll is uncommented.
@Riccardo i have edited this part and it has worked for me $stmt = $db->prepare("INSERT INTO... sory for the small mistake.
@Riccardo i also changed this part UTF-8 to utf8
1

Firts you see your phpinfo:

<?php
    phpinfo();
?>

Then see in here , php_mysql is enabled or disabled?

If there not php_mysql, change php.ini file: Uncomment extension=php_mysql.dll

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.