PHP update()MySQL数据库不工作

时间:2018-01-25 18:16:11

标签: php mysql sql database web

我正在进行一项测验,该测验最后要求用户提供人口统计信息,但我遇到了一些问题,正确填充了我收集的信息。

以下代码是我的databaseAdapator.php文件的一部分。第一个,initUser(),在我的数据库中设置一个用户条目,并将所有内容初始化为0 - 这正在发生/我可以在MySQL中看到这些值。

udpateUser()是我遇到问题的地方。调用它的页面(addUser.php)具有该函数所需的所有信息,但是当我调用它时,数据库中没有任何变化。

基本上,我想执行以下命令:

update users set age=11, gender="Male", height_cm=2 where user_id=95;

我的代码

public function initUser($age, $gender, $height, $weight, $ip){
    //create new dummy user
    $stmt = $this->DB->prepare("Insert into users values(null, :age, :gender, :height, :weight, :ip)");
    $stmt->bindParam('age', $age);
    $stmt->bindParam('gender', $gender);
    $stmt->bindParam('height', $height);
    $stmt->bindParam('weight', $weight);
    $stmt->bindParam('ip', $ip);
    $stmt->execute();

    //select last insert ID
    $stmt = $this->DB->prepare("SELECT LAST_INSERT_ID() from users");
    $stmt->execute();
    $userId = $stmt->fetch();
    return $userId[0];
}


public function updateUser($user, $age, $gender, $height, $weight){
    //edit the information for a user after demographics page
    $stmt = $this->DB->prepare("UPDATE users SET age=:age, gender=:gender, height_cm=:height, weight_kg=:weight WHERE user_id=:user");
    $stmt->bindParam('age', $age);
    $stmt->bindParam('gender', $gender);
    $stmt->bindParam('height', $height);
    $stmt->bindParam('weight', $weight);
    $stmt->bindParam('user', $user);
    $stmt->execute();

    return $user;
}

这是addUser.php,即调用该函数的页面。我知道这是可怕的做法(对不起!),我试图让数据库适配器暂时正常工作,并且我已经手动检查了所有变量 - 它们都是有效且非空的。

<?php
require_once("./databaseAdaptor.php");
$user = $_GET["user"];

$age = $_POST["Age"];
$gender = $_POST["Gender"];
$height_in = $_POST['in'];
$height_ft = $_POST['ft'];
$height = 2.54 * (($height_ft * 12) + $height_in);
$weight = $_POST['lbs'];


$userId = $myDatabaseFunctions->updateUser($user, $age, $gender, $height, $weight);
?>

编辑: 这是我调用initUser.php

的地方
<?php
require_once("./databaseAdaptor.php");

$userId = $myDatabaseFunctions->initUser(0, null, 0, 0,  0);
?>

<form name='fr' action='quiz.php' method='POST'>
        <input type='hidden' name='user' value='<?=$userId?>'>
        <input type="submit" />
</form>


<script type='text/javascript'>
document.fr.submit();
</script>

这是demographic.php,其中包含为addUser.php收集信息的表单。转到quiz.php的第一个表单会向用户询问一系列问题,当它们到达最终时会将其发送到此处。查看数据库,他们的答案正在被记录,初始化似乎正在发生&#34;正确&#34;。我唯一不成功的是更新测验尾部的用户信息。

&#13;
&#13;
<?php
require_once("./databaseAdaptor.php");

$page = '0';

if( isset($_GET["user"])) {
        $user = $_GET["user"];
} else {
        echo "error";
$page = '1';
}
?>

<!DOCTYPE HTML5>
<html lang="en">

<body bgcolor="#0000">
<font color="white">

    <meta charset="UTF-8">
    <meta http-equiv="Content-Script-Type" content="text/javascript" />
    <title>FOOD CALORIES</title>
    <link rel="stylesheet" href="css/style.css"/>

<body>

    <div>
        <h4><a href="index.html">Calorie4Food</a></h4>
    </div>
    <br></br>
    <br></br>
        <center>



                        <form  method="post" action="./addUser.php?user=<?php echo $user?>" onsubmit="checkform()">
<h6>
                        <!-- Age Button -->
                <fieldset id="Age" >
                        Please Select Age: <br>
                        <input type="radio" value="10" name="Age" > 10<br>
                        <input type="radio" value="20" name="Age"> 11 - 20<br>
                        <input type="radio" value="30" name="Age"> 21 - 30<br>
                        <input type="radio" value="40" name="Age"> 31 - 40<br>
                        <input type="radio" value="50" name="Age"> 41 - 50<br>
                        <input type="radio" value="60" name="Age"> 51 - 60<br>
                        <input type="radio" value="60" name="Age">  > 60<br>
                </fieldset>

                        <!-- Gender Button -->
                        <fieldset id="Gender" >
                                Please Select Gender: <br>
                        <input type="radio" value="Male" name="Gender" > Male <br>
                        <input type="radio" value="Female" name="Gender"> Female <br>
                        <input type="radio" value="Other" name="Gender"> Other <br>
                        </fieldset>

                        <fieldset id="HeightWeight">
                                Please provide your height and weight so that we may calculate your bmi: <br></br>                              
                                <div id="calculator">
                                
                                <!-- Button for Metric/Standard conversion -->
                                <input id="toUS" type="button" class="btnsml" value="US" onclick="metrictous();" style="display:none;">
                                <input id="tometric" type="button" class="btnsml" value="metric" onclick="ustometric();" style="display:inline;">

                                <p id="height"></p>
                                <div id="usheight" style="display:inline;">
                                    <c>Height:</c>
                                <input type="text" name="ft" id="ft" size="1" maxlength="1" onkeyup="updateBMI();" value="0">
                                    <c>ft</c> <br></br>
                                <input type="text" name="in" id="in" size="2" maxlength="2" onkeyup="updateBMI();" value="0">
                                    <c>in</c>
                                </div>
                                <div id="metricheight" style="display:none;">
                                    <c>Height:</c>
                                <input type="text" name="cm" id="cm" size="3" maxlength="3" onkeyup="updateBMI();" value="0">
                                    <c>cm</c>
                                    </div>
                                <br></br>
                                <p id="weight"></p>
                                <div id="usweight" style="display:inline;">
                                    <label for="lbs"><c>Weight:</c></label>
                                <input type="text" name="lbs" id="lbs" size="3" maxlength="3" onkeyup="updateBMI();" value="0">
                                    <c>lbs</c>
                                          </div>

                                <div id="metricweight" style="display:none;">
                                    <label for="kg"><c>Weight:</c></label>
                                <input type="text" name="kg" id="kg" size="3" maxlength="3" onkeyup="updateBMI();" value="0">
                                    <c>kg</c>
                                    </div>
                                    <br></br>
                                <p></p>
                                    </div>
                                    </div>
                                <br></br> Thank you! 

                        </fieldset>

                        <br></br>
                        
                        <input type="hidden" value="<?=$user?>" name="user"/>
                        <input type="submit" value="Submit" name="submit"/>

                        </form>

</h6>

</body>
</html>
&#13;
&#13;
&#13;

所以一般流程是:initUser.php(调用databaseAdaptor),quiz.php,demographic.php,addUser.php(调用databaseAdaptor)。数据库中直到addUser.php的所有信息似乎都是正确的。

1 个答案:

答案 0 :(得分:1)

您在致电WITH MEMBER [Measures].[ParameterCaption] AS [Employee].[Employee Name].CURRENTMEMBER.MEMBER_CAPTION MEMBER [Measures].[ParameterValue] AS [Employee].[Employee ID].CURRENTMEMBER.UNIQUENAME MEMBER [Measures].[ParameterLevel] AS [Employee].[Employee ID].CURRENTMEMBER.LEVEL.ORDINAL SELECT {[Measures].[ParameterCaption] , [Measures].[ParameterValue] , [Measures].[ParameterLevel]} ON COLUMNS , [Employee].[Employee ID].ALLMEMBERS ON ROWS FROM ( SELECT ( STRTOSET(@ReportingCurrencyReportingCurrency, CONSTRAINED) ) ON COLUMNS FROM ( SELECT ( STRTOSET(@PROJGROUPProjGroup, CONSTRAINED) ) ON COLUMNS FROM ( SELECT ( STRTOSET(@CompanyCompanyAccounts, CONSTRAINED) ) ON COLUMNS FROM [ACTIVITY]))) 的网页上错误地提取了$userId

您将其称为:

updateUser()

但您的表单正在使用$user = $_GET["user"]; ,这意味着$_POST正在设置为$user。这意味着在执行脚本期间不会发生任何事情,因为查询 成功执行,没有列NULLuser_id 。所以你应该通过以下方式调用它:

NULL

这将使其与通过表单提交的内容相同。

故障排除

你似乎还有一些问题。以下是一些建议:

以不同方式使用PDO。尝试通过$user = $_POST["user"]; 传递所有变量并添加execute()标记,以帮助您找出问题所在。我还添加了一个<pre>,以便在点击此功能时它会终止页面,因此您实际上可以获得die()显示,这应该在排除故障后删除。

<pre>