0

我有一个功能,如果您在下拉菜单中选择一个值,我的代码会使用所选值更新数据库。现在我尝试添加一个检查来检查数据库中是否已经存在该值。但它不起作用。我究竟做错了什么?

测试.php

<html>
<head>
    <script type="text/javascript" src="js/jquery.js"></script> 
    <link href="style.css" rel="stylesheet" type="text/css">
    <script>
    function updateDb() {
     $.post("buh.php", $("#form").serialize());
    }
    </script>
</head>
<body>
    <form id="form">    
    <?php
        include 'Includes/database_connection.php';
        $sql = "select * FROM sims ORDER BY phonenr asc"    ;
        $result = mysql_query($sql,$con);       
        $id1 = $_GET["id1"];
        echo "<input type='hidden' id='hidden' name='hidden' value='$id1'>";
        echo "<select id='select' name='select' onChange='updateDb()'>";
        while ($row = mysql_fetch_array($result)) {
            echo "<option value='" . $row['phonenr'] . "'>" . $row['phonenr'] . "</option>";
        }
        echo "</select>";
    ?>
    </form>
    <?php
    echo '<a href="javascript:window.close()">Sluit venster</a>';  
    ?>
<body>

Buh.php

<?php
include 'Includes/database_connection.php';
$sql = "select * FROM sims WHERE phonenr='".mysql_real_escape_string($_POST["select"])."'"  ;
$result = mysql_query($sql,$con);
while ($row = mysql_fetch_array($result)) {
    $id = $row['id'];
    $id1 = mysql_real_escape_string($_POST["hidden"]);
    $checkID = mysql_query("SELECT sim_id from pairings WHERE sim_id = '$id'");
    if (mysql_num_rows($checkID) > 0) {
       if(confirm("Simkaart is al gekoppeld weet u zeker dat u deze wilt vervangen?")){
        mysql_query("UPDATE pairings SET sim_id='$id' WHERE unit_id='$id1'")
        or die(mysql_error());   
        }
    }
    else {
        mysql_query("UPDATE pairings SET sim_id='$id' WHERE unit_id='$id1'")
            or die(mysql_error());   
    }
}
4

1 回答 1

0

通过更改修复它

 if(confirm("Simkaart is al gekoppeld weet u zeker dat u deze wilt vervangen?")){

到一个 javascript 函数

谢谢!

于 2013-05-24T12:35:55.357 回答