0

i am learning php and i am trying to insert data into a database from a form using php. here is the php

<?php
include "connecttodb.php";
 $name = $_POST['name'];
 $email = $_POST['email'];
 $password = $_POST['pass'];

 $query = "INSERT INTO ******* SET username='$name', password='$password', email='$email'";

 mysql_query($query)
 or die('There was an error in your registration');

 echo'Registration Complete!';

 mysql_close();
 ?>

and i keep getting the error 'There was an error in your registration'.

here is the html for it:

<form action='register.php' method='post'>
  Username:<input type='text' name='name'><br/>
  Email:<input type='text' name='email'><br/>
  Password:<input type='text' name='pass'><br/>
  <input type='submit' value='submit'>
  </form>

if anybody could help i would greatly appreciate it

3
Contributors
11
Replies
12
Views
6 Years
Discussion Span
Last Post by hielo
0

There is no "SET" in an insert query. Refer to the query below.


INSERT INTO table_name ( field1, field2,...fieldN )
VALUES
( value1, value2,...valueN );

Your getting the error because your SQL isn't correct.

0

CAN I STILL USE THE

username='$name', password='$password', email='$email'";

?

0

i just used

<?php
include "dbconnect.php";
 $name = $_POST['name'];
 $email = $_POST['email'];
 $password = $_POST['pass'];

 mysql_query("INSERT INTO ******* (id, username, password, email) VALUES ('','$name', '$password', '$email')")
 or die('There was an error in your registration');

 echo'Registration Complete!';

 mysql_close();
 ?>

and got the same error

0

Has your connection script worked on other queries?

If not, the issue may be there but I can't help you because I don't know the environment you're developing in. You'll have to look up the connection errors on Google and try to resolve it that way.

A good debug method is putting the query into a variable, so...

$query = "INSERT INTO ******* (id, username, password, email) VALUES ('','$name', '$password', '$email')";

Then printing the query

print $query;

Then copying and pasting it into the SQL console. It will tell you if there is any issue with the query. I don't see any, but you can use it for future reference.

0

Print the query out like I suggested, copy it, open PHPmyAdmin, open the SQL window, paste the query, execute the query, and if it doesn't give you any errors *and inserts the data, then the error is somewhere else.

When you debug you have to take little steps to ensure that each step is working.

0

assuming the name of your table is Account, try

<?php
include "connecttodb.php";
 $name = mysql_real_escape_string( $_POST['name']);
 $email = mysql_real_escape_string($_POST['email']);
 $password = mysql_real_escape_string($_POST['pass']);

 $query = "INSERT INTO Account( username,password,email) VALUES('$name', '$password', '$email')";

 mysql_query($query)
 or die('There was an error in your registration');

 echo'Registration Complete!';

 mysql_close();
 ?>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.