Need help in submitting a contact form to a phpmyadmin database

I’ve tried using this tutorial but it doesn’t work:

I’ve copied and pasted 100% of the code so there should be no problems. Though after running a test i get the error message saying couldn’t enter data:

Here’s my code,
HTML:

<div class="morecontent_holders">
    <div class="text1">
        <form action="thankyou.php" method="post">
        Name:<br>
        <input type="text" name="u_name" required><br>

        Email:<br>
        <input type="email" name="u_email" required><br>

        Subject:<br>
        <input type="text" name="subj" required><br>

        Message:<br>
        <input type="text" name="message" required><br>
        <input type="submit" value="Submit"><br>
</form>
    </div>

PHP file named connection.php:

<?php
 
 
function Connect()
{
 $dbhost = "localhost";
 $dbuser = "root";
 $dbpass = "";
 $dbname = "responses";
 
 // Create connection
 $conn = new mysqli(servername, username, password, dbname) or die($conn->conne$
 
 return $conn;
}
 
?>

second php file named thankyou.php:

<?php

require 'connection.php';
$conn    = Connect();
$name    = $conn->real_escape_string($_POST['u_name']);
$email   = $conn->real_escape_string($_POST['u_email']);
$subj    = $conn->real_escape_string($_POST['subj']);
$message = $conn->real_escape_string($_POST['message']);
$query   = "INSERT into tb_cform (u_name,u_email,subj,message) VALUES('" . $nam$
$success = $conn->query($query);

if (!$success) {
    die("Couldn't enter data: ".$conn->error);

}

echo "Thank You For Contacting Us <br>";

$conn->close();

?>

Hi hammer welcome to the forum

It doesn’t look that way to me.
Compare the INSERT lines.

Please note that although that tutorial may be OK (and that is debatable) as it is for beginning localhost learning purposes, it is something that is not suitable for use on a live site.

I wouldn’t even consider it suitable for that - both the HTML and PHP need to be thrown away and redone properly before use. Both are good examples of how NOT to code.

2 Likes

Note take I will remove them, could you refer to me anything similar that is suitable and works.
Thank you

lol ninja’d

I just edited my post to reflect that.

And I agree, depending on what is supposedly being taught learning something that will need to be unlearned is of highly questionable value.

About the only value I can see with the tutorial is that it can provide “low hanging fruit” evidence that the database can be successfully connected to.

It does not connect that’s the thing, I ran it and it came back with an error it’s why I’ve posted this topic in the first place.
If you know of any tutorials i could follow which is similar to this, is coded a hell of alot better and actually works please post.

Yeah for some reason nano didn’t want to select everything but trust me 100% of the code is there.

Well the code for connecting to the database is the only part of what you posted that is coded correctly - so if it isn’t connecting then the problem must lie elsewhere (such as the user or password not matching)

Well i can login to phpmyadmin with the same U&P from what the tutorial said it’ll be the same U&P for phpmyadmin and the connection.php, unless that tutorial is wrong? (Note that i’ve removed the password from the above example)

My guess is you are XAMPP (or similar) as your localhost server and that although Apache has started MySQL hasn’t

When you look at the XAMPP UI doesn’t it show both as running?

MySQL must be running since my phpmyadmin allows me to create a database and fill tables.

What error message do you get from the call to connect to the database?

What does var_dump($conn) produce immediately after that call?

Well once i enter information in the forms i click submit and i got this error, Couldn’t enter data:

That is after all the junk code that you have between the connection call. You should first check if the connection is actually working and then replace that junk code with proper validation for the inputs and then a proper database call using prepare/bind/execute in place of query.

Even if you are just going to continue testing the junk code we still need to know whether the connection actually worked and what the query actually looks like (as you didn’t post the query code properly above).

From reading the reply’s above I’ve removed the php code from my webserver, and I’ve tested SQL commands within phpmyadmin and data does submit into the database. So atleast I know for sure phpmyadmin works. Though if you know of any good tutorials please link them, I’ll be most grateful.

Try running a variation of this (with your connect values hard-coded into the mysqli_connect line

http://php.net/manual/en/function.mysqli-connect.php

<?php
$link = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db");

if (!$link) {
    echo "Error: Unable to connect to MySQL." . PHP_EOL;
    echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
    echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
    exit;
}

echo "Success: A proper connection to MySQL was made! The my_db database is great." . PHP_EOL;
echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;

mysqli_close($link);
?>

Yeah as expected I got the successful connection: Success: A proper connection to MySQL was made! The my_db database is great. Host information: 127.0.0.1 via TCP/IP, I think what the problem is that the tutorial was based on an older version of php, im currently using php7.

Thanks for checking, one step closer now.

It could be, but although I see code I wouldn’t use, nothing jumps out at me as not being compatible.

Try this to make sure the table is there as spelled.

<?php
$link = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db");

if (!$link) {
    echo "Error: Unable to connect to MySQL." . PHP_EOL;
    echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
    echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
    exit;
}

echo "Success: A proper connection to MySQL was made! The my_db database is great." . PHP_EOL;
echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;

/* change my_db to your datavase name */
$query = "SHOW TABLES FROM my_db";
if ($result = $link->query($query)) {
    while ($row = $result->fetch_row()) {
      var_dump($row);
    }
    $result->close();
}

mysqli_close($link);
?>

I got the response array(1) { [0]=> string(8) “tb_cform” }
Which tb_cform is in the database.

It might be the INSERT is failing because the same information is already in it?

One last try for now

<?php
$link = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db");

if (!$link) {
    echo "Error: Unable to connect to MySQL." . PHP_EOL;
    echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
    echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
    exit;
}

echo "Success: A proper connection to MySQL was made! The my_db database is great." . PHP_EOL;
echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;

$query = "SELECT * FROM tb_cform";
if ($result = $link->query($query)) {
    while ($row = $result->fetch_row()) {
      var_dump($row);
    }
    $result->close();
} else {
  echo "no result";
}


mysqli_close($link);
?>

I normally avoid using the " * - everything " preferring to list the fields I’m interested in, but I don’t know what they might be.

re a tutorial, I’ve tried a bit to find one that’s up-to-date and worthy of recommending but haven’t had any luck yet.