这是基于O'Reilly书中学习PHP,MySQL,JavaScript和CSS第二版的一个示例。无法将内容添加到数据库
我无法通过接口向数据库添加任何记录。每当我尝试,不管是什么我进入,我得到确切的同样的错误:
column count doesn't match value count at row 1
如果我打的添加记录按钮两次以上的时间,其间没有退出页面,程序开始删除记录来自数据库。
<?php // sqltest.php
// Note: This example is different to the one in the book. It has
// been amended to work correctly when deleting entries.
require_once 'login.php';
$db_server = mysql_connect($db_hostname, $db_username, $db_password);
if (!$db_server) die("Unable to connect to MySQL: " . mysql_error());
mysql_select_db($db_database, $db_server)
or die("Unable to select database: " . mysql_error());
if (isset($_POST['delete']) && isset($_POST['isbn']))
{
$isbn = get_post('isbn');
$query = "DELETE FROM classics WHERE isbn='$isbn'";
if (!mysql_query($query, $db_server))
echo "DELETE failed: $query<br />" .
mysql_error() . "<br /><br />";
}
if (isset($_POST['author']) &&
isset($_POST['title']) &&
isset($_POST['category']) &&
isset($_POST['year']) &&
isset($_POST['isbn']))
{
$author = get_post('author');
$title = get_post('title');
$category = get_post('category');
$year = get_post('year');
$isbn = get_post('isbn');
$query = "INSERT INTO classics VALUES" .
"('$author', '$title', '$category', '$year', '$isbn')";
if (!mysql_query($query, $db_server))
echo "INSERT failed: $query<br />" .
mysql_error() . "<br /><br />";
}
echo <<<_END
<form action="sqltest.php" method="post"><pre>
Author <input type="text" name="author" />
Title <input type="text" name="title" />
Category <input type="text" name="category" />
Year <input type="text" name="year" />
ISBN <input type="text" name="isbn" />
<input type="submit" value="ADD RECORD" />
</pre></form>
_END;
$query = "SELECT * FROM classics";
$result = mysql_query($query);
if (!$result) die ("Database access failed: " . mysql_error());
$rows = mysql_num_rows($result);
for ($j = 0 ; $j < $rows ; ++$j)
{
$row = mysql_fetch_row($result);
echo <<<_END
<pre>
Author $row[0]
Title $row[1]
Category $row[2]
Year $row[3]
ISBN $row[4]
</pre>
<form action="sqltest.php" method="post">
<input type="hidden" name="delete" value="yes" />
<input type="hidden" name="isbn" value="$row[4]" />
<input type="submit" value="DELETE RECORD" /></form>
_END;
}
mysql_close($db_server);
function get_post($var)
{
return mysql_real_escape_string($_POST[$var]);
}
?>
这里是sql文件
-- MySQL dump 10.13 Distrib 5.1.50, for Win32 (ia32)
--
-- Host: localhost Database:
-- ------------------------------------------------------
-- Server version 5.1.50-community
/*!40101 SET @[email protected]@CHARACTER_SET_CLIENT */;
/*!40101 SET @[email protected]@CHARACTER_SET_RESULTS */;
/*!40101 SET @[email protected]@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @[email protected]@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @[email protected]@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @[email protected]@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @[email protected]@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @[email protected]@SQL_NOTES, SQL_NOTES=0 */;
--
-- Current Database: `publications`
--
CREATE DATABASE /*!32312 IF NOT EXISTS*/ `publications` /*!40100 DEFAULT CHARACTER SET latin1 */;
USE `publications`;
--
-- Table structure for table `accounts`
--
DROP TABLE IF EXISTS `accounts`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `accounts` (
`number` int(11) NOT NULL DEFAULT '0',
`balance` float DEFAULT NULL,
PRIMARY KEY (`number`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `accounts`
--
LOCK TABLES `accounts` WRITE;
/*!40000 ALTER TABLE `accounts` DISABLE KEYS */;
INSERT INTO `accounts` VALUES (12345,1050.61),(67890,140);
/*!40000 ALTER TABLE `accounts` ENABLE KEYS */;
UNLOCK TABLES;
--
-- Table structure for table `classics`
--
DROP TABLE IF EXISTS `classics`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `classics` (
`author` varchar(128) DEFAULT NULL,
`title` varchar(128) DEFAULT NULL,
`category` varchar(16) DEFAULT NULL,
`year` smallint(6) DEFAULT NULL,
`isbn` char(13) NOT NULL DEFAULT '',
PRIMARY KEY (`isbn`),
KEY `author` (`author`(20)),
KEY `title` (`title`(20)),
KEY `category` (`category`(4)),
KEY `year` (`year`),
FULLTEXT KEY `author_2` (`author`,`title`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `classics`
--
LOCK TABLES `classics` WRITE;
/*!40000 ALTER TABLE `classics` DISABLE KEYS */;
INSERT INTO `classics` VALUES ('Mark Twain (Samuel Langhorne Clemens)','The Adventures of Tom Sawyer','Classic Fiction',1876,'9781598184891'),('Jane Austen','Pride and Prejudice','Classic Fiction',1811,'9780582506206'),('Charles Darwin','The Origin of the Species','Classic Fiction',1856,'9780517123201'),('Charles Dickens','The Old Curiosity Shop','Classic Fiction',1841,'9780099533474'),('William Shakespear','Romeo and Juliet','Play',1594,'9780192814968');
/*!40000 ALTER TABLE `classics` ENABLE KEYS */;
UNLOCK TABLES;
--
-- Table structure for table `classics52`
--
DROP TABLE IF EXISTS `classics52`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `classics52` (
`author` varchar(128) DEFAULT NULL,
`title` varchar(128) DEFAULT NULL,
`category` varchar(16) DEFAULT NULL,
`year` smallint(6) DEFAULT NULL,
`isbn` char(13) NOT NULL DEFAULT '',
PRIMARY KEY (`isbn`),
KEY `author` (`author`(20)),
KEY `title` (`title`(20)),
KEY `category` (`category`(4)),
KEY `year` (`year`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `classics52`
--
LOCK TABLES `classics52` WRITE;
/*!40000 ALTER TABLE `classics52` DISABLE KEYS */;
/*!40000 ALTER TABLE `classics52` ENABLE KEYS */;
UNLOCK TABLES;
--
-- Table structure for table `customers`
--
DROP TABLE IF EXISTS `customers`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `customers` (
`name` varchar(128) DEFAULT NULL,
`isbn` varchar(128) NOT NULL DEFAULT '',
PRIMARY KEY (`isbn`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `customers`
--
LOCK TABLES `customers` WRITE;
/*!40000 ALTER TABLE `customers` DISABLE KEYS */;
INSERT INTO `customers` VALUES ('Joe Bloggs','9780099533474'),('Mary Smith','9780582506206'),('Jack Wilson','9780517123201');
/*!40000 ALTER TABLE `customers` ENABLE KEYS */;
UNLOCK TABLES;
--
-- Current Database: `test`
--
CREATE DATABASE /*!32312 IF NOT EXISTS*/ `test` /*!40100 DEFAULT CHARACTER SET latin1 */;
USE `test`;
/*!40103 SET [email protected]_TIME_ZONE */;
/*!40101 SET [email protected]_SQL_MODE */;
/*!40014 SET [email protected]_FOREIGN_KEY_CHECKS */;
/*!40014 SET [email protected]_UNIQUE_CHECKS */;
/*!40101 SET [email protected]_CHARACTER_SET_CLIENT */;
/*!40101 SET [email protected]_CHARACTER_SET_RESULTS */;
/*!40101 SET [email protected]_COLLATION_CONNECTION */;
/*!40111 SET [email protected]_SQL_NOTES */;
-- Dump completed on 2012-12-26 18:43:24
是我的SQL数据库本身搞砸?如果是这样的话,它如何改变独立计划的功能?如果不是,是什么导致我的错误?
如果有人需要他们来测试程序,我可以提供我的.sql备份文件和登录文件。 – David
你可能想要找到一本新书,它已经更新了不使用'mysql_ *'函数的例子。这些已被弃用,不应该再被使用。如果你只是PHP新手,也许可以找出一个更新的文本,这样你就可以学习更适合的方法来做事。 –
这本书很新。无论如何,我开始怀疑我的计算机的过度安全系统是否应该归咎于此处...... – David