如何使用php

时间:2017-07-08 02:07:45

标签: php loops

我按循环创建了所有文本字段,当我单击文本字段的按钮时,我可以将其发布到另一个页面。但现在我无法检索文本字段的值。如何检索循环文本字段的值?

这是我的代码。

<?php 
                $MYSQLQuery1=mysql_query("select id,ccode,pdesc,cost from product2")or die(mysql_error());
                $i=1;
                while($row=mysql_fetch_array($MYSQLQuery1)){
                ?>
                    <tr>
                    <td><?php echo"<img src='img/".$row['ccode'].".jpg' width='150' height='200'/>","<br/>"; 
                              echo $row['pdesc'],"<br/>"; 
                              echo $row['cost']; ?>
                    </td>
                    <td><?php echo "<input type='text' name='product".$i."'>";  ?></td>
                    <td>
                        <form action="addvehicle.php" role="form" method="POST" class="form-inline">
                            <input type="hidden" name="ID" value="<?php echo $row['id']; ?>">
                            <input type="submit" name="SUBMITEDITVEHICLE"  value="Edit" class="btn btn-success">
                        </form>
                    </td>
                    </tr>
                <?php 
                    $i++;
                }
                ?>

这是我发布值的地方。

if(isset($_POST['SUBMITEDITVEHICLE']))
{
    if($_POST['SUBMITEDITVEHICLE']=='Edit')
    {   
            $i=0;
            $E_1 = clean($_POST['ID']);
            $gt = clean($_POST['product. $i']); 
            echo $gt,"<br/>";
            $query_editvehicle=mysql_query("SELECT ccode,pdesc,cost FROM product2 WHERE ID='$E_1' ")or die(mysql_error());
            while($roweditvehicle=mysql_fetch_array($query_editvehicle))
            {
                $txtid = $roweditvehicle['id'];
                $txtvehicleregno = $roweditvehicle['ccode'];
                $txtmakemodel = $roweditvehicle['pdesc'];
                $txtcost = $roweditvehicle['cost']; 
            }
        echo txtvehivleregno;
        echo txtmakemodel;
        echo txtcost;
        $i++;
    }
}

2 个答案:

答案 0 :(得分:1)

  

<td><?php echo "<input type='text' name='product".$i."'>"; // ?></td>

右侧是一个文本框,因此您必须按

指定值
<td><?php echo "<input type='text' name='product".$i."' value=$row['cost']>"; // ?></td>

答案 1 :(得分:1)

据我所知,你可以像这样回应它:

<input type='text' name='product[]'> //don't forget the []

然后如果你发布它就可以有一个数组值,

$products = $_POST['product'];
print_r($products);//to print array

试试这段代码:

<?php 
            $MYSQLQuery1=mysql_query("select id,ccode,pdesc,cost from product2")or die(mysql_error());
            $i=1;
            echo '<form action="addvehicle.php" role="form" method="POST" class="form-inline">';
            while($row=mysql_fetch_array($MYSQLQuery1))
              {
                echo "
                     <tr>
                        <td>
                           <img src='img/".$row['ccode'].".jpg' width='150' height='200'/>
                           <br/>
                          ".$row['pdesc']."<br/>"
                           .$row['cost']. 
                      "</td>
                        <td>
                          <input type='text' name='product[]'>
                       </td>
                      <td>
                        <input type='hidden' name='ID' value='".$row['id']."'>
                        <input type='submit' name='SUBMITEDITVEHICLE'  value='Edit' class='btn btn-success'>

                     </td>
                    </tr>"; //end of one echo and end of tr
               }//end of while

 echo "</form>"; //end of form
            ?>