Welcome to WebmasterWorld Guest from 54.145.209.34

Forum Moderators: open

MySql

Input

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

10+ Year Member



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.

<html>
<head>
Information to login to database here.
</head>

<body>
<?php

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

?>
</body>
</html>

Help!
5:50 am on Sep 23, 2010 (gmt 0)

5+ Year Member



Try this example from php.net.

<html>
<head>
Information to login to database here.
</head>

<body>
<?php

$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());
}

mysql_close($link);

?>
</body>
</html>
3:49 pm on Sep 23, 2010 (gmt 0)

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



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

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



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')";

or

$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)

5+ Year Member



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

Featured Threads

Hot Threads This Week

Hot Threads This Month