Php 如何从数据库中检索特定行的数据

Php 如何从数据库中检索特定行的数据,php,mysql,html,mysqli,Php,Mysql,Html,Mysqli,过去几天我一直在徘徊,一旦我在主页上单击查看详细信息按钮,就无法完成如何从数据库中检索特定行的数据[SU编号是唯一的] 这是我的主页 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=

过去几天我一直在徘徊,一旦我在主页上单击查看详细信息按钮,就无法完成如何从数据库中检索特定行的数据[SU编号是唯一的]

这是我的主页 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 } ?>
  <?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);
   ?>

美国编号: 提案的标题:


学会:


电邮:


联系人姓名和地址:


提交日期:


这个主页(view.php)运行良好。现在。一旦我点击view.php(主页)上的view detail按钮,它就会查看特定行的所有细节。 下面是表_打印(详细页)代码,它是不完整的,我不能这样做

第二页:table_print.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 } ?>
  <?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);
   ?>

您似乎已经将
s_no
作为URL参数
id
传递到第二页。因此,第二个页面需要选择它(即
id=$\u GET['id']
),然后您可以在SQL查询中使用它添加
,其中s\u no=id

注意:在将任何字符串放入SQL之前,您需要对其进行引用,否则您将存在SQL注入漏洞(即internet上的任何人都可以删除您的数据库)。使用


此外,如果
s\u no
是唯一的,则在第二页
时不需要
,您可以获取一行(如果存在)。

查询中没有WHERE子句来获取特定行。id位于
$\u GET
数组中。为了防止sql注入,需要使用带有占位符的预处理语句。尝试以下代码

    <?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);
     ?>

您正在打开php标记,但没有关闭它,这就是为什么会出现语法错误

查询语句一定有问题,请确保表名和列名正确

     $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 = ?";

@N Moeini非常感谢你的努力。我得到语法错误,意外的“修复”。再次测试它。@N Moeini显示错误。。在非对象上调用成员函数bind_param()。非常感谢您的响应。。。我是php新手。。怎么做,你能帮我做吗。谢谢lot@explorer104堆栈溢出不是代码编写服务。我们可以帮助您解决特定问题,但我们无法为您完成工作。@pranjal k现在准确的错误线是。。。。“对C:\wamp\www第35行中的非对象调用成员函数bind_param()”,第35行是$stmt->bind_param(“i”,$id)//绑定输入参数。。。这是什么意思?请把它修好