Page 1 of 1

problem, inserting into mysql

Posted: Thu Aug 11, 2011 1:28 pm
by kc11
Hi,

I am trying to insert form data into a mysql table.

Here is my table:

[text]
CREATE TABLE `mydata` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`first_name` varchar(255) DEFAULT NULL,
`last_name` varchar(255) DEFAULT NULL,
`phone` varchar(20) DEFAULT NULL,
`email` varchar(255) DEFAULT NULL,
`message` varchar(4096) DEFAULT NULL,
`submit_date` date NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1
[/text]

here is my code:

Code: Select all


<?php
// Connecting to the MySQL server
$host='localhost';
$user_name='xxx_mydata';
$pwd='mydata';
$database_name='xxx_mydata' ; 
$db=mysql_connect($host, $user_name, $pwd);
if (mysql_error() > "") print mysql_error() . "<br>";
mysql_select_db($database_name, $db);
if (mysql_error() > "") print mysql_error() . "<br>";
// Storing form values into PHP variables
$first_name = $_POST["first_name"]; // Since method="post" in the form
$last_name = $_POST["last_name"];
$phone = $_POST["phone"];
$email = $_POST["email"];
$city = $_POST["city"];
$message = $_POST["message"];
$submitdate = date("Ymd");
// Inserting these values into the MySQL table
// we created above
$query = "insert into mydata (first_name, last_name, phone, email, message, submit_date) values (’" . $first_name . "‘, ‘" . $last_name . "‘, ‘" . $phone . "‘, ‘" . $email . "‘, ‘" . $message . "‘, ‘" . $submitdate . "‘)";
print $query;
$result = mysql_query($query);
// mysql_query() is a PHP function for executing
// MySQL queries
echo "<h1>Thank you for submitting your details!</h1>";
when I run this the $sql string is:
insert into mydata (first_name, last_name, phone, email, message, submit_date) values (’sdsa‘, ‘das‘, ‘222-444-5656‘, ‘adas@jhj.com‘, ‘gfhfghf‘, ‘20110811‘)
I am not getting any mysql errors so I believe the connection is working. Could the problem have anything to do with only inserting 6 fields into a 7 field table?
If so , I'm not sure how to insert `id` to allow autoincrementing.

I would appreciate any help. Thank you,

KC

Re: problem, inserting into mysql

Posted: Thu Aug 11, 2011 1:36 pm
by Celauran
Your first backtick isn't a backtick. That's where I'd start.

You've specified six values and are inserting six, so that's not a problem.

Re: problem, inserting into mysql

Posted: Thu Aug 11, 2011 2:21 pm
by kc11
Here is the sql string now:
insert into mydata (first_name, last_name, phone, email, message, submit_date) values (‘dsdsd‘, ‘das‘, ‘222-444-5656‘, ‘xxx@yyy.org‘, ‘sadasdas‘, ‘20110811‘)
It still is not inserting.

I tried using phpmyadmin, and get the following error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '@yyy.org‘, ‘sadasdas‘, ‘20110811‘)' at line 1
KC

Re: problem, inserting into mysql

Posted: Thu Aug 11, 2011 3:21 pm
by kc11
I changed the ` to ' giving:
$query = "insert into mydata (first_name, last_name, phone, email, message, submit_date) values (' " . $first_name . "', '" . $last_name . "', '" . $phone . "', " . $email . ", '" . $message . "', '" . $submitdate . "')";
It now works! ( very arcane though )

- KC