php - MYSQL插入使用PHP prepared Statement

标签 php mysql prepared-statement

我正在尝试使用下面的 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");
?>

最佳答案

<?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();    
}
?>

关于php - MYSQL插入使用PHP prepared Statement,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37389703/

相关文章:

php - Silverstripe 条件验证

php 从 mysql 中选择,其中标题以 A 开头(并且只有 A)

mysql - 使用 Inert 语句中的 Select 将字符串常量插入表中

postgresql - 如何使用准备好的语句在 postgresql 中插入带时区的时间戳?

javascript - 如何使用 PHP 删除客户端文件?

php - onclick 设置 php session 以保持选中复选框。取消选中结束 session 时

mysql - 如何将数据从数据库复制到另一个数据库?

PHP 影响行返回 0 但查询执行

c# - ef 核心一对多关系抛出异常无法添加或更新子行

java - java中的preparedStatement SQL错误