繁体   English   中英

HTML PHP MYSQL如何检查数据是否存在然后确认更新

[英]HTML PHP MYSQL How to check if data exist then confirm update

我在程序中使用html php和mysql。 我希望它检查数据库中的记录,然后在不存在的记录上插入一条记录。 如果存在,它将仍然添加记录并将现有记录放在另一个表中。 但我要先确认用户,然后再做。

我认为我的代码有点蛮力,但是我拥有的全部。 谢谢。

<html>
<head>
  <title>Move Monitor</title>
  <?php require 'mysqlconnect.php'; 
  include('session.php');
  ?>
    <link rel="stylesheet" href="css/style.css">
  <meta charset="UTF-8">

</head>
<body>

<?php
if (isset($_POST['ws'])) {

    $ws=$_SESSION['todeploy'];
    $comp=$_POST['ws'];
    $daterecord=date("m/d/y");


    $sql = "SELECT count(workstation_id) from workstation where workstation_id = '$comp' ";

    $query = mysqli_query($conn,$sql);
    $row = mysqli_fetch_array($query);
    $rows = $row[0];

    if ($rows<1) {
        $sql="insert into workstation (workstation_id,monitor) values ('$comp','$ws')";
        if (!mysqli_query($conn,$sql))
        {
        die('Error adding ws' . mysql_error());
        }

        $sql="UPDATE computer_unit set date_recorded= '$daterecord' , deployed_stocked='deployed' status ='OK' where asi_code = '$ws' " ;
        if (!mysqli_query($conn,$sql))
        {
        die('Error qupdate unit' . mysql_error());
        }

    }else{

    $sql = "SELECT monitor from workstation where workstation_id = '$comp' ";

    $query = mysqli_query($conn,$sql);
    $row = mysqli_fetch_array($query);
    $monitor = $row['monitor'];

    if ($monitor=='') {

            $sql="UPDATE workstation set monitor = '$ws' where workstation_id = '$comp'";

            if (!mysqli_query($conn,$sql))
            {
            die('Error upd ws' . mysql_error());
            }

            $sql="UPDATE computer_unit set date_recorded= '$daterecord' , deployed_stocked='deployed' , status='OK' where asi_code = '$ws' " ;
                if (!mysqli_query($conn,$sql))
                {
                die('Error qupdate unit' . mysql_error());
                }
    }else{

        $sql="UPDATE computer_unit set date_recorded = '$daterecord' , deployed_stocked='stocked' where asi_code = '$monitor' " ;
                if (!mysqli_query($conn,$sql))
                {
                die('Error unit deployed' . mysql_error());
                }
        $sql="UPDATE computer_unit set date_recorded= '$daterecord' , deployed_stocked='deployed' , status ='OK' where asi_code = '$ws' " ;
                if (!mysqli_query($conn,$sql))
                {
                die('Error unit stocked' . mysql_error());
                }
        $sql="UPDATE workstation set monitor = '$ws' where workstation_id = '$comp' " ;
                if (!mysqli_query($conn,$sql))
                {
                die('Error ws' . mysql_error());
                }

    }

    }

    unset($_SESSION['todeploy']);
    echo "<script> alert('Success.')</script>";
    ?><script>document.location="monitorstock.php" </script>

    <?php
}
else{

    $_SESSION['todeploy']=$_GET['del'];

?>

<div id="addform">
            Add Details: <br><br>
            <form action="monitordeploy.php" method="post" enctype="multipart/form-data">
            <label>Workstation:</label><br> <input type="text" id="ws" name="ws" size="70%" style="margin-bottom:10px;" required>
            <br>
            <input type="Submit" name="add" value="Submit" style=" float: right; margin-top: 10%; width: 100;font-family: arial helvetica san-serif; font-size:15px;">

            </form>
    <button onclick="window.location='monitorstock.php'" style="float: right;margin-top:15%;margin-right:-100;width: 100;"> Back </button>
<?php } ?>

</body>
</html>

使用mysql和PHP。 如果我对您的理解正确,只需查询数据库即可。

$sql = "SELECT id FROM MYTABLE where id = 'val' ";  
$query = mysqli_query($conn,$sql);
if(!$row = mysqli_fetch_array($query))
{
    //Insert data as no record exists
}
else
{
    //add data to another table
}

我认为在获取结果和处理时存在问题。您可以通过

$行= mysql_num_rows($查询)

然后

If($ rows <1){//您的代码}

您可以构建一个函数,例如将其称为unique($ sql),该函数接受sql select语句,该语句将返回true或false,如果返回true,则意味着您有一条记录(如果没有,则为false),它将是类似的:

function unique($sql) {
  $result = mysqli_query($conn,$sql);
  $count = mysqli_num_rows($result);
  if ($count) {
    return true;
  }
  return false;
}

所以之后您可以说:

if (unique($sql)) {
  // insert some data in the database
}

您可以使用ajax立即获取数据,代码如下所示:

    <script>
 $("button").click(function(){
    var data = $('#your-input');
      $.ajax({
        url: // the url to the php page for example test.php,
        type: 'post',
        data: {data: data},
        success: function (response) { // this function will be triggered if ajax request has been sent correctly

         // and here what you can do is to check data received from response and perform the right response to the user
        }   
      });
   });
    </script>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM