Aiden Ryan
Aiden Ryan

Reputation: 3

SQL query INSERT not working inserting values into my DB

I'm trying to insert registration data into a database but my php code isn't inserting the values into the DB although I'm not getting any errors either, can someone help me? this is the code i'm currently using:

$connect = mysql_connect("localhost","myusername","mypassword");
mysql_select_db("application");

$queryreg = mysql_query('INSERT INTO users("username","password","email","date") VALUES("$username","$password","$email","$date")');

die ("You Have Been Registered.");

I just need to add the username password email and date into the fields i have specified but it won't work, please someone help!

Upvotes: 0

Views: 5272

Answers (2)

bamana
bamana

Reputation: 1625

Here is a good reference for connecting and doing an insert into a database using PHP and MySQL W3Schools PHP MySQL Insert. From the looks of it you need to remove the "s around username, password, email and date like Haim Evgi mentioned. Also I would try closing the connection if you are finished with it. Here is a code sample from the site:

$con = mysql_connect("localhost","peter","abc123");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("my_db", $con);

mysql_query("INSERT INTO Persons (FirstName, LastName, Age)
VALUES ('Peter', 'Griffin', '35')");

mysql_query("INSERT INTO Persons (FirstName, LastName, Age) 
VALUES ('Glenn', 'Quagmire', '33')");

mysql_close($con);

Upvotes: 0

Haim Evgi
Haim Evgi

Reputation: 125446

change this line

 $queryreg = mysql_query("INSERT INTO users(username,password,email,date)
 VALUES('".$username."','".$password."','".$email."','".$date."')");

check error

if (mysql_errno()) { 
    die('Invalid query: ' . mysql_error());
}

Upvotes: 1

Related Questions