hosted by liquidweb


Go Back   Web Hosting Talk : Web Hosting Main Forums : Programming Discussion : Error inserting into database
Reply

Programming Discussion Discussions related to web programming languages and other related issues. Topics may include configuration, optimization, practical usage and database connectivity.
Forum Jump

Error inserting into database

Reply Post New Thread In Programming Discussion Subscription
 
Send news tip View All Posts Thread Tools Search this Thread Display Modes
  #1  
Old 02-28-2006, 06:02 PM
Jeanco Jeanco is offline
Web Hosting Guru
 
Join Date: Aug 2003
Posts: 266

Error inserting into database


Hello,

It's been a while since I've used a mySQL database, and unfortunately my handy reference book isn't with me. Basically, I'm wondering why I'm getting an error when I run the insert query seen below (note this is only the snippet):

PHP Code:
    //if all is good, send it to the database
    
if ($valid) {
        
$email $_POST["email"];
        
$password md5($_POST["password"]);
        
$alias $_POST["alias"];
        
$fanme $_POST["fname"];
        
$lname $_POST["lname"];
        
$address1 $_POST["address1"];
        
$address2 $_POST["address2"];
        
$city $_POST["city"];
        
$province $_POST["province"];
        
$postal $_POST["postal"];
        
$country $_POST["country"];
        
$bdate $_POST["month"] . "/" $_POST["day"] . "/" $_POST["year"];
        
$mailinglist $_POST["mailinglist"];        

        echo 
$email "<br>" $password "<br>" $alias "<br>" $fname "<br>" $lname "<br>" $address1 "<br>" $address2 "<br>" $city "<br>" $province "<br>" $postal "<br>" $country "<br>" $bdate "<br>" $mailinglist "<br>";
        
        
$query "INSERT INTO users (email, password, alias, fname, lname, address1, address2, city, province, postal, country, bdate, mailinglist) VALUES ('$email', '$password', '$alias','$fname', '$lname', '$address1', '$address2', '$city', '$province', '$postal', '$country', '$bdate', '$mailinglist')";
        
        
mysql_query($query) or die('Error, insert query failed');

        
$query "FLUSH PRIVILEGES";
        
mysql_query($query) or die('Error, flush failed');
         
        echo 
"User Added"
    
    } 
Please note that I connect to the database fine, its just inserting thats causing a problem at the moment. The database is set with the following fields. All fields are text and not null with the exception of ID, which is not null, primary key, auto-increment, and integer type.

ID (primary key and set to auto-increment)
email
password
alias
submissions
fname
lname
address1
address2
city
province
postal
country
bdate
mailinglist

Thanks in advance.

Reply With Quote


Sponsored Links
  #2  
Old 02-28-2006, 07:49 PM
ThatScriptGuy ThatScriptGuy is offline
Web Hosting Master
 
Join Date: Feb 2003
Location: AR
Posts: 2,370
Not 100% sure on this one, but try removing the single quotes from the values. I don't think you need them since they are inside the interpreted double quotes of the query.

__________________
Kevin Cackler
Tech Daddies - A Conway, Arkansas based development company.
501-205-1512

Reply With Quote
  #3  
Old 02-28-2006, 08:43 PM
Jeanco Jeanco is offline
Web Hosting Guru
 
Join Date: Aug 2003
Posts: 266
I'm afraid thats not it.

Any other ideas?

Reply With Quote
Sponsored Links
  #4  
Old 02-28-2006, 10:01 PM
zoid zoid is offline
Hail to the Meerkat
 
Join Date: Aug 2001
Posts: 3,231
This is just a guess, but it might be the format of $bdate which might not be compatible with MySQL's date type (given its not a varchar column).

Which error do you actually get (-> mysql_error())?

__________________
Sitemeer.com ● The 1-Click Uptime Check
Ensuring service availability since 1776
Visit the meerkat at fb.com/sitemeer


Reply With Quote
  #5  
Old 03-01-2006, 01:16 AM
saidev saidev is offline
Newbie
 
Join Date: Dec 2005
Posts: 14
Not sure how your server is setup, but here is something else to try, call addslashes() to take care of all the escape characters.
i.e.
$alias = addslashes($_POST["alias"]);
$fanme = addslashes($_POST["fname"]);

__________________
CRONHosting.com | ClickThese Web Directory | NextLevelArcade.com | AllTherapyTalk.com |

Reply With Quote
  #6  
Old 03-01-2006, 07:19 AM
kajakske kajakske is offline
Newbie
 
Join Date: Dec 2002
Location: Gent, Belgium
Posts: 14
Another hint is to actually display what error you get ... it will most likely tell you alot more than we can.

And right before executing it, print out $query, so you see what gets sent to the SQL server.

__________________
Hawkfield: web development and design

Reply With Quote
  #7  
Old 03-01-2006, 08:14 AM
Oras Oras is offline
WHT Addict
 
Join Date: Jan 2005
Location: Baghdad, Iraq
Posts: 172
You forget the ID in the query
This code should work:
PHP Code:
//if all is good, send it to the database 
    
if ($valid) { 
        
$email $_POST["email"]; 
        
$password md5($_POST["password"]); 
        
$alias $_POST["alias"]; 
        
$fanme $_POST["fname"]; 
        
$lname $_POST["lname"]; 
        
$address1 $_POST["address1"]; 
        
$address2 $_POST["address2"]; 
        
$city $_POST["city"]; 
        
$province $_POST["province"]; 
        
$postal $_POST["postal"]; 
        
$country $_POST["country"]; 
        
$bdate $_POST["month"] . "/" $_POST["day"] . "/" $_POST["year"]; 
        
$mailinglist $_POST["mailinglist"];         

        echo 
$email "<br>" $password "<br>" $alias "<br>" $fname "<br>" $lname "<br>" $address1 "<br>" $address2 "<br>" $city "<br>" $province "<br>" $postal "<br>" $country "<br>" $bdate "<br>" $mailinglist "<br>"
         
        
$query "INSERT INTO users (ID,email, password, alias, fname, lname, address1, address2, city, province, postal, country, bdate, mailinglist) VALUES (NULL,'$email', '$password', '$alias','$fname', '$lname', '$address1', '$address2', '$city', '$province', '$postal', '$country', '$bdate', '$mailinglist')"
         
        
mysql_query($query) or die('Error, insert query failed'); 

        
$query "FLUSH PRIVILEGES"
        
mysql_query($query) or die('Error, flush failed'); 
          
        echo 
"User Added";  
     
    } 

__________________
The Dream is the blueprint of success, the hope is the budget and hard working is the achievement

Reply With Quote
  #8  
Old 03-01-2006, 10:26 AM
orbitz orbitz is offline
Web Hosting Master
 
Join Date: Mar 2004
Posts: 1,301
Quote:
Originally Posted by Oras
You forget the ID in the query
This code should work:
This is an insert query - I suppose he set Id as a auto increment. No Id is needed.

For the code, print out $query and also the mysql_error() like others had previously suggested.

Reply With Quote
  #9  
Old 03-02-2006, 02:44 AM
Burhan Burhan is offline
Community Guide
 
Join Date: Jul 2003
Location: Kuwait
Posts: 5,100
No, no a few things wrong here - but not what you are guessing

Column names must be encapsed in ` ` if you use a reserved word (such as password) as a column name.

Also, instead of sitting here guessing about it, learn to love mysql_error().

You don't need to use FLUSH PRIVILEGES, as you are not modifying the MySQL user database, but your own. Also, FLUSH PRIVILEGES requires root-level permissions on MySQL.

Also -- and it seems I keep saying this on every MySQL-related thread -- ALWAYS USE mysql_real_escape_string() on all user submitted values!

__________________
In order to understand recursion, one must first understand recursion.
If you feel like it, you can read my blog
Signal > Noise

Reply With Quote
  #10  
Old 03-02-2006, 04:24 PM
Jeanco Jeanco is offline
Web Hosting Guru
 
Join Date: Aug 2003
Posts: 266
Thanks fyre.

Read up on the escpae string function and its now in full use on the site.

Reply With Quote
Reply

Related posts from TheWhir.com
Title Type Date Posted
Rackspace Adds Redis as a Service with Exceptional Cloud Services Acquisition Web Hosting News 2013-04-01 10:55:48
Samsung Ventures Backs Cloudant Database as a Service Web Hosting News 2013-02-08 14:10:26
Cloud Host Savvis Extends Cloud-Based Database Platform to European Customers Web Hosting News 2012-10-12 11:06:37
VMware vFabric Data Director 2.0 Adds Support for Oracle Databases Web Hosting News 2012-07-10 16:30:32
Web Host 1&1 Experiences Server Outage Web Hosting News 2011-09-26 17:29:11


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes
Postbit Selector

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump
Login:
Log in with your username and password
Username:
Password:



Forgot Password?
Advertisement:
Web Hosting News:



 

X

Welcome to WebHostingTalk.com

Create your username to jump into the discussion!

WebHostingTalk.com is the largest, most influentual web hosting community on the Internet. Join us by filling in the form below.


(4 digit year)

Already a member?