dtpa98038 2017-03-29 14:02
浏览 24

在两个使用execute()的函数上使用LAST_INSERT_ID();

One submit button. Two tables. On clicking submit a marker is created, along with its lng and lat values. This information is added to marker table.

The marker ID is a FK in the submissions table. Documentation for LAST_INSERT_ID(); says: The ID that was generated is maintained in the server on a per-connection basis. I think each execution resets this, but not sure.

It is currently inserting data into the marker table but nothing into the submissions table. I have it checking to make sure all fields have content if not it is an invalid form and an error shows, same for a db connection, but at the moment its taking me to a success page.

This SQL Query straight into mysql database gives the desired result:

INSERT INTO markers VALUES (NULL, 53.645792, -1.785035);

INSERT INTO submissions (submission_id, marker_id, title, info, image) 
VALUES (1, LAST_INSERT_ID(), "this is a title", "info", "image");

My code in my files however don't...

Model:

function insertSubmissionMarker($conn,$lat,$lng)
{ 
    $query="INSERT INTO markers VALUES (NULL, :lat, :lng)";
    $stmt=$conn->prepare($query);
    $stmt->bindValue(':lat', $lat);
    $stmt->bindValue(':lng', $lng);
    $affected_rows = $stmt->execute();
    if($affected_rows==1)
    {
        return true;
    }else{
        return false;
    }
}

function insertSubmissionContent($conn,$marker_id,$title,$info,$image)
{   
    $query="INSERT INTO submissions VALUES (NULL, LAST_INSERT_ID(), :title, :info, :fileToUpload)";
    $stmt=$conn->prepare($query);
    $stmt->bindValue(':title', $title);
    $stmt->bindValue(':info', $info);
    $stmt->bindValue(':fileToUpload', $image);
    $affected_rows = $stmt->execute();  
    if($affected_rows==1)
    {
        return true;
    }else{
        return false;
    }
}

function addToMap ($conn,$lat,$lng,$title,$info,$image){
    $x = insertSubmissionMarker ($conn,$lat,$lng);
    $y = insertSubmissionContent ($conn,$title,$info,$image);
    if($x && $y = true)
    {
        return true;
    }else{
        return false;
    }

}

Controller:

$conn=getConn();
$success=addToMap($conn,$title,$info,$lat,$lng,$image);
$conn=NULL; //close the connection
if($success)
{
    include("views/add-map-confirm-view.php");

}else
{
    $errorMsgs[]="Problem inserting data into the database";
    include("views/add-map-error-view.php");
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
    • ¥500 52810做蓝牙接受端
    • ¥15 基于PLC的三轴机械手程序
    • ¥15 多址通信方式的抗噪声性能和系统容量对比
    • ¥15 winform的chart曲线生成时有凸起
    • ¥15 msix packaging tool打包问题
    • ¥15 finalshell节点的搭建代码和那个端口代码教程
    • ¥15 Centos / PETSc / PETGEM
    • ¥15 centos7.9 IPv6端口telnet和端口监控问题
    • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作