homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Databases
Forum Library, Charter, Moderator: open

Databases Forum


 3:19 am on Sep 23, 2010 (gmt 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 Sep 23, 2010 (gmt 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 Sep 23, 2010 (gmt 0)

Also, you should sanitize your inputs to avoid SQL Injection [en.wikipedia.org].


 4:15 pm on Sep 23, 2010 (gmt 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)

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...]

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Databases
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved