jQuery-ajax-post-to-php脚本&;更新mysql数据库

jQuery-ajax-post-to-php脚本&;更新mysql数据库,php,mysql,ajax,Php,Mysql,Ajax,我有一个显示库存的页面。当库存变得有缺陷(或任何其他状态)时,我想从下拉列表中选择该选项,并相应地更新数据库,而无需刷新页面。我一直在玩弄.ajax和jQuery,我想我已经很接近了,我只是不能让它正常工作 首先,下面是填充下拉列表的代码。这段代码(在我的站点的其他地方使用)没有问题。尽管出于姓名和身份的目的我会包括 $myQuery2 = "SELECT InventoryItemStatus.InventoryItemStatusID, InventoryItemStatus.Invento

我有一个显示库存的页面。当库存变得有缺陷(或任何其他状态)时,我想从下拉列表中选择该选项,并相应地更新数据库,而无需刷新页面。我一直在玩弄.ajax和jQuery,我想我已经很接近了,我只是不能让它正常工作

首先,下面是填充下拉列表的代码。这段代码(在我的站点的其他地方使用)没有问题。尽管出于姓名和身份的目的我会包括

$myQuery2 = "SELECT InventoryItemStatus.InventoryItemStatusID, InventoryItemStatus.InventoryItemStatusDescription FROM InventoryItemStatus";
$myConnection = mysql_connect($myServer,$myUsername,$myPassword);
if (!$myConnection){
    die('Conncetion Failed:' . mysql_error());
}
@mysql_select_db($myDatabase) or die("Unable to select database");
$result2 = mysql_query($myQuery2) or die (mysql_error());

if(mysql_num_rows($result2)){ 
    $select2= '<select style="width:90px" id="StatusList" name="StatusList">';
    $select2.='<option value="0.5"> --Status-- </option>';
        while($rs=mysql_fetch_array($result2)){
            $select2.='<option value="'.$rs['InventoryItemStatusID'].'">'.$rs['InventoryItemStatusDescription'].'</option>';
            }
} 
$select2.='</select>';
如您所见,我只是尝试将库存项目id传递给脚本,并将该项目更新为“2”状态。我的最终结果是将其更新为下拉菜单的任何值,但我确信,如果我能让这部分正常工作,我就能找出其余部分

提前感谢您的帮助! 博恩·乔伊

编辑

这个很好用,非常感谢。不幸的是,我遇到了另一个问题。例如,我有10个库存项目。我执行复杂查询以检索数组中所需的所有信息,然后执行foreach。顶部的库存项似乎可以正确地更新状态,但它下面的任何项都不会执行更新的sql查询

if (sizeof($rows14) > 0) {
foreach($rows14 as $row14):

$query17 = "
SELECT 
    SystemID,
    Serial
FROM 
    Device 
WHERE 
    Device.InventoryItemID = :InventoryItemID
";
$query_params17 = array( 
    ':InventoryItemID' => $row14['InventoryItemID']
);
try { 
    $stmt17 = $db->prepare($query17); 
    $stmt17->execute($query_params17); 
} 
catch(PDOException $ex) { 
    die("Failed to run query: " . $ex->getMessage()); 
}
$row17 = $stmt17->fetch();


$myQuery2 = "SELECT InventoryItemStatus.InventoryItemStatusID, InventoryItemStatus.InventoryItemStatusDescription FROM InventoryItemStatus";
$result2 = mysql_query($myQuery2) or die (mysql_error());
if(mysql_num_rows($result2)){ 
    $select2= '<select style="width:90px" name="StatusList">';
        while($rs=mysql_fetch_array($result2)){
            if($rs['InventoryItemStatusID'] == $row14['Status']){ 
                $select2.='<option value="'.$rs['InventoryItemStatusID'].'" selected="'.$row14['Status'].'">'.$rs['InventoryItemStatusDescription'].'</option>'; 
            }
            else{
                $select2.='<option value="'.$rs['InventoryItemStatusID'].'">'.$rs['InventoryItemStatusDescription'].'</option>';                        
            }
            }
} 
$select2.='</select>';

echo"
<tr class='tableRowClass2'>
<td height='20'>{$row14['OnHand']}</td>
<td height='20'>{$row14['ItemName']} - {$row17['SystemID']} - {$row17['Serial']}</td>
<td height='20'>
    <form method='post'>
    {$select2}
    <input type='hidden' value='{$row14['InventoryItemID']}' name='StarmontInventoryItemID'/>
    </form>
</td>
</tr>";
endforeach;
}
if(sizeof($rows4)>0){
foreach(第14行为第14行):
$query17=”
挑选
系统ID,
电视连续剧
从…起
装置
哪里
Device.InventoryItemID=:InventoryItemID
";
$query_params17=数组(
':InventoryItemID'=>$row14['InventoryItemID']
);
试试{
$stmt17=$db->prepare($query17);
$stmt17->execute($query\u params17);
} 
捕获($ex){
die(“无法运行查询:”..ex->getMessage());
}
$row17=$stmt17->fetch();
$myQuery2=“从InventoryItemStatus中选择InventoryItemStatus.InventoryItemStatusID、InventoryItemStatus.InventoryItemStatusDescription”;
$result2=mysql\u query($myQuery2)或die(mysql\u error());
如果(mysql_num_rows($result2)){
$select2='';
而($rs=mysql\u fetch\u数组($result2)){
如果($rs['InventoryItemStatusID']=$row14['Status']){
$select2.=''.$rs['InventoryItemStatusDescription'].';
}
否则{
$select2.=''.$rs['InventoryItemStatusDescription'].';
}
}
} 
$select2.='';
回声“
{$row14['OnHand']}
{$row14['ItemName']}-{$row17['SystemID']}-{$row17['Serial']}
{$select2}
";
endforeach;
}

我已经尝试了多种不同的方法,以使其适用于所有库存项目。我不确定将其放入foreach循环是否有效。再次感谢您的帮助

现在,您正在将ajax调用中的值作为一个简单字符串发送,您希望它是一个键/值对

更改此项:

data: mydata
对这样的事情:

data: {'StarmontInventoryItemID' : mydata}
现在服务器端您应该能够访问
$\u POST['StarmontInventoryItemID']

请注意,您始终可以执行
print\r($\u POST)
以查看POST数据是如何到达服务器端的


Obgligatory注意:您正在使用不推荐的
mysql.*
方法,需要切换到
mysqli.*
PDO
。您的查询也是不安全的,并且对SQL注入开放。

您必须编辑JS

 $(document).ready(function(){
  $("#StatusList").change(function() {
    var mydata = $(this).val();
    var inputdata = $("#StarmontInventoryItemID").val();
    $.ajax({   
       type: 'POST',   
       url: 'update_status/update_starmont.php',   
       data: {StatusList:mydata ,StarmontInventoryItemID:inputdata }
    });
  });
});
在jQueryAjax中,cal数据应该是一个映射{myvar:myvalue},其中myvar是php代码$\u POST['myvar']

 success: success
success是一个函数,如果您有一个成功的ajax调用,它将被执行,您也可以这样使用它

success: function(data) { console.log(data) ... }
你很容易受到伤害
 $(document).ready(function(){
  $("#StatusList").change(function() {
    var mydata = $(this).val();
    var inputdata = $("#StarmontInventoryItemID").val();
    $.ajax({   
       type: 'POST',   
       url: 'update_status/update_starmont.php',   
       data: {StatusList:mydata ,StarmontInventoryItemID:inputdata }
    });
  });
});
 success: success
success: function(data) { console.log(data) ... }