Hi there

Completely stumped on the simplest of things...

Here is my PHP code:


//includes the connection parameters from external file


// Receive the variables
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$ip = gethostbyname($_SERVER['REMOTE_ADDR']);

// Establish connection with database
$connect = mysql_connect($hostname, $username, $password) or die("Could not connect to localhost");

// Select database
$select = mysql_select_db($database, $connect) or die("Could not find database.");

// Insert data to database
$sql = mysql_query("INSERT INTO tc_mailist ('fname', 'lname', 'email', 'ip') VALUES ('Joe', 'Bloggs', 'joe@bloggs.com', $ip)");

if(!$sql) {
} else {
	echo "1 record added";


$sql step doesn't seem to work no matter how I change the formatting.

No literature I've read seems to help. Any ideas?


Recommended Answers

All 3 Replies

Do not use quotes around column names.

$sql = mysql_query("INSERT INTO tc_mailist (fname, lname, email, ip) VALUES ('Joe', 'Bloggs', 'joe@bloggs.com', $ip)");

Thanks for your reply mwasif!!

Ok, I've removed the quotes and at first it still didn't work.

When I tried reducing the fields, I realised the ip address field was stopping the code working. When I just use fname, lname, email ... it works. With ip field, it doesn't.

Not sure why this is, even when I use:
$ip = $_SERVER;

I even changed the field type to INT (unsigned) in the database from VARCHAR (150) but this also doesn't make a difference.

Probably not that important but it would be good to know how to pass ip address to database. :)

Thanks again!

If ip field is varchar then use quotes around $ip in the query. But if you want to insert in INT format then use INET_ATON() to insert and to retrieve INET_NTOA().

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.