Need help understanding insert
Posted: Sun Nov 09, 2008 10:31 am
Hi all. I'm a Python/Perl programmer trying to learn PHP/MySql and running into something I don't quite understand. I began with a simple contacts database that I can input stuff from a webpage. What I don't understand is why I have to enter anything for the primary id key when it is auto-incremented. Further confusing is, tutorials show a database like mine and the php code doesn't try to insert an int for the key. Let me show you what I have:
This works:
From my php:
That gives me this error: Unknown column 'test' in 'field list'
I've tried putting a 0 in the $query VALUES string, and I get tihs error: Column count doesn't match value count at row 1
Can someone help me understand how this should work and why? Thanks in advance!
-Sully
Code: Select all
mysql> desc contacts;
+--------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| f_name | varchar(20) | YES | | NULL | |
| l_name | varchar(20) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| email | varchar(60) | YES | | NULL | |
| hphone | varchar(20) | YES | | NULL | |
| cphone | varchar(20) | YES | | NULL | |
+--------+-------------+------+-----+---------+----------------+
7 rows in set (0.00 sec)
Code: Select all
mysql> insert into contacts values (0,'s', 's', 32, 'email', 'none', 'nhone');
Query OK, 1 row affected (0.00 sec)
mysql> select * from contacts;
+----+--------+--------+------+-------+--------+--------+
| id | f_name | l_name | age | email | hphone | cphone |
+----+--------+--------+------+-------+--------+--------+
| 1 | s | s | 32 | email | none | nhone |
+----+--------+--------+------+-------+--------+--------+
1 row in set (0.00 sec)
Code: Select all
<?php
include 'config.php';
include 'opendb.php';
$f_name = $_POST['f_name'];
$l_name = $_POST['l_name'];
$email = $_POST['email'];
$age = $_POST['age'];
$hphone = $_POST['hphone'];
$cphone = $_POST['cphone'];
$query = "insert into contacts (
f_name, l_name, email, age, hphone, cphone) values (
$f_name, $l_name, $email, $age, $hphone, $cphone)";
mysql_query($query) or die(mysql_error());
include 'closedb.php';
echo "<h3>Input Successful!</h3>"
?>
I've tried putting a 0 in the $query VALUES string, and I get tihs error: Column count doesn't match value count at row 1
Can someone help me understand how this should work and why? Thanks in advance!
-Sully