Can't insert record into mySQL database

Reply

Join Date: Oct 2009
Posts: 2
Reputation: keira23 is an unknown quantity at this point 
Solved Threads: 0
keira23 keira23 is offline Offline
Newbie Poster

Can't insert record into mySQL database

 
0
  #1
Oct 1st, 2009
Hi there

Completely stumped on the simplest of things...

Here is my PHP code:

  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!
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 190
Reputation: mwasif is an unknown quantity at this point 
Solved Threads: 25
mwasif mwasif is offline Offline
Junior Poster

Re: Can't insert record into mySQL database

 
0
  #2
Oct 1st, 2009
Do not use quotes around column names.

  1. $sql = mysql_query("INSERT INTO tc_mailist (fname, lname, email, ip) VALUES ('Joe', 'Bloggs', 'joe@bloggs.com', $ip)");
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 2
Reputation: keira23 is an unknown quantity at this point 
Solved Threads: 0
keira23 keira23 is offline Offline
Newbie Poster

Re: Can't insert record into mySQL database

 
0
  #3
Oct 1st, 2009
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!
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 190
Reputation: mwasif is an unknown quantity at this point 
Solved Threads: 25
mwasif mwasif is offline Offline
Junior Poster

Re: Can't insert record into mySQL database

 
0
  #4
Oct 1st, 2009
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().
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC