Q:

HTML/PHP form doesn't insert data to MySQL Database

I am new to PHP, and I have been trying to make a small Homework Organiser Application.
The idea is that you can input a Subject and Description and it will be added to a MySQL Database (that's it for now).
I have created a html form:
<form action="insert.php">
        <label for="subj">Subject:</label>
        <br>
        <input type="text" name="subj">
        <br>
        <label for="desc">Description:</label>
        <br>
        <input type="text" name="desc">
        <br>
        <input type="submit" value="Submit" name="submit">
    </form>

and some php code:
<?php
$subject = $_POST['subj'];
$description = $_POST['desc'];
$subject = mysql_real_escape_string($subject);
$description = mysql_real_escape_string($description);

$dbhost = ''; //These are filled in actually
$dbuser = ''; //These are filled in actually
$dbpass = ''; //These are filled in actually
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
    die('Could not connect: ' . mysql_error());
}

$sql = 'INSERT INTO organiser '.
    '(subject,description) '.
    'VALUES ('$subject', '$description')';

mysql_select_db(''); //These are filled in actually
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
    die('Could not enter data: ' . mysql_error());
}
echo "Entered data successfully\n";
mysql_close($conn);
?>

The problem is that the input from the Subject and Description boxes on the HTML form don't go into the MySQL Database. 
However, If I set
'VALUES ('$subject', '$description')';

to
'VALUES ("test", "test")';

it works.
Any help is appreciated!
Thanks!

A:

Your problem is because you are using single quotes in your $sql declaration, 
$sql = 'INSERT INTO organiser '.'(subject,description) '.'VALUES ('$subject', '$description')';

When you use single quotes you are telling PHP that you would like everything within the quotes to be taken literally.
$age = 20; 
$myAge = 'I am $age years';
echo $myAge; 

This would echo I am $age years since you used single quotes. 
However, if you used double quotes instead, 
$age = 20; 
$myAge = "I am $age years";
echo $myAge;

The output would be, 
I am 20 years

Thus, your $sql statement should be (I write it on one line instead), 
$sql = "INSERT INTO organiser (subject,description) VALUES ('$subject', '$description')";
       ^                                                                               ^

If you echo your $sql it would become something along the lines of,  
INSERT INTO organiser (subject,description) VALUES ('Your subject', 'Your description') 

Your use of single quotes within your SQL-statement is correct, you can read more about the subject here: When to use single quotes, double quotes and backticks

