简体   繁体   English

使用 PHP 从 MySql 获取具有给定 ID 的特定行

[英]Get specific row with given ID from MySql using PHP

Edit: After receiving some help, I edited some parts and here formlarigor2.php file and I got new error says 1)mysqli_stmt_bind_result() expects at least 2 parameters, 1 given.... 2)mysqli_fetch_array() expects parameter 1 to be mysqli_result, object given in C.. I want to layout the page like given.编辑:在收到一些帮助后,我编辑了一些部分,这里是 formlarigor2.php 文件,我收到新错误说 1)mysqli_stmt_bind_result() 至少需要 2 个参数,给定 1.... 2)mysqli_fetch_array() 需要参数 1 mysqli_result,C 中给出的 object .. 我想像给定的那样布局页面。 布局 That's why I used td> codes.. Any help?这就是我使用 td> 代码的原因。有什么帮助吗? :` :`

<?php

    $conn = mysqli_connect("localhost","root","","son_fbe");
            if (mysqli_connect_error()) {

            echo "Failed to connect to MySQL: " . mysqli_connect_error();
                 exit();

        }


    $formid = isset($_GET['formid ']) ? $_GET['formid '] : ''; 
    if ($stmt = mysqli_prepare($conn, "SELECT * FROM derssaydirma WHERE formid = ?")) {

            mysqli_stmt_bind_param($stmt, "s", $formid );
            mysqli_stmt_execute($stmt);
            mysqli_stmt_bind_result($stmt);

    }



?>


 <!DOCTYPE html> 
     <html lang="en"> 
         <head> <title></title> </head>
        <body>  <table   align="center" bordercolor="#CCCCCC" border="1" >
      <?php

   $i=0;
   while($row = mysqli_fetch_array($stmt)) {
   if($i%2==0)
  $classname="even";
   else
  $classname="odd";
  ?>

 <tr class="<?php if(isset($classname)) echo $classname;?>">
  <td width="235">Form ID</td>
 <td width="299"><?php echo $row["formid"]; ?></td>
  </tr>

<tr class="<?php if(isset($classname)) echo $classname;?>">
<td>O_AdiSoyadi</td>
<td><?php echo $row["O_AdiSoyadi"]; ?></td>
</tr>

 <tr class="<?php if(isset($classname)) echo $classname;?>">
 <td>OgrenciNo</td>
<td><?php echo $row["OgrenciNo"]; ?></td>
</tr>
<tr class="<?php if(isset($classname)) echo $classname;?>">
 <td>Program_BaslanilanDonem</td>
<td><?php echo $row["Program_BaslanilanDonem"]; ?></td>
 </tr>
  //There are about 20 more like this code block( <tr></tr> <?php
 $i++;
 }
 ?>
</table>


`  

I have a table that consists of more than 25 columns.我有一个包含超过 25 列的表。 There is a page that shows forms waiting for approval.有一个页面显示 forms 等待批准。 In that form, I do not show all columns instead I show only a few.在那种形式中,我不会显示所有列,而是只显示一些列。 I do that using PHP code.我使用 PHP 代码来做到这一点。 Here my code.这是我的代码。

There is a part that says see all submitted forms and go to that forms.有一部分说看到所有提交的 forms 和 go 到 forms。 I am able to see all submitted ones however I could not see the specific row.我可以看到所有提交的,但是我看不到具体的行。 For example, the table displays formid, name and etc, I want to get information of formid's 2. How can I do that?比如表格显示formid、name等,我想获取formid的2的信息,我该怎么做呢? ` `

桌子

Use the form id as parameter使用表单 id 作为参数

<td width="174"  class="centertext"><a href="formlarigor2.php?formid=<?php echo $row["formid"]; ?>"> Go to the form</a></td> 

and in the formlarigor2.php you can get it as, where you put it at the start of php.在 formlarigor2.php 中,您可以将其放在 php 的开头。

$formid = isset($_GET['formid ']) ? $_GET['formid '] : ''; 
if ($stmt = mysqli_prepare($conn, "SELECT * FROM derssaydirma WHERE formid = ?")) {

    /* bind parameters for markers */
    mysqli_stmt_bind_param($stmt, "s", $formid );

    /* execute query */
    mysqli_stmt_execute($stmt);

    mysqli_stmt_bind_result($stmt, $col1, $col2,$col3,$col4,$col5,$col6);
    while (mysqli_stmt_fetch($stmt)) {
      printf("%s %s\n", $col1, $col2);
    }
}

the fetch row part is usually in a while loop ti get multiple rows.获取行部分通常在一个while循环中获取多行。 but you have only one但你只有一个

I am guessing that "Go to the Form" will be showing more information about the form in a different page layout.我猜“转到表单”将在不同的页面布局中显示有关表单的更多信息。

So the element in the html should link to another php page that shows that form in detail.所以 html 中的元素应该链接到另一个 php 页面,该页面详细显示了该表单。 The link should take the id of the selected form.该链接应采用所选表单的id

<td width="174" class="centertext"><a href="formlarigor2.php/?formId=<?php echo $row["formid"]; ?>"> Go to the form</a></td>

And that linked php page will have a query that says something like:链接的 php 页面将有如下查询:

$formId = $_GET['formId'];
$stmt = $mysqli->prepare("SELECT * FROM derssaydirma where id = ?");
$stmt->bind_param("i", $formId);
$stmt->execute();
$result = $stmt->get_result();

The using $result you can do the detail layout.使用 $result 可以进行详细布局。

Note the use of prepared statements to avoid SQL injection.注意使用准备好的语句来避免 SQL 注入。

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

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