1

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

?>
4

1 回答 1

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 =?)进行更新,主键始终是唯一的。

于 2013-08-06T17:52:43.127 回答