最后在pdo中插入ID [英] last insert id in pdo

查看:109
本文介绍了最后在pdo中插入ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个只有一个输入的表格.当我单击按钮时,将发出AJAX请求,并将数据发送到response.php,该文件使用pdo库将数据插入db.没关系,但是在页面中,有一个成功函数将数据加载到表单中-当ajax请求完成时,结果为空白...我需要刷新(F5)页面以查看数据. . 这是这段代码:

<?php
include_once("config.php");
if (!empty($_POST)) {
        try{
    $statement = $conn->prepare("INSERT INTO DIAGNOSTICO (id_paciente, id_doctor, hconsulta) VALUES (?, ?, ?)");

    if ($statement->execute(array($_POST['id_paciente'], $_POST['id_doctor'], $_POST['hconsulta'])));
        $dbSuccess = true;

} catch (Exception $e) {
    $return['databaseException'] = $e->getMessage();
}
 {
echo $conn->lastInsertId(); 
echo '<li id="item_'.$row["id_diagnostico"].'">';
echo '<div class="del_wrapper"><a href="#" class="del_button" id="del-'.$row["id_diagnostico"].'">';
echo '<img src="../images/icon_del.gif" border="0" />';
echo '</a></div>'; echo ' Fecha de consulta : ';echo $row["f_diagnostico"]; echo ' <br><br> ';
echo $row["hconsulta"].'</li>';
}
   $dbh = null;  
}
?>

它正在工作,但是在页面中时,结果从ajax请求返回并显示id但不显示数据...我需要数据结果...您能帮我吗?

最诚挚的问候!

解决方案

使用lastinsertid后,您必须使用

$sth = $dbh->prepare("SELECT * FROM DIAGNOSTICO WHERE id_diagnostico= ".$conn->lastInsertId());
$sth->execute();

$row = $sth->fetch(PDO::FETCH_ASSOC);
echo '<li id="item_'.$row["id_diagnostico"].'">';
echo '<div class="del_wrapper"><a href="#" class="del_button" id="del-'.$row["id_diagnostico"].'">';
echo '<img src="../images/icon_del.gif" border="0" />';
echo '</a></div>'; echo ' Fecha de consulta : ';echo $row["f_diagnostico"]; echo ' <br><br> ';
echo $row["hconsulta"].'</li>';

data

$sth = $dbh->prepare("SELECT * FROM DIAGNOSTICO WHERE id_diagnostico= ".$conn->lastInsertId());
$sth->execute();

$row = $sth->fetch(PDO::FETCH_ASSOC);
echo '<li id="item_'.$row["id_diagnostico"].'">';
echo '<div class="del_wrapper"><a href="#" class="del_button" id="del-'.$row["id_diagnostico"].'">';
echo '<img src="../images/icon_del.gif" border="0" />';
echo '</a></div>'; echo ' Fecha de consulta : ';echo $row["f_diagnostico"]; echo ' <br><br> ';
echo $row["hconsulta"].'</li>';

阅读 http://php.net/manual/en/pdostatement.fetch.php

I have a form with only one input. When I click the button, an AJAX request is made and the data is sent to the response.php, which uses the pdo library to insert the data to db. Here it is okay, but in the page, there is a success function which loads the data into the form - when the ajax request completes, the result is blank...I need to refresh(F5) the page to see the data .. Here is this code:

<?php
include_once("config.php");
if (!empty($_POST)) {
        try{
    $statement = $conn->prepare("INSERT INTO DIAGNOSTICO (id_paciente, id_doctor, hconsulta) VALUES (?, ?, ?)");

    if ($statement->execute(array($_POST['id_paciente'], $_POST['id_doctor'], $_POST['hconsulta'])));
        $dbSuccess = true;

} catch (Exception $e) {
    $return['databaseException'] = $e->getMessage();
}
 {
echo $conn->lastInsertId(); 
echo '<li id="item_'.$row["id_diagnostico"].'">';
echo '<div class="del_wrapper"><a href="#" class="del_button" id="del-'.$row["id_diagnostico"].'">';
echo '<img src="../images/icon_del.gif" border="0" />';
echo '</a></div>'; echo ' Fecha de consulta : ';echo $row["f_diagnostico"]; echo ' <br><br> ';
echo $row["hconsulta"].'</li>';
}
   $dbh = null;  
}
?>

It is working, but when in the page, the result returns from the ajax request and shows the id but not the data...I need the data result...can you help me?

best regards!

解决方案

After using the lastinsertid you have to select data by using

$sth = $dbh->prepare("SELECT * FROM DIAGNOSTICO WHERE id_diagnostico= ".$conn->lastInsertId());
$sth->execute();

$row = $sth->fetch(PDO::FETCH_ASSOC);
echo '<li id="item_'.$row["id_diagnostico"].'">';
echo '<div class="del_wrapper"><a href="#" class="del_button" id="del-'.$row["id_diagnostico"].'">';
echo '<img src="../images/icon_del.gif" border="0" />';
echo '</a></div>'; echo ' Fecha de consulta : ';echo $row["f_diagnostico"]; echo ' <br><br> ';
echo $row["hconsulta"].'</li>';

Read http://php.net/manual/en/pdostatement.fetch.php

这篇关于最后在pdo中插入ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆