| | |
Can't insert record into mySQL database
Please support our MySQL advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Oct 2009
Posts: 2
Reputation:
Solved Threads: 0
Hi there
Completely stumped on the simplest of things...
Here is my PHP code:
$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!
Completely stumped on the simplest of things...
Here is my PHP code:
MySQL Syntax (Toggle Plain Text)
<?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!
•
•
Join Date: Dec 2007
Posts: 190
Reputation:
Solved Threads: 25
Do not use quotes around column names.
php Syntax (Toggle Plain Text)
$sql = mysql_query("INSERT INTO tc_mailist (fname, lname, email, ip) VALUES ('Joe', 'Bloggs', 'joe@bloggs.com', $ip)");
•
•
Join Date: Oct 2009
Posts: 2
Reputation:
Solved Threads: 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['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!
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!
•
•
Join Date: Dec 2007
Posts: 190
Reputation:
Solved Threads: 25
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().
![]() |
Similar Threads
- Import excel contents (.xls format) into mysql database (PHP)
- How insert image into mysql database and retrieve (PHP)
- use singlw query to insert record in diff tables (PHP)
- How to Insert/Edit data from TAG SELECT HTML Form to MySQL Database? (PHP)
- using session variables to insert data to MYSQL Database (PHP)
- PHP and SQL Error (MySQL)
- Mysql database connection code.....urgent..... (ASP.NET)
- Date type problem JSP, mySQL (Java)
- insert values to mysql database (JSP)
Other Threads in the MySQL Forum
- Previous Thread: enabling relations in phpmyadmin
- Next Thread: Need to sum distinct rows, not distinct values
| Thread Tools | Search this Thread |
agplv3 alfresco api artisticlicense aws bizspark breathalyzer camparingtocolumns changingprices cmg communityjournalism contentmanagement contractors copyright count court crm database design developer development distinct drupal dui ec2 email enterprise eudora facebook form foss gnu government gpl greenit groklaw groupware hiring hyperic images innerjoins insert ip joebrockmeier join journalism keyword keywords kickfire laptop law legal license licensing linux maintenance managing mariadb matchingcolumns metron micromanage microsoft microsoftexchange mindtouch montywidenius mozilla multiple music mysql mysqlcolumnupdating mysqldatetimeordermax() mysqlindex mysqlinternalqueries mysqlquery mysqlsearch news open-xchange opendatabasealliance opengovernment opensource oracle penelope php query referencedesign reorderingcolumns resultset saas select sharepoint simpledb sourcecode spotify sql sugarcrm syntax techsupport thunderbird transparency virtualization





