2017-05-05 4 views
1

컴퓨터가 데이터베이스에있을 수있는 경우를 기준으로 if… else 문을지나 봅니다. 나는 약간의 연구를했고, 나를 위해 좋은 것을 발견했다. 그러나 그것은 완전하지 않은 것처럼 보였다. 그 이후로, 그것은 내 if ...을 통해 지나간다. 그리고 내 else을 통해 같은 시간에 .... 내 문제는 내 Hello world이 아니고 function (popup)이 아니고 내 데이터베이스에 정보가 삽입되어 있습니다. 어쩌면 내 코드에서 실수를 한 것일 수도 있고 찾지 못할 수도 있습니다. 나는 insert into 내가 전에 update을 할 필요가 있기 때문에코드 통과 if 문 및 else 문

데이터베이스 ($nomComputer as $info)$computername == 컴퓨터를 가면 내가 원하는 무엇,의 insert into 어떻게 든 popup에서 만든 예 할 때까지 작동하지 않았다는 (은 조금 다르다 먼저 if else 문을 조정하고 싶습니다.) ....하지만 컴퓨터 이름이 데이터베이스에 없다면 데이터베이스에 삽입됩니다.

이 결과에 대해서는 else if($computername!=$info)을 변경합니다. 데이터베이스 이름과 비슷한 컴퓨터 이름을 입력하고 직접 삽입하면 아무 것도하지 않고 처음으로 if($computername==$info)을 전달합니다.

나는 내가 지나가는 모든 질문에서 나를 돕는 것을 찾지 못했다. (다른 모든 것들은 PHP/jQuery (파이썬이나 C++과 같은) 다른 언어로되어있다.).

if 문과 else 문에서 코드를 잘못 작성한 것이 있습니까?

이가 넣어 내 검증 내 foreach가 포함 된 경우이 내 루프가 데이터베이스에 이미

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를 컴퓨터 이름을 얻을 수 있습니다

//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; 
} 

문 (지금은 버튼 위에 있지만 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 내용 다음 database

편집 사람의 컴퓨터 이름의

<!-- 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> 

Exemple는 생각이있어? 나는 많은 것을 시도했다. 대신 ===을 사용하고, alert을 제외하고, 성공하지 못하면 $function을 주석 (if의 경우)에 넣는다. 내 코드는 여전히 if 문과 else 문으로 전달됩니다! 어쩌면 그것은 다른 것일 수도 있지만 손가락을 그 위에 놓을 수는 없습니다!

답변

1

내 문제는 내가 foreachifelse 문을 넣어, 그래서 나는 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{ 
      ... 
     } 
0

스크립트를 출력하려면 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{ 
... 
+0

후에는 새 레코드를 삽입한다? – inarilo

+0

나는 시도했지만, 결과를주지 않았다. – Nexis

+0

더 이상 스크립트는 보이지 않았다. – Nexis