AJAX数据库问题

时间:2013-12-11 12:45:27

标签: php jquery ajax postgresql

我正在尝试更新表中的列,我将布尔值从false更改为true。我正在使用AJAX将数据发送到php文件,然后php文件执行数据库更新查询。 到目前为止我有

if (r == true)
{
    mypic.style.visibility = 'hidden';
    leftbox.style.border = '3px solid white';
    //pressed ok DO AJAX STUFF HERE
    var data = true;
    $.post('filetoupdate.php', data, function(data) {
        //Here you can get the output from PHP file which is (data) here
    });
}
else
{
    //return pill to clock
}

<?php 
require_once "connect.php";
    echo
   pg_query("UPDATE usermeds SET taken=true WHERE id=21");


?>

连接脚本肯定有效。代码不能用于某种原因不确定为什么? action.js:

function doFirst(){
    mypic = document.getElementById('pillpic');
    mypic.addEventListener("dragstart", startDrag, false);
    mypic.addEventListener("dragend", endDrag, false);
    leftbox = document.getElementById('mouth');
    leftbox.addEventListener("dragenter", dragenter, false);
    leftbox.addEventListener("dragleave", dragleave, false);
    leftbox.addEventListener("dragover", function(e){e.preventDefault();}, false);
    leftbox.addEventListener("drop", dropped, false);

}
function endDrag(e){
    mypic = e.target;
    mypic.style.visibility = 'visible';
}
function dragenter(e){
    e.preventDefault();
    leftbox.style.border = '3px solid red';

}
function dragleave(e){
    e.preventDefault();
    leftbox.style.border = '3px solid white';
}
function startDrag(e){
    var code = '<img src="clock/pill.png" alt="pill image" id="pillpic"/>';
    e.dataTransfer.setData('Text', code);
}
function dropped(e){
    e.preventDefault();
    var r=confirm("Are you sure you want to take this pill?");
    if (r == true)
    {
        mypic.style.visibility = 'hidden';
        leftbox.style.border = '3px solid white';
        //pressed ok DO AJAX STUFF HERE
        var data = true;
        $.post('js/filetoupdate.php', data, function(data) {
            //Here you can get the output from PHP file which is (data) here
        });
    }
    else
    {
        //return pill to clock
    }


}
window.addEventListener("load", doFirst, false);
init();
function init(){
var clock = document.getElementById('clock');
var currentdate = new Date();
var datetime = currentdate.getHours();
if(datetime==1||datetime==13){
clock.style.backgroundImage="url(clock/clock1.png)";
}
else if(datetime==2||datetime==14){
clock.style.backgroundImage="url(clock/clock2.png)";
}
else if(datetime==3||datetime==15){
clock.style.backgroundImage="url(clock/clock3.png)";
}
else if(datetime==4||datetime==16){
clock.style.backgroundImage="url(clock/clock4.png)";
}
else if(datetime==5||datetime==17){
clock.style.backgroundImage="url(clock/clock5.png)";
}
else if(datetime==6||datetime==18){
clock.style.backgroundImage="url(clock/clock6.png)";
}
else if(datetime==7||datetime==19){
clock.style.backgroundImage="url(clock/clock7.png)";
}
else if(datetime==8||datetime==20){
clock.style.backgroundImage="url(clock/clock8.png)";
}
else if(datetime==9||datetime==21){
clock.style.backgroundImage="url(clock/clock9.png)";
}
else if(datetime==10||datetime==22){
clock.style.backgroundImage="url(clock/clock10.png)";
}
else if(datetime==11||datetime==23){
clock.style.backgroundImage="url(clock/clock11.png)";
}
else if(datetime==0||datetime==12){
clock.style.backgroundImage="url(clock/clock12.png)";
}
}

html:

    <title> Homepage </title>
    <link rel="stylesheet" type="text/css" href="stylesheet.css">
    <style type="text/css">
    #mouth {
    position: absolute;
    left: 491px;
    top: 551px;
    right: auto;
    bottom: auto;
    width: 214px;
    height: 218px;
}
    #message {
    position: absolute;
    left: 836px;
    top: 321px;
    right: auto;
    bottom: auto;
}
    #message2 {
    position: absolute;
    left: 826px;
    top: 379px;
    right: auto;
    bottom: auto;
}
    #addbutton {
    position: absolute;
    left: 873px;
    top: 197px;
    right: auto;
    bottom: auto;
}
    #clock {
    background-color: #FFF;
    background-image: url(clock/clock1.png);
    height: 398px;
    width: 457px;
    position: absolute;
    left: 781px;
    top: 415px;
    right: auto;
    bottom: auto;
}
    #wrapper #photoslider #appframe #clock img {
    height: 30px;
    width: 30px;
    position: absolute;
    left: 308px;
    top: 154px;
    right: auto;
    bottom: auto;
}
    </style>
    <script type="text/javascript" src="js/jquery-1.10.2.js"></script>
    <script type="text/javascript" src="js/yail.1.4.js"></script>
    <script type="text/javascript" src="js/action.js"></script>

</head>

<body>
    <div  id="wrapper">
        <div id="header">
            <h1> Medomind </h1>
            <div id="login">
            <form>
            Username: <input type="text/email" name="user"><br>
            Password:  <input type="password" name="password"><br>
            <button name="action" value="send" type="submit">Log In</button> 
            Sign Up? 
            </form>

        </div>
        </div>

        <div id ="testimonials"></div>
        <div id ="photoslider">
        <!--APP CONTENT STARTS HERE-->
        <!--APP CONTENT STARTS HERE-->
        <!--APP CONTENT STARTS HERE-->
        <div id="appframe">
        <div id="addbutton"><a href="http://google.com"><img src="images/addbutton.png" alt="add new medication image"/></a></div>
        <div id="message"><h2>Todays medication</h2></div>
        <div id="message2"><h3>Drag and drop to take medication</h3></div>
        <div id="mouth"><img src="images/mouth.png" alt="mouth image"/></div>
        <div id="clock"><img src="clock/pill.png" alt="pill image" id="pillpic"/></div>
        </div>
        <!--APP CONTENT ENDS HERE-->
        <!--APP CONTENT ENDS HERE-->
        <!--APP CONTENT ENDS HERE-->
        </div>

    </div>
        <div id ="footer">
        Team C CS353
    </div>
</body>


</html>

1 个答案:

答案 0 :(得分:1)

<?php

$connect = pg_connect("host=localhost dbname=postgres user=postgres password=pass");
$query = "UPDATE usermeds SET taken=true WHERE id=21";

$result = pg_query($connect ,$query );

if ($result) {
    echo "Record updated";
}
else {
    echo "Error occured";
}
pg_close($connect);

&GT;