PHP:无法从MySQL和未定义的变量中获取结果

时间:2019-02-13 16:35:57

标签: php html mysql

我正在一个项目中,正在从数据库中获取一些信息,并希望将其显示在表单中。当我在SQL日志中检查它时,我的SQL查询看起来很好,但是我没有从mysql得到任何结果。我想从数据库中检索值并将其设置在窗体内。我在用SQL做错什么?如何显示从数据库读取的值并将其显示在表单中?谢谢。

代码:

<?php
      $client_id = $_GET['clientId'];
      $name ="rahul";
      $contact;
$link = mysqli_connect("localhost", "root", "akshay2787", "tim");
$sql = "select name,contact,tel1,tel2,email1,email2,investment_time,interest,transport,deposit,tax1,tax2,tax3,
 address1,address2,address3,notes from client_setup where client_id='".$client_id."'";
 if($result = mysqli_query($link, $sql)){
    if(mysqli_num_rows($result) > 0){       
    while($row = mysqli_fetch_array($result)){
      $name = $row['name'];
      echo "<script type=\"text/javascript\">alert('Thank you form is submitted. $name');</script>";
      $contact = $row['Contact'];      
        $tel1 = $row['Telephone1'];      
    }
}

   ?>
///And below I display the values.

<form action="" method="get" align="center" >
 <table  align="center" class="table-block">
<tr class="highlight">
        <td width="100"><label for="netmask">Name</label></td>
        <td width="600"><input class="highlight" type="text" name="Name" id="name"><?php print $name;?></input></td>
     </tr>
</table>

MYSQL日志:

2019-02-13T16:27:56.450712Z         4 Query     select name,contact,tel1,tel2,email1,email2,investment_time,interest,transport,deposit,tax1,tax2,tax3,
 address1,address2,address3,notes from client_setup where client_id='6'

client_setup表:

+-----------------+------------------+------+-----+---------+----------------+
| Field           | Type             | Null | Key | Default | Extra          |
+-----------------+------------------+------+-----+---------+----------------+
| client_setip_id | int(30) unsigned | NO   | PRI | NULL    | auto_increment |
| client_id       | int(30) unsigned | NO   |     | NULL    |                |
| name            | varchar(50)      | YES  |     | NULL    |                |
| username        | varchar(50)      | YES  |     | NULL    |                |
| password        | varchar(50)      | YES  |     | NULL    |                |
| contact         | varchar(50)      | YES  |     | NULL    |                |
| tel1            | varchar(50)      | YES  |     | NULL    |                |
| tel2            | varchar(50)      | YES  |     | NULL    |                |
| email1          | varchar(50)      | YES  |     | NULL    |                |
| email2          | varchar(50)      | YES  |     | NULL    |                |
| investment_time | double           | YES  |     | NULL    |                |
| interest        | double           | YES  |     | NULL    |                |
| transport       | double           | YES  |     | NULL    |                |
| deposit         | double           | YES  |     | NULL    |                |
| tax1            | double           | YES  |     | NULL    |                |
| tax2            | double           | YES  |     | NULL    |                |
| tax3            | double           | YES  |     | NULL    |                |
| address1        | varchar(500)     | YES  |     | NULL    |                |
| address2        | varchar(500)     | YES  |     | NULL    |                |
| address3        | varchar(500)     | YES  |     | NULL    |                |
| notes           | varchar(500)     | YES  |     | NULL    |                |
+-----------------+------------------+------+-----+---------+----------------+
21 rows in set (0.01 sec)

2 个答案:

答案 0 :(得分:0)

我认为这是您的数据不显示的地方

 <td width="600"><input class="highlight" type="text" name="Name" id="name"><?php print $name;?></input></td>

假设您的查询正确,它可能不会显示在您的输入字段中。

改为显示它

 <td width="600"><input class="highlight" type="text" name="Name" id="name" value="<?php echo $name;?>"></td>

 <td width="600"><input class="highlight" type="text" name="Name" id="name" value="<?=$name?>"></td>

假设您的查询不起作用,我将使用它来显示查询错误

if($result = mysqli_query($link, $sql)){
  //Block of code in OP question
}
else{
    echo "Error: ". mysqli_error().". The Query was <br /><pre>$sql</pre>";
}

答案 1 :(得分:0)

对不起,我的愚蠢错误。在测试期间,我更改了数据库中的clientid。设置正确的clientid之后,我得到了数据。谢谢。我投票结束这个问题。谢谢大家。