INSERT
不起作用,不知道为什么。
db.php:
function check($value)
{
if ($value)
return htmlspecialchars(stripslashes(trim($value)));
else
return null;
}
$db_host = 'localhost';
$db_user = 'root';
$db_pass = '';
$db_name = 'cart';
$connect = mysql_connect($db_host, $db_user, $db_pass) or die ('no connect');
mysql_select_db($db_name) or die ('no db');
function add($name, $price, $description, $image)
{
global $connect;
$sql = "INSERT INTO items (name, price, description, image)
values ('{$name}', '{$price}', '{$description}', '{$image}');";
$query = mysql_query($sql, $connect);
if (!$query)
die(mysql_error());
mysql_close($connect);
}
添加.php:
header('Content-Type: text/html; charset=utf-8');
mb_internal_encoding('utf-8');
include_once('./db.php');
$name = check($_POST['name']);
$price = check($_POST['price']);
$description = check($_POST['description']);
$image = check($_POST['image']);
if ($name && $price && $description && $image)
{
add($name, $price, $description, $image);
echo 'sent';
}
当我收到sent
消息时,表中没有新行items
。我使用 phpmyadmin 检查它。
使用此代码(通过安装脚本)创建数据库:
function reset_mysql()
{
global $connect;
$sql = "CREATE TABLE items (
id INT AUTO_INCREMENT NOT NULL,
name VARCHAR(50) NOT NULL,
price VARCHAR(10) NOT NULL,
description VARCHAR(200) NOT NULL,
image VARCHAR(30) NOT NULL,
primary key (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;";
$query = mysql_query($sql, $connect);
if (!$query)
die(mysql_error());
mysql_close($connect);
}
怎么了?