PHP DATA获取问题

时间:2017-05-21 17:01:43

标签: php mysql

我是PHP和HTML的新手。我试图从输入表单数据中获取行值,但我无法获取数据。

以下是我的代码。

HTML:

<form id="main" action="test.php" method="post" enctype="multipart/form-data"   >


                  <div class="row">             
                                <div class="col-md-12">
                                    <label for="model" style="font-size: 15px"> Model </label><br>
                                        <input type="text" id="tags" name="model" placeholder="Type Your Model Number" >
                                </div>
                  </div>


                   <div class="row">
                        <div class="col-md-12">
                        <button type="submit" id="button" name="submit1" />SUBMIT</button>
                        </div>
                   </div>

                  </form>
</html>

PHP代码:

 <?php
if(isset($_POST['submit']))
{
// id to search
$model = $_POST['model'];

// connect to mysql
$connect = mysqli_connect("localhost", "root", "","test");

// mysql search query
$query = "SELECT `offer`, `amount` FROM `offer`";

$result = mysqli_query($connect, $query);

// if id exist 
// show data in inputs
if(mysqli_num_rows($result) > 0)
{
  while ($row = mysqli_fetch_array($result))
  {
    $offer = $row['offer'];
    $amount = $row['amount'];


}}
if($model){

    echo "
    <form method='post' action=''>


        <div class='col-md-5'>
                    $offer
                </div>

                <div class='col-md-5'>
                    INR $amount/-
                </div>                  
                </div></form>"; 
 else {

        $offer = "";
        $amount = "";
        $offer2 = "";
        $amount2 = "";
     }    
mysqli_free_result($result);
mysqli_close($connect);}
else{
$offer = "";
$amount = "";
$offer2 = "";
$amount2 = "";
}
?>

另外,请注意该型号是字母数字。优惠将是耳机,金额将是100.我请求帮助我。

2 个答案:

答案 0 :(得分:1)

首先,我们不知道您的错误是什么,其次是您没有包含所有代码。但是,对于PHP使用以下代码

<?php

$mysqli = new mysqli("localhost", "root", "", "test") or die($mysqli->error);


    $select = $mysqli->query("SELECT * from offer") or die($mysqli->error);
if($select->num_rows){
    while($row = $select->fetch_array(MYSQLI_ASSOC)){
        $amount = $row['amount'];
        $offer= $row['offer'];
        }
    }

?>

您使用了输入和按钮,但我没有在HTML中看到任何表单标记

答案 1 :(得分:0)

您必须在查询中传递$model并执行此操作才能显示结果:

echo "<form method='post' action=''>";

      while ($row = mysqli_fetch_array($result))
      {
         echo " <div class='col-md-5'>
             ".$row['offer']."
         </div>

         <div class='col-md-5'>
             INR ".$row['amount']."-
         </div>   ";               
      }
echo "</form>";