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

Recommended Answers

All 11 Replies

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.

CAN I STILL USE THE

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

?

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

Do the field names match exactly to what you have in the database?

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.

i use phpmyadmin. and why wont it work, if there are no bugs

When you ran the query in the console, did it add the data correctly?

what do you mean?

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.

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();
 ?>
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.