将数组值插入HTML表的特定字段

时间:2018-01-15 00:18:19

标签: javascript php jquery arrays postgresql

我需要将pg_query选择的值插入到数组中,并将其组织到表的特定字段中,输入类型为= text:



<tr>
  <td>
    <label for="street">Street:</label>
  </td>
  <td>
    <input type="text" placeholder="Street name" required="required" id="street" name="street" onchange="sendForm(this.form)">
  </td>
</tr>

<tr>
  <td>
    <label for="building_no">Building No:</label>
  </td>
  <td>
    <input type="text" placeholder="Number of building (XXXX)" name="bld_no">
  </td>
</tr>

<tr>
  <td>
    <label for="ID Sector">ID Sector:</label>
  </td>
  <td>
    <input type="text" placeholder="IotaNet Sector (5)" name="id_sector">
  </td>
</tr>
&#13;
&#13;
&#13;

所有值都是字符串,数组具有所有选定的值:

&#13;
&#13;
$qexist = "SELECT 
       test.tb.street, 
       test.tb.bld_no, 
       test.tb.id_sector, 
  FROM test.tb
  WHERE test.tb.street = '$street'
  AND test.tb.bld_no = '$bld_no'
  "; 
  $ress = pg_query($qexist);
  while ($row = pg_fetch_array($ress)) { 

//   values of $row[] have to be inserted into exact fields
    echo $row[1].'street';
    echo $row[2].'bld_no';
    echo $row[3].'id_sector';
  ? ? ? ? ? ?
    var_dump($row);
      /* result of var_dump
array(30) { [0]=> string(20) "90315464612890004 " ["id_bld"]=> string(20) "90315464612890004 " 
        [1]=> string(50) "Street" ["street"]=> string(50) "Street" 
        [2]=> string(4) "0004" ["bld_no"]=> string(4) "0004" 
        [3]=> string(1) "5" ["id_sector"]=> string(1) "5"................ */
  }
&#13;
&#13;
&#13;

请告知如何解决此问题。我尝试了不同的方法,但没有成功,尽管网站显示echo和var_damp结果,但字段仍为空。谢谢。

2 个答案:

答案 0 :(得分:0)

<input type="text" placeholder="Street name" required="required" id="street" name="street" value="<?php echo $row[1] ?>">

<input type="text" placeholder="Number of building (XXXX)" name="bld_no" value="<?php echo $row[2] ?>">

<input type="text" placeholder="IotaNet Sector (5)" name="id_sector" value="<?php echo $row[3]?>">

答案 1 :(得分:0)

我通过实验方式找到了解决方案,但它确实有效。我创建了新的变量数组$value[],为它们赋值$row[]并且代码开始工作

<?php>
$qexist = "SELECT 
       test.tb.id_bld, 
       test.tb.street, 
       test.tb.bld_no, 
  FROM test.tb
  WHERE test.tb.street = '$street'
  AND test.tb.bld_no = '$bld_no'
  "; 
  $ress = pg_query($qexist);
  while ($row = pg_fetch_array($ress)) { 
        $value[0] = $row[0];
        $value[1] = $row[1];
        $value[2] = $row[2];
  }
 ?>
 

<table>
  <tbody>
    <form method="get" action="t54646.php">
      <tr>
        <td>
          <label for="id_bld">Building ID:</label>
        </td>
        <td>
          <input type="text" placeholder="Not for fill - system ordered" name="id_bld" value="<?php echo $value[0] ?>">
        </td>
      </tr>

      <tr>
        <td>
          <label for="street">Street:</label>
        </td>
        <td>
          <input type="text" placeholder="Street name" required="required" id="street" name="street" onchange="sendForm(this.form)" value="<?php echo $value[1] ?>">
        </td>
      </tr>

      <tr>
        <td>
          <label for="building_no">Building No:</label>
        </td>
        <td>
          <input type="text" placeholder="Number of building (XXXX)" name="bld_no" value="<?php echo $value[2] ?>">
        </td>
      </tr>

如果有人可以解释为什么数组在变量重命名后开始工作,这对很多人来说很有用