PHP和mySQL“UPDATE”实际上并没有更新

时间:2013-03-05 06:00:17

标签: php mysql sql-update

所以我和我的朋友得出的结论是,$ _email变量可以解决所有问题。只要它是硬编码的,它就可以工作。但是,只要它在任何地方留下$ _email,它就不会。该消息以“已更新”的形式传递,但不会更新。

require_once('appVars6.php');
require_once('connectVars6.php');
$_dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$_id = $_GET['id'];
$_queryOne = "SELECT * FROM midterm WHERE id = '$_id'";
$_resultOne = mysqli_query($_dbc, $_queryOne) or die ('Error Querying Database');
while ($_row = mysqli_fetch_array($_resultOne)) {
echo '<form class="update" method="post" action="MT_vjones_udpateRecord.php?id=' . $_id . '">';
echo '<input type="hidden" name="id" id="id" value="' . $_row['id'] . '" />';
echo '<input type="text" name="firstName" id="firstName" value="' . $_row['firstName'] . '" /><br />';
echo '<input type="text" name="lastName" id="lastName" value="' . $_row['lastName'] . '" /><br />';
echo '<input type="text" name="email" id="email" value="' . $_row['email'] . '" /><br />';
echo '</form>';
}
if ( isset($_GET['firstName']) && isset($_GET['lastName']) && isset($_GET['email'])) {
$_id = $_GET['id'];
$_firstName = $_GET['firstName'];
$_lastName = $_GET['lastName'];
$_email = $_GET['email'];   
}
else if ( isset($_POST['firstName']) && isset($_POST['lastName']) && isset($_POST['email'])) {
$_id = $_POST['id'];
$_firstName = mysqli_real_escape_string($_dbc, trim($_POST['firstName']));
$_lastName = mysqli_real_escape_string($_dbc, trim($_POST['lastName']));
$_email = mysqli_real_escape_string($_dbc, trim($_POST['email']));
}
else {
echo '<br />';
echo '<p class="error">Sorry, no record was selected.</p>';
}
if(isset($_POST['submit'])) {
if ($_POST['confirm'] == 'Yes') {
//$_dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$_query = "UPDATE midterm " .
"SET email = '$_email'" .
"WHERE id = $_id" ;
$_result = mysqli_query($_dbc, $_query) or die (mysqli_error($_dbc));
mysqli_close($_dbc);
echo '<p>The record of ' . $_firstName . ' ' . $_lastName . ' for ' . $_email . ' was successfully updated.';
}
else {
echo '<p class="error">The record was not updated.</p>';
}
}
else if (isset($_id) && isset($_firstName) && isset($_lastName) && isset($_email)) {
echo '<p>Are you sure you want to update the following record?</p>';
/*echo '<form class="update" method="post" action="MT_vjones_updateRecord.php">';
echo '<input type="text" name="firstName" id="firstName" value="' . $_firstName . '" /><br />';
echo '<input type="text" name="lastName" id="lastName" value="' . $_lastName . '" /><br />';
echo '<input type="text" name="email" id="email" value="' . $_email . '" /><br />';
echo '</form>';*/
echo '<form class="update" method="post" action="MT_vjones_updateRecord.php?id=' . $_id . '">';
echo '<div class="yesNo"><input class="radio" type="radio" name="confirm" value="Yes" /> Yes </div><br />';
echo '<div class="yesNo"><input class="radio" type="radio" name="confirm" value="No" checked="checked" /> No </div><br /><br />';
echo '<input class="applyBtn" type="submit" value="UPDATE" name="submit" />';
echo '<input type="hidden" name="id" value="' . $_id . '" />';
echo '<input type="hidden" name="firstName" value="' . $_firstName . '" />';
echo '<input type="hidden" name="lastName" value="' . $_lastName . '" />';
echo '<input type="hidden" name="email" value="*testBACK2FUN@test.com*" />';
}
echo '<p><a href="MT_vjones_adminAccess.php">&lt;&lt; Back to the Admin Page</a></p>';

正如您所看到的,我们在那里输入了电子邮件地址以供测试......

4 个答案:

答案 0 :(得分:0)

  $_query = "UPDATE midterm " .
            "SET email = '$_email' WHERE id = '$_id'" ;

应该是

$_query = "UPDATE midterm " .
          "SET email = $_email".
          "WHERE id = $_id " ;

答案 1 :(得分:0)

检查id是否与您要更新的内容相匹配。 确保在更新之前和之后打印$ _id和$ _email。

@ user710502:您不需要在PHP中使用双引号分隔引号。它无论如何都会读它,你唯一可能会打扰的是你是从数组中读取的 例如:

"UPATE midterm SET email='".$POST['email']."'"

答案 2 :(得分:0)

原因是您在变量之前使用$_,这不是有效的变量声明。

因为$_是为SUPER GLOBAL中的(i.e $_SESSION,$_SERVER,$_POST,$_GET,$_COOKIE etc)保留的。{/ p>

如果它不是你的问题,那么你需要连接你的变量,如下所示。

$_query = "UPDATE midterm SET email = '".$_email."' WHERE id = '".$_id."'" ;

答案 3 :(得分:0)

解决了!表格问题。

应该是:

<?php
require_once('appVars6.php');
require_once('connectVars6.php');

$_dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

$_id = $_GET['id'];
$_queryOne = "SELECT * FROM midterm WHERE id = '$_id'";
$_resultOne = mysqli_query($_dbc, $_queryOne) or die ('Error Querying Database');

while ($_row = mysqli_fetch_array($_resultOne)) {
    echo '<form class="update" method="post" action="MT_vjones_udpateRecord.php?id=' . $_id . '">';
    echo '<input type="hidden" name="id" id="id" value="' . $_row['id'] . '" />';
    echo '<input type="hidden" name="firstName" id="firstName" value="' . $_row['firstName'] . '" />';
    echo '<input type="hidden" name="lastName" id="lastName" value="' . $_row['lastName'] . '" />';
    echo '<input type="hidden" name="email" id="email" value="' . $_row['email'] . '" />';
    echo '</form>';
}

if ( isset($_GET['firstName']) && isset($_GET['lastName']) && isset($_GET['email'])) {
    $_id = $_GET['id'];
    $_firstName = $_GET['firstName'];
    $_lastName = $_GET['lastName'];
    $_email = $_GET['email'];   
}

    else if ( isset($_POST['firstName']) && isset($_POST['lastName']) && isset($_POST['email'])) {
        $_id = $_POST['id'];
        $_firstName = mysqli_real_escape_string($_dbc, trim($_POST['firstName']));
        $_lastName = mysqli_real_escape_string($_dbc, trim($_POST['lastName']));
        $_email = mysqli_real_escape_string($_dbc, trim($_POST['email']));
    }

        else {
                echo '<br />';
                echo '<p class="error">Sorry, no record was selected.</p>';
        }

if(isset($_POST['submit'])) {
    if ($_POST['confirm'] == 'Yes') {

    $_query = "UPDATE midterm " .
                "SET email = '$_email'" .
                "WHERE id = $_id" ;

    $_result = mysqli_query($_dbc, $_query) or die (mysqli_error($_dbc));

    mysqli_close($_dbc);
    echo '<p>The record of ' . $_firstName . ' ' . $_lastName . ' for ' . $_email . ' was successfully updated.';
    }

        else {
            echo '<p class="error">The record was not updated.</p>';
        }
}

    else if (isset($_id) && isset($_firstName) && isset($_lastName) && isset($_email)) {
        echo '<p>Are you sure you want to update the following record?</p>';
        echo '<form class="update" method="post" action="MT_vjones_updateRecord.php?id=' . $_id . '">';
        echo '<div class="yesNo"><input class="radio" type="radio" name="confirm" value="Yes" /> Yes </div><br />';
        echo '<div class="yesNo"><input class="radio" type="radio" name="confirm" value="No" checked="checked" /> No </div><br /><br />';
        echo '<input type="hidden" name="id" value="' . $_id . '" />';
        echo '<input type="text" name="firstName" value="' . $_firstName . '" /><br />';
        echo '<input type="text" name="lastName" value="' . $_lastName . '" /><br />';
        echo '<input type="text" name="email" value="' . $_email . '" />';
        echo '<input class="applyBtn" type="submit" value="UPDATE" name="submit" />';
    }

echo '<p><a href="MT_vjones_adminAccess.php">&lt;&lt; Back to the Admin Page</a></p>';
?>