【问题标题】:MYSQL insertion using PHP prepared StatementMYSQL insert 使用 PHP 编写的 Statement
【发布时间】:2016-05-23 11:23:07
【问题描述】:

我正在尝试使用下面的 php 准备语句在 mysql 表中插入一行,但代码总是通过该语句并移动到回显“失败”。下面的代码中缺少什么?

(我的数据库有额外的列,但我没有添加它,因为我不想在其中插入值(这些列之一是自动递增的))

<?php
    $ActivityDate = $_POST["ActivitytDate"];
    $CoreSite = $_POST["CoreSite"];
    $ActionAuditor = $_POST["ActionAuditor"];
    $DCOSPOC = $_POST["DCOSPOC"];

    if($stmt = $mysqli->prepare("Insert INTO DCO_Database (ActivityDate, CoreSite, ActionAuditor, DCOSPOC) Where (ActivityDate=? AND CoreSite=? AND ActionAuditor=? AND DCOSPOC=?)"))
    {
        $stmt->bind_param("ssss", $ActivityDate, $CoreSite, $ActionAuditor, $DCOSPOC);
        $stmt->execute();
        $stmt->close();
    }
    else{
        echo ("Failed");
        $mysqli->close();    
    }
?>

我已经编辑了代码以使用值,它不是回显失败而是回显成功..但仍然没有向数据库添加值

<?php
$ActivityDate = $_POST["ActivitytDate"];
$CoreSite = $_POST["CoreSite"];
$ActionAuditor = $_POST["ActionAuditor"];
$DCOSPOC = $_POST["DCOSPOC"];
$AreaOwner = $_POST["AreaOwner"];
$ActionImplementer = $_POST["ActionImplementer"];
$ActionOwner = $_POST["ActionOwner"];
$MailSubject = $_POST["MailSubject"];
$ActionType = $_POST["ActionType"];
$RequestType = $_POST["RequestType"];
$RequestNumber = $_POST["RequestNumber"];
$OpenTime = $_POST["OpenTime"];
$CloseTime = $_POST["CloseTime"];
$ActionResult = $_POST["ActionResult"];
$Violation = $_POST["Violation"];
$ActionDetails = $_POST["ActionDetails"];
$Snags = $_POST["Snags"];
$SnagDesc = $_POST["SnagDesc"];
$Layout = $_POST["Layout"];
$LayoutDesc = $_POST["LayoutDesc"];
$CabinetLocation = $_POST["CabinetLocation"];
$Mapping = $_POST["Mapping"];
$MappingDesc = $_POST["MappingDesc"];
$Notes = $_POST["Notes"];

if($stmt = $mysqli->prepare("Insert INTO DCO_Database (ActivityDate, CoreSite, ActionAuditor, DCOSPOC, AreaOwner, ActionImplementer, ActionOwner, MailSubject, ActionType, RequestType, RequestNumber, OpenTime, CloseTime, ActionResult, Violation, ActionDetails, Snags, SnagDesc, Layout, LayoutDesc, CabinetLocation, Mapping, MappingDesc, Notes) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"))
{
$stmt->bind_param("ssssssssssssssssssssssss", $ActivityDate, $CoreSite, $ActionAuditor, $DCOSPOC, $AreaOwner, $ActionImplementer, $ActionOwner, $MailSubject, $ActionType, $RequestType, $RequestNumber, $OpenTime, $CloseTime, $ActionResult, $Violation, $ActionDetails, $Snags, $SnagDesc, $Layout, $LayoutDesc, $CabinetLocation, $Mapping, $MappingDesc, $Notes);
$stmt->execute();
$stmt->close();
}
else{
echo ("Failed");
$mysqli->close();    
}
echo ("Successful");
?>

【问题讨论】:

  • 您不能在 INSERT 查询中使用 where 子句。我认为您正在寻找更新!
  • 似乎类对象$mysqliprepare 方法(顺便说一句,在您的代码中没有定义)正在返回一个虚假值。您可能想要重新访问该部分,或从处理数据库内容的类中粘贴代码。
  • 插入查询应如下所示INSERT INTO table_name (column1,column2,column3,...) VALUES (value1,value2,value3,...);

标签: php mysql prepared-statement


【解决方案1】:

//认为应该做如下,insert语句只允许使用select语句时的where子句,

<?php
$ActivityDate = $_POST["ActivitytDate"];
$CoreSite = $_POST["CoreSite"];
$ActionAuditor = $_POST["ActionAuditor"];
$DCOSPOC = $_POST["DCOSPOC"];

if($stmt = $mysqli->prepare("Insert INTO DCO_Database (ActivityDate, CoreSite, ActionAuditor, DCOSPOC) values (?,?,?,?)");
{
$stmt->bind_param("ssss", $ActivityDate, $CoreSite, $ActionAuditor, $DCOSPOC);
$stmt->execute();
$stmt->close();
}
else{
echo ("Failed");
$mysqli->close();    
}
?>

【讨论】:

  • 我更新了代码并用值更新了帖子但仍然无法正常工作,如果可能,请检查编辑
【解决方案2】:

使用 values insted of where

<?php
 $ActivityDate = $_POST["ActivitytDate"];
 $CoreSite = $_POST["CoreSite"];
 $ActionAuditor = $_POST["ActionAuditor"];
  $DCOSPOC = $_POST["DCOSPOC"];

  if($stmt = $mysqli->prepare("Insert INTO DCO_Database (ActivityDate,  CoreSite, ActionAuditor, DCOSPOC) values(?,?,?,?)"))
  {

 $stmt->bind_param("ssss", $ActivityDate, $CoreSite, $ActionAuditor, $DCOSPOC);
 $stmt->execute();
 $stmt->close();

 }
 else{
   echo ("Failed");
   $mysqli->close();    
 }

?>

【讨论】:

    【解决方案3】:

    你必须使用

    价值观

    在您的查询中。并删除

    也来自查询。请检查以下代码。

    if($stmt = $mysqli->prepare("Insert INTO DCO_Database (ActivityDate, CoreSite, ActionAuditor, DCOSPOC) values (?,?,?,?)"))
    {
    $stmt->bind_param("ssss", $ActivityDate, $CoreSite, $ActionAuditor, $DCOSPOC);
    $stmt->execute();
    $stmt->close();
    }
    else{
    echo ("Failed");
    $mysqli->close();    
    }
    

    请注意 where 用于更新。所以插入使用查询如下:

    INSERT INTO table_name (column1,column2,column3,...) VALUES (value1,value2,value3,...);
    

    【讨论】:

    • 这不是一个有效的查询。
    • 谢谢,我用值更新了代码,但还是不行,请你看一下帖子中的代码编辑
    • 已解决,我正在编写额外的 fetch 命令导致问题。非常感谢
    猜你喜欢
    • 1970-01-01
    • 2012-04-15
    • 2011-10-21
    • 1970-01-01
    • 1970-01-01
    • 2022-10-04
    • 1970-01-01
    • 1970-01-01
    • 2014-06-20
    相关资源
    最近更新 更多