2015-07-12 55 views
0

我试图做我的第一次AJAX调用,我想要做的事很简单,但我的数据库没有被更新。使用特定ID更新数据库的AJAX调用

我想要做的就是当我点击用户旁边的接受按钮,他们的身份证件将被接收并以新状态“已接受”发送,状态从“待处理”更改为“已接受” '为我的user_requests数据库表中的特定用户。

数据库中没有任何内容正在改变,唯一发生在AJAX代码中的事情是我获得了我的#success消息,但可能是0.3秒,并且它不会淡出。

有没有人看到我在做什么错在我的尝试?

<h2>Pending User Requests</h2> 
<br /> 
<div id="success" style="color: red;"></div> 
<?php 
    $con = mysqli_connect("localhost", "root", "", "db"); 
    $run = mysqli_query($con,"SELECT * FROM user_requests ORDER BY id DESC"); 
    $numrows = mysqli_num_rows($run); 

    if($numrows) { 
     while($row = mysqli_fetch_assoc($run)){ 
      //comment added by php-dev : condition could be set in the query --> 
      if($row['status'] == "Pending"){ 

       $pending_id = $row['id']; 
       $pending_user_id = $row['user_id']; 
       $pending_firstname = $row['firstname']; 
       $pending_lastname = $row['lastname']; 
       $pending_username = $row['username']; 

?> 

      <!-- comment added by php-dev : useless form tag --> 
      <form action="" method="POST" id="status"> 
       <!-- comment added by php-dev : useless input field, no field name --> 
       <input type='hidden' value='<?php echo $pending_id; ?>' id='pending_id' /> 
      <?php 
       // comment added by php-dev : comparing string to boolean value true 
       if ($pending_firstname == true) { 
        echo "Name - ". $pending_firstname . " " . $pending_lastname . "</br>" 
         . "Username - ". $pending_username . "</br></br>" 
      ?> 
      <!-- comment added by php-dev : conditional form closing tag --> 
      </form> 
      <button class="approve" type="submit" form="status" name="approve" 
        value="<?= $pending_id; ?>"> 
       Approve 
      </button> 
      <button id="deny" type="submit" form="status" name="deny" value="Denied"> 
       Deny 
      </button> 
      <br><br><br> 
     <?php 
       // comment added by php-dev : else statement misplaced --> 
       ;} else { 
        echo "There are no Pending Requests at this time."; 
       } 
      } 
     } 
    } 
?> 

我的AJAX调用...

<script> 
    $(document).ready(function(){ 
    $('.approve').click(function(){ 
     $.ajax({ 
      url: 'userRequest_approve.php', 
      data: { 
       id: $(this).val(), //the value of what you clicked on 
       //you clicked on it so you know the status might as well hardcode it 
       status: 'Approved' 
      }, 
      success: function(data) { 
       //do something with the data that got returned 
       // comment added by php-dev : for debug purposes, the #success should show 
       // the server reponse instead 
       $('#success').html('User Status Changed!'); 
       //do something with the data that got returned 
       $('#success').delay(5000).fadeOut(400); 
      }, 
      type: 'POST' 
     }); 
    }); 
    }); 
</script> 

我userRequest_approve.php文件插入到数据库更新状态...

<?php 
require_once 'core/init.php'; 

$term = mysql_escape_string($term); // Attack Prevention 
$pending_id = $_POST['id']; 
$status = $_POST['approve']; 

$con = mysqli_connect("localhost","root","","db"); 
    /* check connection */ 
    if (mysqli_connect_errno()) { 
     printf("Connect failed: %s\n", mysqli_connect_error()); 
     exit(); 
    } 
    $stmt = $con->prepare(
     "INSERT INTO user_requests (status, date_responded) VALUES (?, NOW())" 
    ); 
    if (false===$stmt) { 
    // Check Errors for prepare 
     die('User Request update prepare() failed: ' . htmlspecialchars($con->error)); 
    } 
    $stmt->bind_param('s', $status); 
    // comment added by php-dev : should be false === $stmt->bind_param ... 

    if (false===$stmt) { 
    // Check errors for binding parameters 
     die('User Request update bind_param() failed: ' . htmlspecialchars($stmt->error)); 
    } 
    $stmt->execute(); 
    // comment added by php-dev : should be false === $stmt->execute ... 
    if (false===$stmt) { 
     die('User Status update execute() failed: ' . htmlspecialchars($stmt->error)); 
    }  
?> 
+0

我通过格式化更多可读性更新你的答案。 我还在代码中添加了一些注释,请检查它们。 –

回答

1

如果要更新,你应该试试这个:

$stmt = $con->prepare("UPDATE user_requests SET status=?, date_responded=NOW() WHERE id=?"); 
$stmt->bind_param('si', $status, $pending_id); 

你还需要一个名字attribu TE对你隐藏得那么它会被发送:

<input type='hidden' name='id' value='<?php echo $pending_id; ?>' id='pending_id'/> 

原来的答案

我只看到一个问题:

这是你使用Ajax请求:

$.ajax({ 
     url: 'userRequest_approve.php', 
     data: { 
      id: $(this).val(), //<< id 
      status: 'Approved' //<< status 
     }, 
     success: function(data) { 
      //do something with the data that got returned 
      $('#success').html('User Status Changed!'); 
      $('#success').delay(5000).fadeOut(400);//do something with the data that got returned 
     }, 
     type: 'POST' 
    }); 

请注意,您发送的数据是idstatus

然而,在PHP端:

$pending_id = $_POST['id']; //yep 
$status = $_POST['approve']; //does it exist? 

您应该使用

$status = $_POST['status']; 
+0

好吧,它终于发送了一些东西到我的分贝,但所有的信息是空白,除了我的自动增量ID字段,批准状态和日期。它没有使用当前用户的ID从他们那里获取信息? – Paul

+0

它也插入一个新的列行而不是更新现有的用户。 – Paul

+0

但您未执行更新。你只是在数据库中插入数据。 – Mindastic