【问题标题】:Mysqli Insert Command not Working [closed]Mysqli插入命令不起作用[关闭]
【发布时间】:2014-12-28 09:23:52
【问题描述】:

我有一个名为“kargo”的数据库和一个名为“cargos”的表。我尝试了下面的代码,但来自POST 的数据没有添加到“货物”表中。连接很好,没有返回错误。我能做什么?

<?php include("functions.php"); get_header(); ?>
<div id="content">
<?php $mysqli = new mysqli('localhost','root','','kargo');
$name = $_POST['c_name'];
$c_texit = $_POST['c_texit'];
$c_tarrive = $_POST['c_tarrive'];
$target = "kargo/";
$target_file = $_FILES["cpic"]["tmp_name"];
$target_move = $target.$_FILES["cpic"]["name"];
$uploadOk = 1 ;

if (file_exists($target_move)) {
$uploadOk = 0; } 

if($uploadOk == 1 ) {
$query = $mysqli->query("INSERT INTO cargos 
VALUES(NULL,'".$name."','".$c_texit."','".$c_tarrive."','".$target_move."')");
move_uploaded_file($target_file,$target_move);
echo "Cargo added."; header( "refresh:5;url=panel.php" ); }
else { echo "-"; header( "refresh:5;url=panel.php" ); } ?>
</div>
<?php get_footer(); ?>

【问题讨论】:

  • 可能是VALUES(NULL 应该是VALUES (NULL
  • 试过了,什么都没发生
  • 首先要做的是检查您的错误日志并使用文件顶部的error_reporting(E_ALL); 打开错误报告。您还应该使用缩进来帮助将来更容易地发现语法错误。您还需要告诉我们期望的行为,而不仅仅是“它不起作用”
  • 添加了“echo error_reporting(E_ALL);”在“?>”标签之前。返回 32767
  • 'doesn't work' 不是问题描述。 '不返回错误' - 你怎么知道?这段代码中没有一个错误检查。

标签: php mysql mysqli


【解决方案1】:

如果您插入的数据与表 cargos 匹配,请尝试检查您在每一列中使用的数据类型以及 NOT NULL 和其他限制。

如果你使用 localhost,你可以通过向 phpmyadmin 执行那个 mysqli 命令来检查错误。


您也可以使用mysqli_error()函数来检查SQL是否有错误。

例子:

$query = $mysqli->query("INSERT INTO cargos VALUES(NULL,'".$name."','".$c_texit."','".$c_tarrive."','".$target_move."')"))
if($mysqli->query($query) === FALSE)
    {
       mysqli_error($mysqli);   // generate SQL error message.
    }
    else
    {
       // execute something...
    }

【讨论】:

  • 我在 phpmyadmin 上遇到了这个错误,谢谢朋友。 " 列 'id' 不能为空"
猜你喜欢
  • 2017-10-20
  • 1970-01-01
  • 2013-02-24
  • 1970-01-01
  • 1970-01-01
  • 2023-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多