Welcome to WebmasterWorld Guest from

Forum Moderators: open

Message Too Old, No Replies



3:19 am on Sep 23, 2010 (gmt 0)

Preferred Member

10+ Year Member

joined:Apr 1, 2005
votes: 0

Greetings all

I'm trying to input data to a table but I'm not having any luck.

A MySql database has been created and I've been able to put a table in it with two fields titled "Username" and "Password."

Now I'm trying to put data into the table. But I'm not having any luck.

Below is the most recent code I've tried.

Information to login to database here.


$sql = INSERT INTO aaa (username,password)
values (User3, Carpet);


5:50 am on Sept 23, 2010 (gmt 0)

Junior Member

10+ Year Member

joined:June 2, 2005
votes: 0

Try this example from php.net.

Information to login to database here.


$sql = 'INSERT INTO aaa (username,password) ' .
'values (User3, Carpet)';
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
if (!mysql_select_db('database_name')) {
die('Could not select database: ' . mysql_error());
$result = mysql_query($sql);
if (!$result) {
die('Could not query:' . mysql_error());


3:49 pm on Sept 23, 2010 (gmt 0)

Moderator from US 

WebmasterWorld Administrator lifeinasia is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Dec 10, 2005
votes: 99

Also, you should sanitize your inputs to avoid SQL Injection [en.wikipedia.org].
4:15 pm on Sept 23, 2010 (gmt 0)

Senior Member

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 28, 2004
votes: 0

The problem is most likely you are trying to insert values into textual fields (varchar, text, etc.) and textual fields need to be quoted. Second, you are probably having PHP errors:

$variable = something; // this references a constant if valid, otherwise it's an error.

$variable = "something"; // ok

Note that double quotes will interpolate other variables, single quotes will not, which you use is dependent on what you need.

$foo = 'This or that';
$variable = "He said $foo"; // prints "He said This or that"
$variable = 'He said $foo'; // prints "He said $foo"

So your two options are

$sql = "INSERT INTO aaa (username,password)
values ('User3', 'Carpet')";


$user = mysql_real_escape_string('User');
$occupation = mysql_real_escape_string('Carpet');

$sql = "INSERT INTO aaa (username,password)
values ('$user', '$occupation')";

Note the internal single quotes are for mySQL, they do not affect the variables. A synonymous select,but note the internal quotes must be escapes to encapsulate the variable (PHP):

$sql = "INSERT INTO aaa (username,password)
values (\"$user\", \"$occupation\")";

This starts to have relevance when the values you insert have quotes.
9:49 am on Oct 20, 2010 (gmt 0)

Junior Member

5+ Year Member

joined:Dec 2, 2008
votes: 0

Looks like you are missing lots of things, although I can't tell if you've stripped some of it out for the purpose of posting it here. In any case, you need to:

  1. Have an HTML form that will post the username and password to the PHP script.

  2. Construct the query using the values from the form.

  3. Make sure the values from the form are sanitised and quoted.

  4. Make a database connection.

  5. Actually run the query.

Follow this tutorial to get started properly: [w3schools.com...]

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members