msqli不插入 [英] msqli does not INSERT
问题描述
im试图将表单中的数据(通过POST)插入MySQL数据库,它没有显示任何错误,但是当我在phpMyAdmin中检查它时却没有显示.
im trying to insert data from a form (via POST) into a MySQL database, it does not show any errors, but it does not show up when I check it in phpMyAdmin.
<?php
$amount = $_POST["amount"];
$unit = $_POST["unit"];
$date = date('Y-m-d H:i:s');
$host = "localhost";
$user = "root";
$pass = "";
$db = "finance";
$table = "silver";
$mysqli = new mysqli($host, $user, $pass, 'finance');
if(!$mysqli)
{
echo "<div class=\"error\">";
echo "No connection can be established";
echo "</div>";
die();
}
if ($unit = "gram") $amount = $amount * 28.3495231;
// create query
$query = "INSERT INTO 'silver'.'finance' (
'Transaction_Num',
'dtStamp',
'Amount'
)
VALUES (
NULL, \'2013-07-03 06:18:16\', \'1\'
);";
// execute query
$mysqli->query($query);
?>
我正在XAMPP 1.8.2 w/PHP 5.4.16,Apache 2.4.4和 Windows XP上的MySQL 5.6.11.
I'm running this on XAMPP 1.8.2 w/ PHP 5.4.16, Apache 2.4.4 and MySQL 5.6.11 on Windows XP.
推荐答案
不要在表名和字段名上使用单引号.那应该是反引号.
Don't use single quotes on table and field names. That should be backticks.
尝试一下:
$query = "INSERT INTO `silver`.`finance` (
`Transaction_Num`,
`dtStamp`,
`Amount`
)
VALUES (
NULL, '2013-07-03 06:18:16', '1'
);";
OR/AND,如果Transaction_Num
是AUTO_INCREMENT
,则可能不需要插入它,例如:
OR/AND if the Transaction_Num
is AUTO_INCREMENT
you may not need to insert it like:
$query = "INSERT INTO silver.finance (dtStamp,Amount)
VALUES ( '2013-07-03 06:18:16', 1);";
OR/AND,如果您的dtStamp
是DEFAULT = CURRENT_TIMESTAMP
,则可能不需要插入以下任何一种内容:
OR/AND if you have dtStamp
is DEFAULT = CURRENT_TIMESTAMP
you may not need to insert that either like:
$query = "INSERT INTO silver.finance (Amount)
VALUES (1);";
这篇关于msqli不插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!