Sigh, I dunno why no one wants to just answer this guy's question.
This code will work. A reason will be below this:
Code: Select all
<?php
$con = mysql_connect("mysql12.000webhost.com", "a4369339_main", "password");
if(!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("a4369339_btech", $con);
$sql = 'INSERT INTO `a4369339_btech`.`user_data` (`Student ID`, `Forename`, `Surname`, `Class Code`, `Username`, `Password`, `Grade`, `Target Grade`)
VALUES (' . $_POST['fname'] . ',' . $_POST['sname'] . ',' . $_POST['code'] . ',' . $_POST['uname'] . ',' . $_POST['upassword'] . ',' . $_POST['grade'] . ',' . $_POST['tgrade'] . ')';
if (!mysql_query($sql,$con)) {
die('Error: ' . mysql_error());
}
echo "1 record added";
mysql_close($con)
?>
Reasons why it failed:
1) The T_ERROR is used to describe a token error PHP found while parsing your script. It's usually due to an invalid use of quotes, or in your case, a double semicolon...
2) You cannot start a string with a single quote (') and then put a PHP variable inside of it. So when you started your string as $sql = ', right away you gave up your rights to do something like $sql = '$hi, because PHP will not see it. If you were to have started your string with double quotes ("), then you can use regular variables. But even in this case, it may not work as you're calling for an array value which would need to be surrounded by curly braces in a double quote string (ie: $sql = "{$POST['my_key']} ) . However, both methods you can stop the string, append a variable, and then begin the string again by doing something like $sql = "SELECT " . $_POST['my_key'] . " FROM....
Since you were using single quotes, i chose to just append it with single quotes. IE: $sql = 'SELECT ' . $_POST['my_key'] . ' FROM.....
3) When you're going to call an array key, please get in the habit of surrounding your keys with either a single or double quote. PHP will execute without them, but will produce notices about them. Plus, it's kinda an ugly method to use for coding for other reasons, but that's out of this scope.
I hope this helps.