2017-10-29 2 views
-4

지난 며칠 동안 방황하여 기본 페이지에서 상세 정보보기 버튼을 클릭하면 데이터베이스에서 특정 행의 데이터를 검색하는 방법을 완료 할 수 없습니다. [s_no는 (는) 고유합니다]데이터베이스에서 특정 행의 데이터를 검색하는 방법

여기 내 메인 페이지입니다. view.php

<?php 
    include('dbconnect.php'); 
    $select=mysql_query("SELECT * FROM forms order by s_no desc"); 
    $i=1; 
    while($userrow=mysql_fetch_array($select)) 

    { 
    $s_no=$userrow['s_no']; 
    $title_pro=$userrow['title_pro']; 
    $institute=$userrow['institute']; 
    $email=$userrow['email']; 
    $contact=$userrow['contact']; 
    $date=$userrow['date']; 
?> 
<p align="center"><a href="submit_pro.php">View Paginated</a></p> 
<div class="display"> 
    <p> S.No : <span><?php echo $s_no; ?></span> 
    <p> Title of the proposal: <span><?php echo $title_pro; ?></span> 
    <a href="delete.php?id=<?php echo $s_no; ?>" 
    onclick="return confirm('Are you sure you wish to delete this 
Record?');"> 
      <span class="delete" title="Delete"> X </span></a> 
    </p> 
    <br /> 
    <p> Institute: <span><?php echo $institute; ?></span> 
    <a href="table_print.php?id=<?php echo $s_no; ?>"><span class="edit" 
title="Edit"> VIEW DETAIL</span></a> 
    </p> 
    <br /> 
    <p> Email: <span><?php echo $email; ?></span> 
    </p> 
    <br /> 
    <p> Contact name and address: <span><?php echo $contact; ?></span> 
    </p> 
    <br /> 
    <p> SUBMITTED ON : <span><?php echo $date; ?></span> 
    </p> 
    <br /> 
</div> 
<?php } ?> 

이 메인 페이지 (view.php)는 잘 작동합니다. 지금. 한번 view.php (메인 페이지)의 view detail 버튼을 클릭하면 특정 행의 모든 ​​세부 사항을 볼 수 있습니다. 다음은 내가 할 수없는 불완전한 table_print (세부 정보 페이지) 코드입니다.

두 번째 페이지 : table_print.php

<?php 

    $mysql_hostname = "localhost"; 
    $mysql_user = "root"; 
    $mysql_password =""; 
    $mysql_database = "iaoform_db"; 

// Create connection 
$conn = new mysqli($mysql_hostname, $mysql_user, $mysql_password, $mysql_database); 
// Check connection 
if ($conn->connect_error) { 
    die("Connection failed: " . $conn->connect_error); 
} 
    $id = $_GET['id']; //read id from get inputs 
    $sqli = "SELECT s_no, title_pro, type, cycle, type_pro, thesis, year, 
      proposer, institute, 
      email,present,contact,abstract,status_ongoing,status_file, 
     sch_request,no_night,no_night_impossible,just_request,mode_ob, 
      brief_descrip,plan,hfosc_b,hfosc_n,hfosc_g,hfosc_s,hesp_r,hesp_o, 
     tirspec_b,tirspec_n,tirspec_s,tirspec_c,tirspec_slits,obj_name,obj_ra, 
     obj_dec,obj_epoch,obj_mag,obj_size,scient_just,date, 

     status FROM forms WHERE s_no = ?"; //add a where clause with place 
             holder 
    $stmt = $conn->prepare($sqli); 
    $stmt->bind_param("i", $id); //bind input parameters 
    $stmt->execute(); 
    $stmt->store_result(); 
    /* Get the number of rows */ 
    $num_of_rows = $stmt->num_rows; 

    /* Bind the result to $row variable */ 
    $stmt->bind_result($row); 

    if($num_of_rows < 1){ //exit or send the result if number of rows is less than one 
     echo "Can't find any record!"; 
     mysqli_close($conn);  
     exit(); 
    } { 
    ?> 
<!----------------------------------------------------> 
<?php 
    while ($stmt->fetch()) 
    { 



<table class="tg"> 
    <tr> 
    <th class="tg-9hbo">S.No</th> 
    <th class="tg-yw4l" colspan="5"><?php echo $row["s_no"]; ?> </th> 
    </tr> 
    <tr> 
    <td class="tg-9hbo">Title of the proposal:</td> 
    <td class="tg-yw4l" colspan="5"><?php echo $row["title_pro"]; ?> 
</td> 
    </tr> 
    <tr> 
    <td class="tg-9hbo">Type:</td> 
    <td class="tg-yw4l" colspan="5"><?php echo $row["type"]; ?> </td> 
    </tr> 
    | 
    | 
    | 
    | 
    | 
    <tr> 
    <td class="tg-9hbo">Submission date:</td> 
    <td class="tg-yw4l" colspan="5"><?php echo $row["date"]; ?> </td> 
    </tr> 
    </table> 
     /* free results */ 
     $stmt->free_result(); 

    } 
    mysqli_close($conn); 
    ?> 

그것이를 완료하는 데 매우 감사하게 될 것입니다. 미리 감사드립니다 ... !!!

답변

1

특정 행을 패치하기 위해 쿼리에 WHERE 절이 없습니다. 이드의 배열은 $_GET입니다. SQL 삽입을 막기 위해서는 Prepared Statement를 placeholder와 함께 사용해야합니다. 코드는 PHP 태그를 여는하지만 당신은 구문 오류를 얻고있는 이유는 그게 전부 폐쇄되지

<?php 

     $mysql_hostname = "localhost"; 
     $mysql_user = "root"; 
     $mysql_password =""; 
     $mysql_database = "iaoform_db"; 

    // Create connection 
    $conn = new mysqli($mysql_hostname, $mysql_user, $mysql_password, $mysql_database); 
    // Check connection 
    if ($conn->connect_error) { 
     die("Connection failed: " . $conn->connect_error); 
    } 
    $id = $_GET['id']; //read id from get inputs 
    $sqli = "SELECT s_no, title_pro, type, cycle, type_pro, thesis, year, 
    proposer, institute, 
    email,present,contact,abstract,status_ongoing,status_file, 

    sch_request,no_night,no_night_impossible,just_request,mode_ob, 
    brief_descrip,plan,hfosc_b,hfosc_n,hfosc_g,hfosc_s,hesp_r,hesp_o, 
    tirspec_b,tirspec_n,tirspec_s,tirspec_c,tirspec_slits,obj_name,obj_ra, 
    obj_dec,obj_epoch,obj_mag,obj_size,scient_just,date, 

     status FROM forms WHERE s_no = ?"; //add a where clause with place holder 
     $stmt = $conn->prepare($sqli); 
     $stmt->bind_param("i", $id); //bind input parameters 
     $stmt->execute(); 
     $stmt->store_result(); 
     /* Get the number of rows */ 
     $num_of_rows = $stmt->num_rows; 

     /* Bind the result to $row variable */ 
     $stmt->bind_result($row); 

     if($num_of_rows < 1){ //exit or send the result if number of rows is less than one 
      echo "Can't find any record!"; 
      mysqli_close($conn);  
      exit(); 
     } { ?> 
    <!----------------------------------------------------> 
    <?php 
     while ($stmt->fetch()) 
     { ?> 

    <table class="tg" id="myModal"> 
     <tr> 
     <th class="tg-9hbo">S.No</th> 
     <th class="tg-yw4l" colspan="5"><?php echo $row["s_no"]; ?> </th> 
     </tr> 
     <tr> 
     <td class="tg-9hbo">Title of the proposal:</td> 
     <td class="tg-yw4l" colspan="5"><?php echo $row["title_pro"]; ?> 
    </td> 
     </tr> 
     <tr> 
     <td class="tg-9hbo">Type:</td> 
     <td class="tg-yw4l" colspan="5"><?php echo $row["type"]; ?> </td> 
     </tr> 
     <tr> 
     <td class="tg-9hbo">Number of cycles/nights:</td> 
     <td class="tg-yw4l" colspan="5"><?php echo $row["cycle"]; ?> </td> 
     </tr> 
     <tr> 
     <td class="tg-9hbo">Type of the proposal:</td> 
     <td class="tg-yw4l" colspan="5"><?php echo $row["type_pro"]; ?> </td> 
     </tr> 
     <tr> 
     <td class="tg-9hbo">Title of the thesis:</td> 
     <td class="tg-yw4l" colspan="5"><?php echo $row["thesis"]; ?> </td> 
     </tr> 
     <tr> 
     <td class="tg-9hbo">Expected year of thesis submission:</td> 
     <td class="tg-yw4l" colspan="5"><?php echo $row["year"]; ?> </td> 
     </tr> 
     <tr> 
     <td class="tg-amwm" colspan="6">List of Proposer:</td> 
     </tr> 
     <tr> 
     <td class="tg-9hbo" colspan="2">Proposer</td> 
     <td class="tg-9hbo">Institute</td> 
     <td class="tg-9hbo">Email</td> 
     <td class="tg-9hbo" colspan="2">Present</td> 
     </tr> 
     <tr> 
     <td class="tg-9hbo" colspan="2"><?php echo $row["proposer"]; ?> </td> 
     <td class="tg-yw4l"><?php echo $row["institute"]; ?> </td> 
     <td class="tg-yw4l"><?php echo $row["email"]; ?> </td> 
     <td class="tg-yw4l" colspan="2"><?php echo $row["present"]; ?> </td> 
     </tr> 
     <tr> 
     <td class="tg-9hbo">Contact name and address:</td> 
     <td class="tg-yw4l" colspan="5"><?php echo $row["contact"]; ?> </td> 
     </tr> 
     <tr> 
     <td class="tg-9hbo">Abstract:</td> 
     <td class="tg-yw4l" colspan="5"><?php echo $row["abstract"]; ?> </td> 
     </tr> 
     <tr> 
     <td class="tg-9hbo" colspan="2">Status of ongoing/Previous 
    proposal</td> 
     <td class="tg-9hbo" colspan="4">Attached file</td> 
     </tr> 
     <tr> 
     <td class="tg-9hbo" colspan="2"><?php echo $row["status_ongoing"]; ?> 
      </td> 
     <td class="tg-yw4l" colspan="4"><?php echo $row["status_file"]; ?> 
     </td> 
     </tr> 
     <tr> 
      <td class="tg-9hbo">Scheduling request:</td> 
     <td class="tg-yw4l" colspan="5"><?php echo $row["sch_request"]; ?> 
     </td> 
     </tr> 
     <tr> 
     <td class="tg-amwm" colspan="6">No. of nights requested</td> 
     </tr> 
     <tr> 
     <td class="tg-amwm" colspan="2">Preferred dates</td> 
     <td class="tg-amwm" colspan="4">Impossible date</td> 
     </tr> 
     <tr> 
     <td class="tg-9hbo" colspan="2"><?php echo $row["no_night"]; ?> </td> 
     <td class="tg-yw4l" colspan="4"><?php echo $row["no_night_impossible"]; 
    ?> </td> 
     </tr> 
     <tr> 
     <td class="tg-9hbo">Justification for scheduling request:</td> 
     <td class="tg-yw4l" colspan="5"><?php echo $row["just_request"]; ?> 
     </td> 
     </tr> 
     <tr> 
     <td class="tg-9hbo">Instrument:</td> 
     <td class="tg-yw4l" colspan="5"><?php echo $row["instru1"]; ?> </td> 
     </tr> 
     <tr> 
     <td class="tg-9hbo">Mode of Observation:</td> 
     <td class="tg-yw4l" colspan="5"><?php echo $row["mode_ob"]; ?> </td> 
     </tr> 
     <tr> 
     <td class="tg-9hbo">Brief description of observations:</td> 
     <td class="tg-yw4l" colspan="5"><?php echo $row["brief_descrip"]; ?> 
    </td> 
     </tr> 
     <tr> 
     <td class="tg-9hbo">Plans for data reduction and analysis:</td> 
     <td class="tg-yw4l" colspan="5"><?php echo $row["plan"]; ?> </td> 
     </tr> 
     <tr> 
     <td class="tg-amwm" colspan="6">HFOSC</td> 
     </tr> 
     <tr> 
     <td class="tg-9hbo" colspan="2">Broad Band Filters</td> 
     <td class="tg-9hbo">Narrow Band Filters</td> 
     <td class="tg-9hbo">Grisms</td> 
     <td class="tg-9hbo" colspan="2">Slits</td> 
     </tr> 
     <tr> 
     <td class="tg-9hbo" colspan="2"><?php echo $row["hfosc_b"]; ?> </td> 
     <td class="tg-yw4l"><?php echo $row["hfosc_n"]; ?> </td> 
     <td class="tg-yw4l"><?php echo $row["hfosc_g"]; ?> </td> 
     <td class="tg-yw4l" colspan="2"><?php echo $row["hfosc_s"]; ?> </td> 
     </tr> 
     <tr> 
     <td class="tg-amwm" colspan="6">HESP</td> 
     </tr> 
     <tr> 
     <td class="tg-amwm" colspan="2">Resolution</td> 
     <td class="tg-amwm" colspan="4">Observation mode</td> 
     </tr> 
     <tr> 
     <td class="tg-amwm" colspan="2"><?php echo $row["hesp_r"]; ?> </td> 
     <td class="tg-yw4l" colspan="4"><?php echo $row["hesp_o"]; ?> </td> 
     </tr> 
     <tr> 
     <td class="tg-amwm" colspan="6">TIRSPEC</td> 
     </tr> 
     <tr> 
     <td class="tg-9hbo">Broad Band Filters</td> 
     <td class="tg-9hbo">Narrow Band Filters</td> 
     <td class="tg-9hbo">Single Order Dispersers</td> 
     <td class="tg-9hbo">Cross Dispersers</td> 
     <td class="tg-9hbo" colspan="2">Slits</td> 
     </tr> 
     <tr> 
     <td class="tg-9hbo"><?php echo $row["tirspec_b"]; ?> </td> 
     <td class="tg-yw4l"><?php echo $row["tirspec_n"]; ?> </td> 
     <td class="tg-yw4l"><?php echo $row["tirspec_s"]; ?> </td> 
     <td class="tg-yw4l"><?php echo $row["tirspec_c"]; ?> </td> 
     <td class="tg-yw4l" colspan="2"><?php echo $row["tirspec_slits"]; ?> 
    </td> 
     </tr> 
     <tr> 
     <td class="tg-amwm" colspan="6">List of objects: (essential)</td> 
     </tr> 
     <tr> 
     <td class="tg-9hbo">Name</td> 
     <td class="tg-9hbo">RA (hh mm ss)</td> 
     <td class="tg-9hbo">Dec (dd mm ss)</td> 
     <td class="tg-9hbo">Epoch</td> 
     <td class="tg-9hbo">V mag</td> 
     <td class="tg-9hbo">size</td> 
     </tr> 
     <tr> 
     <td class="tg-yw4l"><?php echo $row["obj_name"]; ?> </td> 
     <td class="tg-yw4l"><?php echo $row["obj_ra"]; ?> </td> 
     <td class="tg-yw4l"><?php echo $row["obj_dec"]; ?> </td> 
     <td class="tg-yw4l"><?php echo $row["obj_epoch"]; ?> </td> 
     <td class="tg-yw4l"><?php echo $row["obj_mag"]; ?> </td> 
     <td class="tg-yw4l"><?php echo $row["obj_size"]; ?> </td> 
     </tr> 
     <tr> 
      <td class="tg-9hbo">Scientific Justification:</td> 
     <td class="tg-yw4l" colspan="5"><?php echo $row["scient_just"]; ?> 
    </td> 
     </tr> 
     <tr> 
     <td class="tg-9hbo">Submission date:</td> 
     <td class="tg-yw4l" colspan="5"><?php echo $row["date"]; ?> </td> 
     </tr> 


    </table> 
<?php 
      /* free results */ 
      $stmt->free_result(); 

     } 
     mysqli_close($conn); 
    ?> 
+0

@N Moeini는 노력에 감사드립니다. 예상치 못한 '<'오류가 발생했습니다. 다시 확인하십시오. – explorer104

+0

수정 됨. 다시 테스트하십시오. –

+0

@ explorer104, 다시 시도해 보셨습니까? –

0

이미 s_no을 URL 매개 변수 id으로 두 번째 페이지로 전달하고있는 것으로 보입니다. 따라서 두 번째 페이지 (예 : id = $_GET['id'])를 가져와야하므로 SQL 쿼리에서이를 사용하여 WHERE s_no = id을 추가 할 수 있습니다.

참고 : SQL에 넣기 전에 문자열을 인용해야합니다. 그렇지 않으면 인터넷에있는 누군가가 데이터베이스를 삭제할 수있는 SQL 주입 취약점이 있습니다. prepared statements을 사용하십시오.

또한 s_no이 고유하면 두 번째 페이지에 while이 필요하지 않습니다. 하나의 행 (있는 경우)을 가져올 수 있습니다.

+0

답장을 보내 주셔서 감사합니다 ... PHP를 사용하는 새로운 방법 .. 어떻게하는지, 제발 해줄 수 있겠습니까? – explorer104

+0

@ explorer104 Stack Overflow는 코드 작성 서비스가 아닙니다. 우리는 당신이 특정한 문제를 해결하도록 도울 수 있지만 우리는 당신을 위해 일할 수 없습니다. – jurez

0

다음 시도

반드시 해당 테이블 이름과 열 이름이 맞다 확인 쿼리 문에 일부 문제가있을 수 있어야합니다

 $sqli = "SELECT s_no, title_pro, type, cycle, type_pro, thesis, year, 
     proposer, institute, 
     email,present,contact,abstract,status_ongoing,status_file, 
     sch_request,no_night,no_night_impossible,just_request,mode_ob, 
     brief_descrip,plan,hfosc_b,hfosc_n,hfosc_g,hfosc_s,hesp_r,hesp_o, 
    tirspec_b,tirspec_n,tirspec_s,tirspec_c,tirspec_slits,obj_name,obj_ra, 
    obj_dec,obj_epoch,obj_mag,obj_size,scient_just,date, 

    status FROM forms WHERE s_no = ?"; 
+0

@ pranjal k 이제 정확한 에러 라인은 .... "비 객체의 멤버 함수 bind_param() 호출 C : \ wamp \ www 35 행 "이고 35 행은 $ stmt-> bind_param ("i ", $ id)입니다. // 입력 매개 변수를 바인딩하십시오. – explorer104

관련 문제