dsc80135
2014-04-02 22:28
浏览 74
已采纳

AJAX / jQuery / PHP / MySQL - 如何更新数据库

I need to update a database using AJAX so don't have my page be reloaded. I can't find what's wrong and unexpectedly I get a success message back but a database doesn't get updated.

JS:

$('.start-time-class').submit(function() {                  

var startTime = "11:30";                
var projectID = 17;             
var userID = 2;             

$.ajax({
 url:'functions/starttime.php',
 data:{startTime:startTime,projectID:projectID,userID:userID},  // pass data 
 dataType:'json',
 success:function(){
  // something
 }                      
});         

});

PHP:

$con = mysqli_connect('localhost','smt','smt','smt');
if (!$con)
{
die('Could not connect: ' . mysqli_error($con));
}

$startTime = $_GET['startTime'];
$projectID = $_GET['projectID'];
$userID = $_GET['userID'];

mysqli_select_db($con,"ajax_demo");
$sql = "INSERT INTO 'uc_project_time'('userID', 'projectID', 'startTime') VALUES (". $userID .", ". $projectID .", ". $startTime .")";

$result = mysqli_query($con,$sql);      

mysqli_close($con);

图片转代码服务由CSDN问答提供 功能建议

我需要使用AJAX更新数据库,所以不要重新加载我的页面。 我找不到什么是错的,出乎意料的是我收到了成功的消息,但是数据库没有得到更新。

JS:

  $('。start-time-class')。submit(function(){
 
var startTime =“11:30”; 
var projectID = 17; 
var userID = 2; 
 
 $ .ajax  ({
 url:'functions / starttime.php',
 data:{startTime:startTime,projectID:projectID,userID:userID},//传递数据
 dataType:'json',
 success:function(  ){
 // something / n} 
}); 
 
}); 
   
 
 

PHP: < pre> $ con = mysqli_connect('localhost','smt','smt','smt'); if(!$ con) { die('无法连接:'。mysqli_error($ con)); } $ startTime = $ _GET ['startTime']; $ projectID = $ _GET ['projectID']; $ userID = $ _GET ['userID']; mysqli_select_db($ con,“ajax_demo”); $ sql =“INSERT INTO'uc_project_time'('userID','projectID','startTime')VALUES(”。$ userID。“,”。$ projectID。“ ,“。$ startTime。”)“; $ result = mysqli_quer Y($ CON,$ SQL); mysqli_close($ con);

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doubi2014 2014-04-02 22:34
    已采纳

    Don't use quotes for table or column names

    Use:

    $sql = "INSERT INTO uc_project_time (userID, projectID, startTime) VALUES ('$userID', '$projectID', '$startTime')";
    

    or

    $sql = "INSERT INTO uc_project_time (userID, projectID, startTime) VALUES ('".$userID."', '".$projectID."', '".$startTime."')";
    

    And do sanitize your code:

    评论
    解决 无用
    打赏 举报
  • 查看更多回答(1条)

相关推荐 更多相似问题