Hey guys, only new to Daniweb, so pardon me, if i seem too "nooby" for you guys.

I am building a website, and currently am working on the "members area"
i have a database named "users" with fields "name", "username", "email", and "password".
i am running a php script, and haved tested myself (by "echoing" until i find the line that screws up) that this line of code

$sql = mysql_query("INSERT INTO c944978_member_database.users (name, email, username, password)
		VALUES('$name', '$email', '$username', '$db_password'") or die (mysql_error());

when the php file gets to this line it gives this error:

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 '' at line 2

Not sure what this means, have done some googling, and found that it is too do with the SQL Database.. but am very new to this so cant find my error.

my sql database is:

CREATE TABLE users (
  userid int(25) NOT NULL auto_increment,
  name varchar(25),
  email varchar(25),
  username varchar(25),
  password varchar(255),
  PRIMARY KEY  (userid)
) TYPE=MyISAM COMMENT='Membership Information';

I got this as a template from, , where i watched tutorials, and changed some of the code to suit my website.

If somebody can point out, why im getting this error, it would be much appreciated :)

James, Gobble45

Dani AI

Generated

Good quick diagnosis by — the problem was a malformed SQL string in the PHP code (missing closing quote/parenthesis), which makes the database parser hit the end of the statement unexpectedly. is also right to push for a proper DB connection and safer query handling; note that using a fully qualified name like database.table is valid in MySQL, but it’s clearer to select the database first and then use the plain table name.

Troubleshooting checklist:

  • Log the final SQL string (error_log or a debug file) before executing it; that usually reveals missing quotes/parentheses.
  • Check for unescaped characters in values (single quotes inside names/email can break a literal SQL string).
  • Run the exact SQL in a DB client (phpMyAdmin or mysql CLI) to get clearer error positions.
  • Confirm a successful DB connection and correct database selection/privileges.
  • Keep error reporting during development (exceptions or logged errors), and avoid suppressing DB errors until the issue is resolved.
  • Prefer prepared statements and strong password hashing instead of interpolating raw values.

Example (PDO + prepared statement + password hashing):

<?php
$pdo = new PDO('mysql:host=localhost;dbname=c944978_member_database;charset=utf8mb4', 'dbuser', 'dbpass', [
  PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
]);

$hashed = password_hash($plainPassword, PASSWORD_DEFAULT);
$stmt = $pdo->prepare('INSERT INTO users (name, email, username, password) VALUES (:name, :email, :username, :password)');
$stmt->execute([':name'=>$name, ':email'=>$email, ':username'=>$username, ':password'=>$hashed]);

Final notes: migrate from the old mysql* API to mysqli or PDO (mysql* is deprecated), always use prepared statements, and use password_hash/password_verify for account security.

Recommended Answers

All 3 Replies

Nevermind guys, i have asked a friend of mine, and he has shown me the answer;
change the php file to say

$sql = mysql_query("INSERT INTO c944978_member_database.users (name, email, username, password)
  VALUES('$name', '$email', '$username', '$db_password')");

Rather than what was there.
Just so anybody else with this issue, can learn :)

All you are doing here is removing the error message, you are not solving the error itself.

To me it appears that you are trying to user database_name.table_name to connect to your database and insert your data all within your insert query which I have never seen before in all my years of programming.

1st make your connection to the database and then run your query, therefore changing c944978_member_database.users to just users.

All you are doing here is removing the error message, you are not solving the error itself.

To me it appears that you are trying to user database_name.table_name to connect to your database and insert your data all within your insert query which I have never seen before in all my years of programming.

1st make your connection to the database and then run your query, therefore changing c944978_member_database.users to just users.

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.