skip to Main Content

I have an HTML form, and I need to past data from it to mysql database in phpMyAdmin. I’m far from php and I don’t know what to do with these errors, also I was unable to set up server and phpMyAdmin locally so I test it right away on the hosting.

index.html:

<section class="form">
        <form class="form__wrapper" action="form.php" method="post">
            <h2 class="form__wrapper_text">Leave your contacts below</h2>
            <div class="form__wrapper_inputs">
                <div class="form__wrapper_inputs_number">
                    <p>Phone</p>
                    <input type="tel" value="+380">
                </div>
                <div class="form__wrapper_inputs_name">
                    <p>Name</p>
                    <input type="text">
                </div>
                <div class="form__wrapper_inputs_company">
                   <p>Company</p>
                   <input type="text">
                </div>
                <div class="form__wrapper_inputs_email">
                    <p>Email</p>
                    <input type="email">
                </div>
                <div class="form__wrapper_inputs_energy" >
                    <p>Consumption</p>
                    <input type="text" pattern="[0-9]*">
                </div>
                <button type="submit" class="form__wrapper_inputs_send">Submit</button>
            </div>

        </form>
    </section>

form.php:

<?php
$dbhost = "***Link to phpmyadmin of my hosting***";
$dbuser = "***phpmyadmin unsername***";
$dbpass = "***phpmyadmin password";
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);

if(! $conn) {
    die('Could not connect: ' . mysqli_error());
}

if(isset($_POST['submit']))
{
    $number = $_POST['number'];
    $name = $_POST['name'];
    $company = $_POST['company'];
    $email = $_POST['email'];
    $consumption = $_POST['consumption'];

    $insert = mysqli_query($db,"INSERT INTO `form`(`number`, `name`, `company`, `email`, `consumption`) VALUES ('$number',
    '$name', '$company', '$email', '$consumption')");

    if(!$insert)
    {
        echo mysqli_error();
    }
    else
    {
        echo "Records added successfully.";
    }
}

mysqli_close($conn);
?>

When I press submit button, I get following errors:

Warning: mysqli_connect(): php_network_getaddresses: getaddrinfo failed: Name does not resolve in /sata2/home/users/***user***/www/***site***/form.php on line 5

Warning: mysqli_connect(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: Name does not resolve in /sata2/home/users/***user***/www/***site***/form.php on line 5

Warning: mysqli_error() expects exactly 1 parameter, 0 given in /sata2/home/users/***user***/www/***site***/form.php on line 8
Could not connect:

In comments I’ve been told that $dbhost variable is wrong, but I’m not sure what to use there. If I assign it IP of my hosting, I get timed out error.

3

Answers


  1. You are connecting to mysql not to phpmyadmin.

    So you should change your credentials like this:

    $dbhost = "***Link to mysql of my hosting***";
    $dbuser = "***mysql username***";
    $dbpass = "***mysql password";
    

    These credentials have been provided to you by your hosting. $dbuser and $dbpass are the same that you use for phpmyadmin but the dbhost should be different

    Login or Signup to reply.
  2. it would be network configuration. The database server is blocking ext-connection to port 3306.

    $dbhost = "***Link to phpmyadmin of my hosting***";
    $dbuser = "***phpmyadmin unsername***";
    $dbpass = "***phpmyadmin password";
    $conn = mysqli_connect($dbhost, $dbuser, $dbpass);
    

    Those info should be:

    $dbhost = "***Database server IP***";
    $dbuser = "***phpmyadmin unsername***";
    $dbpass = "***phpmyadmin password";
    $conn = mysqli_connect($dbhost, $dbuser, $dbpass);
    

    Make sure your server open port 3306 from your webserver.

    Thanks.

    Login or Signup to reply.
  3. index.html (note: you are missing the HTML input element name attributes, so nothing is being sent)

    <section class="form">
            <form class="form__wrapper" action="form.php" method="post">
                <h2 class="form__wrapper_text">Leave your contacts below</h2>
                <div class="form__wrapper_inputs">
                    <div class="form__wrapper_inputs_number">
                        <p>Phone</p>
                        <input name="number" type="tel" value="+380">
                    </div>
                    <div class="form__wrapper_inputs_name">
                        <p>Name</p>
                        <input name="name" type="text">
                    </div>
                    <div class="form__wrapper_inputs_company">
                       <p>Company</p>
                       <input name="company type="text">
                    </div>
                    <div class="form__wrapper_inputs_email">
                        <p>Email</p>
                        <input name="email" type="email">
                    </div>
                    <div class="form__wrapper_inputs_energy" >
                        <p>Consumption</p>
                        <input name="consumption" type="text" pattern="[0-9]*">
                    </div>
                    <button type="submit" class="form__wrapper_inputs_send">Submit</button>
                </div>
    
            </form>
        </section>
    

    form.php

    <?php
    
    // check if form is empty out here before wasting a database connection
    if (!empty($_POST['submit'])) {
    
        $dbname = "database_name"; // you must specify the database name
        $dbhost = "127.0.0.1";
        $dbuser = "database_username"; // add your username here
        $dbpass = "database_password"; // add your password here
    
        // use PDO instead, it is safer than mysqli functions
        // see: https://www.php.net/manual/en/book.pdo.php
    
        // pdo options
        $pdo_options = [
            PDO::ATTR_EMULATE_PREPARES => false,
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
        ];
    
        try {
    
            // connect to database
            $db = new PDO("mysql:host={$dbhost};dbname={$dbname};charset=utf8mb4", $dbuser, $dbpass, $pdo_options);
    
            // use prepared statements to help prevent sql injection
    
            // insert query
            $query = "INSERT INTO `form` (`number`, `name`, `company`, `email`, `consumption`) VALUES (?, ?, ?, ?, ?)";
    
            // prepare statement
            $stmt = $db->prepare($query);
    
            // execute with bound parameters
            $stmt->execute([
                $_POST['number'],
                $_POST['name'],
                $_POST['company'],
                $_POST['email'],
                $_POST['consumption']
            ]);
    
            // success
            echo "Form submitted successfully";
    
        } catch (Exception $e) {
            // error
            echo "Error inserting record into database, " . $e->getMessage();
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search