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_idIDのみを取得すると思います。しかし、それは問題ありません。ステートメントを実行するブロック内にいる場合は、ID を既に (知っている可能性があります)。それを以前のステートメントに追加するだけです:updateselect

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これには、日付ではなく主キー ( ) に基づいてフィールドを更新するという追加の利点があります。2 つの行が同じ日付を持つ可能性があるため、後者は不適切です。一般に、常に一意である主キー (例: ) に基づいて常に更新を行う必要がありますwhere _id =?

于 2013-08-06T17:52:43.127 に答える