检查值是否已存在不起作用

时间:2013-05-24 12:22:23

标签: php mysql

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

test.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());   
    }
}

1 个答案:

答案 0 :(得分:0)

通过更改

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

到一个javascript函数

谢谢!