我正在努力让管理员网站工作,管理员用户可以在表单中输入字段并在数据库中填入信息。这是php代码的主要部分:
<?php
// Parse the form data and add inventory item to the system
if (isset($_POST['name'])) {
$name = pg_escape_string($_POST['name']);
$price = pg_escape_string($_POST['price']);
// See if that product name is an identical match to another product in the system
$sql = pg_query("SELECT id FROM beer WHERE name='$name' LIMIT 1");
$productMatch = pg_num_rows($sql); // count the output amount
if ($productMatch > 0) {
echo 'Sorry you tried to place a duplicate "Product Name" into the system, <a href="inventory_list.php">click here</a>';
exit();
}
// Add this product into the database now
$sql = pg_query("INSERT INTO beer (name, price)
VALUES('$name','$price', now())");
$pid = pg_insert_id();
// Place image in the folder
$newname = "$pid.jpg";
move_uploaded_file( $_FILES['fileField']['tmp_name'], "../inventory_images/$newname");
header("location: inventory_list.php");
exit();
}
?>
当我尝试将字段输入网站页面时,我不断收到警告:
警告:pg_query():查询失败:ERROR:INSERT表达式多于目标列LINE 2:VALUES('djsd','4',now())^ in / home / s14266 / public_html / groupA5 / storeadmin第44行/inventory_list.php
致命错误:在第45行的/home/s14266/public_html/groupA5/storeadmin/inventory_list.php中调用未定义的函数pg_insert_id()
我不确定发生了什么!
答案 0 :(得分:1)
我不确定您的表格Column Names
,但您的查询应该是这样的。您收到警告是因为您为三列添加了值,但是您只指定了两列,在这种情况下name
&amp; price
。
$sql = pg_query("INSERT INTO beer (name, price,date)
^^^^//change as per yours
VALUES('$name','$price', now())");