944,068 Members | Top Members by Rank

Ad:
  • MySQL Discussion Thread
  • Unsolved
  • Views: 2148
  • MySQL RSS
Oct 1st, 2009
0

Can't insert record into mySQL database

Expand Post »
Hi there

Completely stumped on the simplest of things...

Here is my PHP code:

MySQL Syntax (Toggle Plain Text)
  1. <?php
  2.  
  3. //includes the connection parameters FROM external file
  4.  
  5. require_once('connection.php');
  6.  
  7. // Receive the variables
  8. $fname = $_POST['fname'];
  9. $lname = $_POST['lname'];
  10. $email = $_POST['email'];
  11. $ip = gethostbyname($_SERVER['REMOTE_ADDR']);
  12.  
  13. // Establish connection with database
  14. $connect = mysql_connect($hostname, $username, $password) OR die("Could not connect to localhost");
  15.  
  16. // SELECT database
  17. $select = mysql_select_db($database, $connect) OR die("Could not find database.");
  18.  
  19. // INSERT data to database
  20. $sql = mysql_query("INSERT INTO tc_mailist ('fname', 'lname', 'email', 'ip') VALUES ('Joe', 'Bloggs', 'joe@bloggs.com', $ip)");
  21.  
  22. if(!$sql) {
  23. die("Nope");
  24. } ELSE {
  25. echo "1 record added";
  26. }
  27.  
  28. mysql_close($connect);
  29. ?>

$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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
keira23 is offline Offline
2 posts
since Oct 2009
Oct 1st, 2009
0

Re: Can't insert record into mySQL database

Do not use quotes around column names.

php Syntax (Toggle Plain Text)
  1. $sql = mysql_query("INSERT INTO tc_mailist (fname, lname, email, ip) VALUES ('Joe', 'Bloggs', 'joe@bloggs.com', $ip)");
Reputation Points: 29
Solved Threads: 47
Posting Whiz
mwasif is offline Offline
312 posts
since Dec 2007
Oct 1st, 2009
0

Re: Can't insert record into mySQL database

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['REMOTE_ADDR'];

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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
keira23 is offline Offline
2 posts
since Oct 2009
Oct 1st, 2009
0

Re: Can't insert record into mySQL database

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().
Reputation Points: 29
Solved Threads: 47
Posting Whiz
mwasif is offline Offline
312 posts
since Dec 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in MySQL Forum Timeline: enabling relations in phpmyadmin
Next Thread in MySQL Forum Timeline: MySQL where statement - comparing attributes from two differen tables





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC