从JS文件调用PHP文件并插入数据库

时间:2017-06-11 14:29:45

标签: javascript php ajax oracle

我正在我的教员做一个项目,基本上我们正在复制一个PAC-MAN游戏。我试图通过PHP文件将高分数插入Oracle数据库,命名为insertintotable.ph并尝试使用Ajax以便我可以更新它。问题是没有任何反应。

以下是javascript代码:

function score(s, type) { 

var scoreBefore = (SCORE / 10000) | 0;

SCORE += s;
if (SCORE === 0) { 
    $('#score span').html("00");
} else { 
    $('#score span').html(SCORE);
}

var scoreAfter = (SCORE / 10000) | 0;
if (scoreAfter > scoreBefore) { 
    lifes( +1 );
}


if (SCORE > HIGHSCORE) { 
    HIGHSCORE = SCORE;
    if (HIGHSCORE === 0) { 
        $('#highscore span').html("00");
    } else { 
        $.ajax({
    url: '/inserintotable.php',
    type: 'POST',
    dataType: "json",
    data: {
        score: HIGHSCORE.val(),
    }
}).done(function(data){
        alert(JSON.stringify(data));
});
    }
}

if (type && (type === "clyde" || type === "pinky" || type === "inky" || type === "blinky") ) { 
    erasePacman(); 
    eraseGhost(type); 
    $("#board").append('<span class="combo">' + SCORE_GHOST_COMBO + '</span>');
    $("#board span.combo").css('top', eval('GHOST_' + type.toUpperCase() + '_POSITION_Y - 10') + 'px');
    $("#board span.combo").css('left', eval('GHOST_' + type.toUpperCase() + '_POSITION_X - 10') + 'px');
    SCORE_GHOST_COMBO = SCORE_GHOST_COMBO * 2;
} else if (type && type === "fruit") { 
    $("#board").append('<span class="fruits">' + s + '</span>');
    $("#board span.fruits").css('top', (FRUITS_POSITION_Y - 14) + 'px');
    $("#board span.fruits").css('left', (FRUITS_POSITION_X - 14) + 'px');
}

}

这是php文件:

      <?php

  // Query pentru crearea tabelei : CREATE TABLE HighScores (NumeUser varchar2(25),Scor int NOT NULL);
  session_start();
  $userName = isset($_SESSION['userName']) ? $_SESSION['userName'] : NULL;
  $conn = oci_connect('vladut', 'LAB11', 'localhost/XE');
  if (!$conn) {
     $e = oci_error();
     trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
 }

  $stid = oci_parse($conn, 'INSERT INTO HighScores VALUES(:user, :scor)');
  //$user=_$GET["username"]; //din jquery primeste username-ul
  $scor=_$POST["score"]; //idem
  oci_bind_by_name($stid, ':user', $userName);
  oci_bind_by_name($stid, ':scor', $scor);

  oci_execute($stid); 

  oci_free_statement($stid);
  oci_close($conn);

  ?>

1 个答案:

答案 0 :(得分:0)

在您的php文件中检查您使用的14_$POST["score"]是否不正确。您必须使用此

修改此行14代码
$scor=$_POST["score"]; //idem

您还可以使用以下代码添加json_decode函数

<?php
// Query pentru crearea tabelei : CREATE TABLE HighScores (NumeUser varchar2(25),Scor int NOT NULL);
session_start();
$userName = isset($_SESSION['userName']) ? $_SESSION['userName'] : NULL;
$conn = oci_connect('vladut', 'LAB11', 'localhost/XE');

if (!$conn) {
    $e = oci_error();
    trigger_error(htmlentities($e['message'], ENT_QUOTES) , E_USER_ERROR);
}

$stid = oci_parse($conn, 'INSERT INTO HighScores VALUES(:user, :scor)');
// $user=_$GET["username"]; //din jquery primeste username-ul
$score = json_decode($_POST['score']);
$scor = $score->{'score'}; //idem
oci_bind_by_name($stid, ':user', $userName);
oci_bind_by_name($stid, ':scor', $scor);
oci_execute($stid);
oci_free_statement($stid);
oci_close($conn);
?>