Problem with INSERT to mySQL

Im having some problems with inserting some data into a table

Page 1 of 1

3 Replies - 1108 Views - Last Post: 26 March 2009 - 07:34 AM Rate Topic: -----

#1 Johns3n  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 10-March 09

Problem with INSERT to mySQL

Post icon  Posted 26 March 2009 - 05:09 AM

Hello DIC Community

I was wondering if you could help a PHP and mySQL rookie like me with some simple functions that im having some trouble with? :(

I have made a contact form which people can contact the firm im doing a website for and be added to their real mail list and email list. But i also want the contact form to store the given data into a table aswell.. to that end i'v written a code based on the simple INSERT code found on w3schools.com and it looks like this:

<?php
$con = mysql_connect("localhost","//username censored//","//password censored//");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("kulturellen", $con);

$sql="INSERT INTO fusion_maillist (visitor, visitormail, visitoradress, visitorcity, visitorphone, visitorcell, concerts, torvet, kunst, other, kulturellen, notes)
VALUES
('$_POST[visitor]','$_POST[visitormail]','$_POST[visitoradress]','$_POST[visitorcity]','$_POST[visitorphone]','$_POST[visitorcell]','$_POST[concerts]','$_POST[torvet]','$_POST[torvet]','$_POST[kunst]','$_POST[other]','$_POST[kulturellen]','$_POST[notes]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 new record added to table";

mysql_close($con)
?>



However when i do that, it gives me following error message:"Error: Column count doesn't match value count at row 1" and my first row is of course the "id" with auto value.. i have included also a screen shot of my mySQL table structure for you to examine and see if i made any errors?

Posted Image

Im using a danish mySQL interface, so if you have any trouble with translations please just ask ;)

I noticed that if i put in a id under colume data like this:
$sql="INSERT INTO fusion_maillist (id, visitor, etc etc

it will write the data into the table, without any error messages, however it will skip my visitor's name and everything will be shown in the wrong coloums.. but im doing it wrong i know.. but just wanted to add, that i have tried to fix it before posting here ^^

So hope that you guys (and gals) can give me a quick and helpfull solution :D

Thanks in advance..
Kenneth

This post has been edited by Johns3n: 26 March 2009 - 05:12 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Problem with INSERT to mySQL

#2 Johns3n  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 10-March 09

Re: Problem with INSERT to mySQL

Posted 26 March 2009 - 05:25 AM

I found the error myself..

I feel soo stupid, ofc your mySQL is going to fail if your trying to insert 14 data strings into a 13 coloum table.. i had a
$POST_[$torvet]
to much which caused it to fail..

this topic can be closed as it is no longer needed for help, i choose to reply instead of just editing to have the problem still open so other rookie PHP and mySQL coders doesn't make the same mistake as i did.. but the topic need not be relpy'ed to anymore :)

Thanks anyways
Kenneth :)
Was This Post Helpful? 0
  • +
  • -

#3 roudard  Icon User is offline

  • D.I.C Head

Reputation: 17
  • View blog
  • Posts: 143
  • Joined: 26-September 05

Re: Problem with INSERT to mySQL

Posted 26 March 2009 - 06:09 AM

View PostJohns3n, on 26 Mar, 2009 - 04:25 AM, said:

I found the error myself..

I feel soo stupid, ofc your mySQL is going to fail if your trying to insert 14 data strings into a 13 coloum table.. i had a
$POST_[$torvet]
to much which caused it to fail..

this topic can be closed as it is no longer needed for help, i choose to reply instead of just editing to have the problem still open so other rookie PHP and mySQL coders doesn't make the same mistake as i did.. but the topic need not be relpy'ed to anymore :)

Thanks anyways
Kenneth :)

Hi Kenneth,

Good thing you found the problem.
Just a remark on the way you insert data ... using the $_POST variable directly in the query represents a security risk... try to find some info on db injection and php security, there are some simple ways to limit the risks

good luck :)
Was This Post Helpful? 0
  • +
  • -

#4 gregwhitworth  Icon User is offline

  • Tired.
  • member icon

Reputation: 219
  • View blog
  • Posts: 1,604
  • Joined: 20-January 09

Re: Problem with INSERT to mySQL

Posted 26 March 2009 - 07:34 AM

Also, thanks for posting your finding of the problem and what it was. It allows future problem seekers to get answers to their problems.

Come back if you need anymore help, or, just need inspiration to solve it yourself. ;)

--

Greg
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1