i have created a database on a temp site and i am making a form where uses can add or update information to the database that will then be displayed on a page in the site. Here is the code for the form:

<form action="leaderboard.php" mehtod="POST" />
  <p>Points:
    <input type="text" name="Points"/>
    <br>
    First Name:
  <input type="text" name="First Name"/>
  <br>
    Last Name:
  <input type="text" name="Last Name"/>
  <br>
  </p>
  <input type="submit" value="Submit">
<p>&nbsp;</p>

And here is the PHP Script to upload the information to the database.

<?PHP

define('db_name', 'webadmin_leaderboard');
define('db_user', 'webadmin');
define('db_password', 'collide198');
define('db_host', "localhost");

$link = mysql_connect(db_host, db_user, db_password);

if (!$link)	{
	die('Could Not Connect: ' . mysql_error());
}

$db_selected = mysql_select_db(db_name, $link);

if (!$db_selected)	{
		die('can\'t use' . db_name . ': ' . mysql_error());
}

echo 'Connected Successfully';

	

$value = $_POST['Points'];
$sql = "INSERT INTO leaderboard (Points) VALUES ('values')";
$value = $_POST['First Name'];
$sql = "INSERT INTO leaderboard (First Name) VALUES ('values')";
$value = $_POST['Last Name'];
$sql = "INSERT INTO leaderboard (Last Name) VALUES ('values')";


if (!mysql_query($sql))	{
		die('Error: ' . mysql_error());
}

mysql_close();
		
?>

However, i keep getting a sytax error that reads:

Connected SuccessfullyError: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Name) VALUES ('values')' at line 1

Please help me! Its driving me crazy and i know its probably one small little detail im missing.

Recommended Answers

All 3 Replies

$sql = "INSERT INTO leaderboard (First Name) VALUES ('values')"; When a column name contains a space you must quote it using back ticks, like this: $sql = "INSERT INTO leaderboard (`First Name`) VALUES ('values')";

Ok, well that helped fix the syntax error. But now, no information is being posted to the databsa. Each time i try to make an entry, it makes a new line in the database but all the fields are empty. this is the new code:

<?PHP

define('db_name', 'webadmin_leaderboard');
define('db_user', 'webadmin');
define('db_password', 'collide198');
define('db_host', "localhost");

$link = mysql_connect(db_host, db_user, db_password);

if (!$link)	{
	die('Could Not Connect: ' . mysql_error());
}

$db_selected = mysql_select_db(db_name, $link);

if (!$db_selected)	{
		die('can\'t use' . db_name . ': ' . mysql_error());
}

echo 'Connected Successfully';

	

$value = $_POST[`Points`];

$sql = "INSERT INTO leaderboard (Points) VALUES ('$value')";

$value = $_POST[`First Name`];

$sql = "INSERT INTO leaderboard (`First Name`) VALUES ('$value')";

$value = $_POST[`Last Name`];

$sql = "INSERT INTO leaderboard (`Last Name`) VALUES ('$value')";


if (!mysql_query($sql))	{
		die('Error: ' . mysql_error());
}

mysql_close();
		
?>

You cannot have variable names with blanks in HTML forms. Rename your form variables which you retrieve via the $_POST array.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.