0

Hi there

Completely stumped on the simplest of things...

Here is my PHP code:

<?php

//includes the connection parameters from external file

require_once('connection.php');

// 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) {
	die("Nope");
} else {
	echo "1 record added";
}

mysql_close($connect);
?>

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

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

Thanks!

2
Contributors
3
Replies
6
Views
8 Years
Discussion Span
Last Post by mwasif
0

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

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!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.