problem, inserting into mysql
Posted: Thu Aug 11, 2011 1:28 pm
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:
when I run this the $sql string is:
If so , I'm not sure how to insert `id` to allow autoincrementing.
I would appreciate any help. Thank you,
KC
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>";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?insert into mydata (first_name, last_name, phone, email, message, submit_date) values (’sdsa‘, ‘das‘, ‘222-444-5656‘, ‘adas@jhj.com‘, ‘gfhfghf‘, ‘20110811‘)
If so , I'm not sure how to insert `id` to allow autoincrementing.
I would appreciate any help. Thank you,
KC