Arwen
Arwen

Reputation: 205

Why doesn't my HTML form submit data to MySQL database?

I have two .php files. The first file is the contact.php which contains a contact form, another file is the contact_form.php that contains SQL and PHP statements to send the data to the database. There is a successful connection between my contact_form.php file and the database; however, whenever I write information inside the HTML form on my localhost and do "Submit" I get the following error:

ERROR : Could not find file : views\home\contact_form.php.php

I have two questions:

1) I have my contact_form.php file in the same directory as my contact.php file, that is **views\home** then why does it give me an error that it cannot find the file in that directory?

2) Why in the error message my contact_form.php file has additional .php ending?

Code for the contact.php:

<form class="well" id="contactForm" name="sendMsg" novalidate="" action="contact_form.php" method="post">
        <div class="control-group">
            <div class="controls">
                <input class="form-control" name="fullname" id="name" type="text" placeholder="Full Name" required="" data-validation-required-message="Please enter your name" />
            </div>
        </div>
        <div class="control-group">
            <div class="controls">
                <input class="form-control" name="phonenumber" id="phone" type="text" placeholder="Phone Number" required="" data-validation-required-message="Please enter your phone number" />
            </div>
        </div>
        <div class="control-group">
            <div class="controls">
                <input class="form-control" name="emailaddress" id="email-address" type="email" placeholder="Email Address" required="" data-validation-required-message="Please enter your email" />
            </div>
        </div>
        <div class="control-group">
            <div class="controls">
                <textarea rows="6" class="form-control" name="message" id="msg" type="msg" placeholder="Enter detailed question/concern, and we will get back to you." required="" data-validation-required-message="Please enter your question/concern"></textarea>
            </div>
        </div>
        <div class="control-group">
            <div class="controls submit-btn">
                <button class="btn btn-primary" type="submit" value="Submit">Submit</button>
            </div>
        </div>
    </form>
</div>

contact_form.php:

<?php

define('DB_NAME', 'database_db');
define('DB_USER', 'root');
define('DB_PASSWORD', '');
define('DB_HOST', 'localhost');

$connection = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD);

if(!$connection){
die('Database connection failed: ' . mysqli_connect_error());
}

$db_selected = mysqli_select_db($connection, DB_NAME);

if(!$db_selected){
die('Can\'t use ' .DB_NAME . ' : ' . mysqli_connect_error());
}

echo 'Connected successfully';

$name = @$_POST ['fullname'];
$phone = @$_POST ['phonenumber'];
$email = @$_POST ['emailaddress'];
$msg = @$_POST ['message'];

$sql = "INSERT INTO contact_form (fullname, phonenumber, emailaddress, message) VALUES ('$name',   '$phone', '$email', '$msg')";

if (!mysqli_query($connection, $sql)){
die('Error: ' . mysqli_connect_error($connection));
}
?>

I am aware of the SQL injections and will work on it as soon as I see that everything is working properly.

.htaccess

# EmagidPHP .htaccess
# Every website must have this file in its root web folder

Options +FollowSymlinks
RewriteEngine on

# Apache must be compiled with gzip or deflate for transmission
# compression to work for the following file types
# AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml
# AddOutputFilterByType DEFLATE application/x-javascript
# application/javascript text/javascript text/x-js text/x-javascript
# AddOutputFilter DEFLATE js css htm html xml

# Missing favicon should not cause SkyPHP to load
RewriteRule ^favicon\.ico - [L]

# If the requested file or folder is not found, SkyPHP will check multiple codebases for the   correct file
# Also, we are blocking php files that are in the pages folder from being accessed directly
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} /pages/(.*)\.php$
RewriteRule ^(.+)$ /mywebsite/index.php/$1 [L]

Upvotes: 0

Views: 740

Answers (1)

Shomz
Shomz

Reputation: 37701

Seems like something (most likely .htaccess) is rewriting your URLs. Maybe it's done on purpose to have, for example, the url /home actually go to /home.php. In such case, you need to modify your form action accordingly:

action="contact_form"

UPDATE

It's definitely htaccess. The last three lines. I don't see how that fits into what you've shown in the question. So, if you're definitely not using that sort of redirection, feel free to comment out or remove those three lines (no need to change the action url then). Otherwise, what I originally suggested should work.

Upvotes: 1

Related Questions