PHP插入似乎回滚
Posted
技术标签:
【中文标题】PHP插入似乎回滚【英文标题】:PHP Insert seeming to rollback 【发布时间】:2013-08-06 17:32:40 【问题描述】:在我看来,这并没有提交它的工作。它说它插入数据,并返回一个递增的 id。但是它不在数据库中。执行语句后我是否错过了提交工作的调用?
<?php
//Make connection
$con = mysqli_connect('xxxxxxx','xxxxxxxx','xxxxxxxxx') ;
/* check connection */
if (mysqli_connect_errno())
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
//change db to andriodnfp db
mysqli_select_db($con, 'andriodnfp');
$date = htmlspecialchars($_POST["Date"]);
$temperature = htmlspecialchars($_POST["temperature"]);
$temperature = !empty($temperature) ? "'$temperature'" : "NULL";
$Stamps = htmlspecialchars($_POST["Stamps"]);
$Stamps = !empty($Stamps) ? "'$Stamps'" : "NULL";
$Fertile = htmlspecialchars($_POST["Fertile"]);
$Fertile = !empty($Fertile) ? "'$Fertile'" : "NULL";
$Period = htmlspecialchars($_POST["Period"]);
$Period = !empty($Period) ? "'$Period'" : "NULL";
$Intercorse = htmlspecialchars($_POST["Intercorse"]);
$Intercorse = !empty($Intercorse) ? "'$Intercorse'" : "NULL";
$Cervix = htmlspecialchars($_POST["Cervix"]);
$Cervix = !empty($Cervix) ? "'$Cervix'" : "NULL";
$Mood = htmlspecialchars($_POST["Mood"]);
$Mood = !empty($Mood) ? "'$Mood'" : "NULL";
$Headache = htmlspecialchars($_POST["Headache"]);
$Headache = !empty($Headache) ? "'$Headache'" : "NULL";
$Pregnancytest = htmlspecialchars($_POST["Pregnancytest"]);
$Pregnancytest = !empty($Pregnancytest) ? "'$Pregnancytest'" : "NULL";
$Energy = htmlspecialchars($_POST["Energy"]);
$Energy = !empty($Energy) ? "'$Energy'" : "NULL";
$Notes = htmlspecialchars($_POST["Notes"]);
$Notes = !empty($Notes) ? "'$Notes'" : "NULL";
$user_id = htmlspecialchars($_POST["user_id"]);
$user_id = !empty($user_id) ? "'$user_id'" : "NULL";
if ($stmt = mysqli_prepare($con, "SELECT _id FROM CHARTING WHERE Date=? AND user_id=? ORDER BY _id LIMIT 1"))
/* bind parameters for markers */
mysqli_stmt_bind_param($stmt, "ss", $date, $user_id);
/* execute query */
mysqli_stmt_execute($stmt);
/* bind result variables */
mysqli_stmt_bind_result($stmt, $_id);
/* fetch value */
mysqli_stmt_fetch($stmt);
/* close statement */
mysqli_stmt_close($stmt);
if (!empty($_id))
//Date already exists do update
/* create a prepared statement */
if ($stmt = mysqli_prepare($con, "UPDATE CHARTING SET temperature=?, Stamps=?, Fertile=?, Period=?, intercourse=?, cervix=?, mood=?, headache=?, pregnancytest=?, energy=?, Notes=? WHERE $_id =?"))
/* bind parameters for markers */
mysqli_stmt_bind_param($stmt, "ssssssssssss", $temperature, $Stamps, $Fertile, $Period, $Intercorse, $Cervix, $Mood, $Headache, $Pregnancytest, $Energy, $Notes, $_id);
/* execute query */
mysqli_stmt_execute($stmt);
/* bind result variables */
$posts = array('auto_increment_id'=>$_id);
/* close statement */
mysqli_stmt_close($stmt);
else
/* create a prepared statement */
if ($stmt = mysqli_prepare($con, "INSERT INTO CHARTING ( Date, temperature, Stamps, Fertile, Period, intercourse, cervix, mood, headache, pregnancytest, energy, Notes, user_id)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"))
/* bind parameters for markers */
mysqli_stmt_bind_param($stmt, "sssssssssssss", $date, $temperature, $Stamps, $Fertile, $Period, $Intercorse, $Cervix, $Mood, $Headache, $Pregnancytest, $Energy, $Notes, $user_id);
/* execute query */
mysqli_stmt_execute($stmt);
/* bind result variables */
$posts = array('auto_increment_id'=>mysqli_insert_id($con));
/* close statement */
mysqli_stmt_close($stmt);
mysqli_close($con);
$posts = array($posts);
header('Content-type: application/json');
echo json_encode(array('posts'=>$posts));
?>
【问题讨论】:
您的逻辑似乎落后了 - 如果$result
为空,您应该插入而不是更新,对吧?
你完全正确。插入现在有效,唯一的问题是更新没有获得 auto_increment_id
这就是全部代码吗?你检查过你的错误日志吗?
似乎 mysqli_insert_id 对于更新不正确,我可以用什么代替。 @Mike Purcell 是的。
不要在开头多加一个SELECT
,INSERT INTO table ... ON DUPLICATE KEY UPDATE ...
怎么样
【参考方案1】:
我相信mysqli_insert_id
只会获取最后一个 ID 插入(未更新)。但这很好——如果你在运行update
语句的块内,你(可以)已经知道 id。只需将其添加到您之前的 select
声明中即可:
SELECT _id, Date FROM CHARTING WHERE Date=? AND user_id=? ORDER BY _id LIMIT 1
然后当你更新时,你可以简单地更新包含id的行(而不是按日期选择):
UPDATE CHARTING SET temperature=?, Stamps=?, Fertile=?, Period=?, intercourse=?, cervix=?, mood=?, headache=?, pregnancytest=?, energy=?, Notes=? WHERE _id =?
这具有基于主键 (_id
) 而不是日期更新字段的额外好处。后者不好,因为两行可能具有相同的日期。一般来说,您应该始终根据主键(例如where _id =?
)进行更新,这将始终是唯一的。
【讨论】:
太棒了!我做了调整...见上文。 看起来不错,除了在你的更新语句中你想要where _id=?
而不是where $_id=?
- 否则$_id
将被插值,导致你的 sql 语句包含,例如如果$_id
是5
,where 5 = 5
,这不是你想要的。以上是关于PHP插入似乎回滚的主要内容,如果未能解决你的问题,请参考以下文章