【问题标题】:Code pass in if statement and else statement代码传入 if 语句和 else 语句
【发布时间】:2017-10-04 10:29:48
【问题描述】:

我尝试根据计算机是否在数据库中来传递if… else 语句。我做了一些研究,找到了一些对我有用的东西,但它似乎并不完美,因为它通过了我的if...。同时通过我的else...。我的问题是我收到警报Hello world,而不是我的function (popup),它会将信息插入我的数据库。可能是我的代码出错了,找不到了。

我想要什么,如果 $computername == 数据库 ($nomComputer as $info) 中的计算机,insert into 直到在 popup 中以某种方式成为肯定(这有点不同,因为我需要做insert into 之前的 update 我想先调整 if else 语句)....但是如果计算机名称不在数据库中,它将被插入到数据库中。

为此结果,我还将 else 更改为 else if($computername!=$info):当我将与该名称相似的计算机名称放入数据库并直接插入时,我通过了第一个 if($computername==$info) 而没有做任何事情。

在我通过的所有问题中,我找不到任何可以帮助我的问题(我看到的所有其他问题都是 PHP/jQuery 的不同语言(如 python 或 C++))。

if 语句和else 语句中的代码有什么问题吗?

这是我的验证,我的 foreach 包含 if 语句

//Get informations in the form and insert it into db
    $location=$_GET['location'];
    $dept=$_GET['dept'];
    $jack=$_GET['jack'];
    $computername=$_GET['computername'];
    $productkey=$_GET['productkey'];
    $model=$_GET['model'];
    $vendor=$_GET['vendor'];
    $serialnumber=$_GET['serialnumber'];
    $macaddress=$_GET['macaddress'];
    $status=$_GET['status'];
    $starphone=$_GET['starphone'];
    $intid=$_GET['intid'];
    $did=$_GET['did'];
    $software=$_GET['software'];
    $yes=$_GET['yes'];

    foreach($nomComputer as $info){
        if($computername==$info){
            "<script type='text/javascript'>alert(‘Hello World’);
                ;(function($) {
                    $(function() {
                        e.preventDefault();
                        $('#element_to_pop_up').bPopup({
                            appendTo: 'form'
                            , zIndex: 2
                            , modalClose: false
                        });
                     });
                })(jQuery);
            </script> ";            
    }else{
        //Insert Into departement
        $query='insert into department(
        department,
        location_id)values(
        "'.$dept.'",
        "'.$location.'")';
        if(valid_query($query)){
        $message .= "Enregistrement a ete mis a jour! Informations has been saved for department!<br>";
        }else{
                $message .= "Enregistrement n a pas ete mis a jour! Informations has not been saved for department<br>";
        }
        //Insert Into computer
        $query='insert into computer(
        computer_name,
        product_key,
        model,
        serial_number,
        status_id,
        starphone,
        inst_id,
        did,
        macaddress,
        software_id,
        hidden
        )values(
        "'.$computername.'",
        "'.$productkey.'",
        "'.$model.'",
        "'.$serialnumber.'",
        "'.$status.'",
        "'.$starphone.'",
        "'.$intid.'",
        "'.$did.'",
        "'.$macaddress.'",
        "'.$software.'",
        0
        )';
        if(valid_query($query)){
        $message .= "Enregistrement a ete mis a jour! Informations has been saved!<br>";
        }else{
                $message .= "Enregistrement n a pas ete mis a jour! Informations has not been saved<br>";
        }
        $query="select dept_id from department where department='".$dept."' and location_id='".$location."'";
        $deptid=select_query($query);
        $query="select computer_id from computer where macaddress='".$macaddress."'";
        $computerid=select_query($query);
        //Insert Into jack
        $query='insert into jack(
        jack_number,
        dept_id,
        location_id,
        computer_id
        )values(
        "'.$jack.'",
        "'.$deptid.'",
        "'.$location.'",
        "'.$computerid.'")';
        if(valid_query($query)){
        $message .= "Enregistrement a ete mis a jour! Informations has been saved for jack!<br>";
        }else{
                $message .= "Enregistrement n a pas ete mis a jour! Informations has not been saved for jack<br>";
        }
        //Insert Into computer_vendor
        $query='insert into computer_vendor(
        computer_id,
        vendor_id
        )values(
        "'.$computerid.'",
        "'.$vendor.'")';
        if(valid_query($query)){
        $message .= "Enregistrement a ete mis a jour! Informations has been saved for vendor!<br>";
        }else{
                $message .= "Enregistrement n a pas ete mis a jour! Informations has not been saved for vendor<br>";
        }
        $location="";
        $dept="";
        $jack="";
        $computername="";
        $productkey="";
        $model="";
        $vendor="";
        $serialnumber="";
        $macaddress="";
        $status="";
        $starphone="";
        $intid="";
        $did="";

        $messageValide="Enregistrement a ete mis a jour! Informations has been saved";//Display went validate
        }
    }
    break;
}

这是我在数据库中获取计算机名称的循环

header( 'content-type: text/html; charset=utf-8' );
require_once("cnx_db_inventory.php");
$con = mysqli_connect(DB_HOST, DB_USER, DB_PASS,DB_NAME);
    //Get number of rows
    $sql="SELECT computer_name FROM computer";
    $result=mysqli_query($con, $sql);
    $i=1;
    while($row=mysqli_fetch_assoc($result)){
        $nomComputer[$i] = $row['computer_name'];
        $i++;
    } 

// Loop through the results from the database
for ($i = 1; $i <=count($nomComputer); $i++){
        $nomComputer[$i];
}

我尝试插入的popup(现在,它在一个按钮上,但希望它在if statement 中(我尝试了一下))

;(function($) {
    $(function() {
        $('#my-button').bind('click', function(e) {
            e.preventDefault();
            $('#element_to_pop_up').bPopup({
                appendTo: 'form'
                , zIndex: 2
                , modalClose: false
            });
        });
    });
})(jQuery);

还有popup的内容:

<!-- Button that triggers the popup -->
<button id="my-button">POP IT UP</button>
<!-- Element to pop up -->
<div id="element_to_pop_up">
    <h3>ATTENTION</h3>
    <p>This computer is already active in the database.</p>
    <p>Did you want to deactive the computer in the database and insert the new one?</p>
    <div align="right"><a id="yes" name="yes">Yes</a> &nbsp;&nbsp;&nbsp; <a class="b-close">No</a> &nbsp;&nbsp;&nbsp; </div>
</div>

database 中的计算机名称示例

编辑 有人知道吗?我尝试了很多事情(使用=== 而不是==,推迟alert,将$function 放入cmets(if 中的那个))但没有成功。我的代码仍然通过if 语句和else 语句!也许是别的东西,但我不能指望它!

【问题讨论】:

    标签: php jquery mysqli


    【解决方案1】:

    我的问题是我将ifelse 语句放在foreach 中,所以我在foreach 前面和if ... else 后面都做了

        foreach($nomComputer as $info){
            if($computername==$info){
                $foundIt = $computername;
            }
        }
            if($computername==$foundIt){
                echo "<script type='text/javascript'>
                    ;(function($) {
                        $(function() {
                            $('#element_to_pop_up').bind('click', function(e) {
                                e.preventDefault();
                                $('#element_to_pop_up').bPopup({
                                    appendTo: 'form'
                                    , zIndex: 2
                                    , modalClose: false
                                });
                            });
                         });
                    })(jQuery);
                </script> ";
            }else{
                ...
            }
    

    【讨论】:

      【解决方案2】:

      您必须使用echo 来输出脚本。现在你只有字符串。

      if($computername==$info){
              echo "<script type='text/javascript'>alert(‘Hello World’);
                  ;(function($) {
                      $(function() {
                          e.preventDefault();
                          $('#element_to_pop_up').bPopup({
                              appendTo: 'form'
                              , zIndex: 2
                              , modalClose: false
                          });
                       });
                  })(jQuery);
              </script> ";            
      }else{
      ...
      

      【讨论】:

      • 是在插入一条新记录吗?
      • 我不明白。它不可能通过简单的else 语句绕过两者。到底发生了什么?你在浏览器中查看过html源代码吗?
      • 我解决了这两个语句的问题,我要回答我的问题。我的 bPopup 出现在 if($computer==$info) 中时只有一个问题
      • 我解决了弹出窗口的其他问题。谢谢 inarilo
      猜你喜欢
      • 2013-11-26
      • 2014-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-09
      • 2017-02-05
      • 1970-01-01
      • 2014-11-11
      相关资源
      最近更新 更多