在我看来,这并没有完成它的工作。它说它插入数据,并返回一个递增的 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));
?>