在我看来,这不是在做它的工作。它表示它插入数据,并返回增加的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));
?>
答案 0 :(得分:2)
我相信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 =?
)进行更新,主键始终是唯一的。