1

I have a simple form set up in HTML to pass values to PHP and put them in a MYSQL database. I just can't fathom why nothing is happening when I click the submit button. Previously it was saying 'failed' but now nothing. I have checked the values from the form - fine. I've checked the database connection - fine. I've checked the SQL statement - well, I can't see any errors.

This is my main HTML page

<p class="subtitle">Let me know what you think</p>
<form action="db_insert.php">
<input name="username" placeholder="Name">
<br>
<textarea name="comments" placeholder="Please type your comments here" 
cols=120 rows=5></textarea>
<br>
<input type="button" name="submit" value="submit">
<br>
<p id="commTitle">Comments</p>
<br>
<p id="comment"></p>

This is the PHP

<?php
include 'db_connection.php';

//create database connection 
$conn = OpenCon();
$username = htmlspecialchars($_POST['username']);
$comment = htmlspecialchars($_POST['comment']);

$sql = 'INSERT INTO sitecomments(username, comment) VALUES(:username,:comment)';
$stmt = $conn -> prepare($sql);
$stmt -> bindValue(':username', $username);
$stmt -> bindValue(':comment', $comment);
$q_result = $stmt -> execute();

if($q_result){
echo 'Comment Inserted Successfully';
}
else{
echo 'Failed';
}

db_connection.php looks like this (with credentials removed.

<?php

function OpenCon(){
    //pass the database details to variables
    $host = "localhost";
    $dbuser = "*****";
    $dbpass = "*****";
    $dbname = "*****";
    // combine host and db name in to single variable
    $dbhost = "mysql:host=$host;dbname=$dbname";
    //create PDO from database information
    $dbconn = new PDO($dbhost, $dbuser, $dbpass);
    return $dbconn;
}
?>

As I said, I've checked the database connection and all is fine so where on earth am I going wrong? My database has 3 fields but one is autoincremented so I haven't included it in the query. I tried the query in MyPHPAdmin and it passed ok.

9
  • 2
    It would help if you checked for errors in your code Commented Feb 27, 2019 at 19:32
  • 1
    Don't guess when the query fails. Instead, check for PDO errors Commented Feb 27, 2019 at 19:34
  • You should check $stmt->errorInfo() Commented Feb 27, 2019 at 19:36
  • 2
    Also, not the issue but don't use htmlspecialchars here. Commented Feb 27, 2019 at 19:36
  • output the raw query that would get run, and then review it for problems / test it in mysql workbench or phpmyadmin. Commented Feb 27, 2019 at 19:36

2 Answers 2

1

The first thing I notice is that the input has name of "comments" rather than the $_POST variable you're accessing called comment:

<textarea name="comments" placeholder="Please type your comments here" cols=120 rows=5></textarea>

$comment = htmlspecialchars($_POST['comment']);

Try changing that and see if it fixes the issue.

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

3 Comments

Even undefined it should insert an empty record.
Correct, unless the error occurs when running htmlspecialchars($_POST['comment'])
Glad to hear it @HarrySampson. If you have a chance, can you mark my answer as the Accepted Answer.
0

It would be helpful to handle errors within your code. In your current example if something goes wrong you will have a hard time finding out where the problem is.

You can try all of the following examples from the PHP Docs on PDO error handling and PDO::errorInfo:


Assert your connection is valid:

try {
    $dbh = new PDO($dsn, $user, $password, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING));
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
    exit;
}

Assert your SQL is valid

/* Provoke an error -- bogus SQL syntax */
$stmt = $dbh->prepare('bogus sql');
if (!$stmt) {
    echo "\nPDO::errorInfo():\n";
    print_r($dbh->errorInfo());
}   

As usual the error is a pebcak error, and you need to utilize proper debugging tools to find out where your mistakes are. Good luck!

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.