如何将数据从html网格推送到mysql?

时间:2017-06-14 16:38:24

标签: php mysql

我正在尝试将数据从这个简单的html网格推送到sql但我不能这样做。在一开始,我得到的错误是没有var展示的索引。我修好了。当我只使用广告客户价值时,我可以将数据推送到mysql,而第二个值,我不能成功。你能解释一下我做错了什么吗? 提前致谢

<?php

    include_once 'con_ui.php';
    if(isset($_POST['btn-save']))
        {

     $advertiser = $_POST["advertiser"];
     $impressions = (isset($POST["impressions"])?
     $_POST["impressions"]:'');



            $sql_query = "INSERT INTO data(adv, imp) VALUES('$advertiser', '$impressions')";
     mysql_query($sql_query);

            // sql query for inserting data into database

    }
    ?>


     <html>
     <head>
     </head>
     <body>
     <form method="post">
     <table id="myTable" align='center' cellspacing=0 cellpadding=5 border=1> 

     <tr>
     <th>advertiser</th>
     <th>impressions</th>
     </tr>
     <td>

     <select name="advertiser" id="advertiser">
                <option value="">Select advertiser</option>
                <option value = "Brita ">Brita</option>
                <option value = "Sammontana">Sammontana</option>
        </select>

     </td>

    <td name= "impressions" id="impressions" >1000000</td>

     <td>
        <button type="submit" name="btn-save"><strong>SAVE</strong></button>
     </td>
    </form>
    </body>
    </html>

2 个答案:

答案 0 :(得分:0)

这里有一些问题:

首先是中的$ POST

   $impressions = (isset($POST["impressions"])?$_POST["impressions"]:'');

缺少下划线,应该是$ _POST

   $impressions = (isset($_POST["impressions"])?$_POST["impressions"]:'');

其次,浏览器无法识别

<td name= "impressions" id="impressions" >1000000</td>

作为表单字段。如果您想传递用户无法编辑的值,您可以选择一些选项。您可以使用隐藏字段,如:

<td><input type="hidden" name="impressions" id="impressions" value="1000000">1000000</td>

或者您可以使用文本输入并禁用用户输入,如

<td><input type="text" name="impressions" id="impressions" value="1000000" disabled></td>

答案 1 :(得分:0)

在这里,您可以获得用于自动添加原始代码的代码的详细信息,让用户输入数据并保持代码可编辑

嘿谢谢你的回复。问题在于我需要让用户在单元格中输入日期并且具有内容并且内容应该是可编辑的。另外我有一个按钮添加行,插入一个新原始但如果使用表单类型作为输入则不起作用。这里有完整的代码

    <?php
include_once 'con_ui.php';
if(isset($_POST['btn-save']))
{
 // variables for input data
 $advertiser = $_POST["advertiser"];
 $impressions = (isset($_POST["impressions"])?
    $_POST["impressions"]:'');

 // variables for input data

 // sql query for inserting data into database

        $sql_query = "INSERT INTO data(adv, imp) VALUES('$advertiser', '$impressions')";
 mysql_query($sql_query);

        // sql query for inserting data into database

}
?>


 <html>
 <head>
 <script>
 function myFunction() {
    var table = document.getElementById("myTable");
    var new_client = document.getElementById("advertiser_row1").innerHTML;
    var new_impressions = document.getElementById("impressions_row1").innerHTML;

    var row = table.insertRow(1);
    var cell1 = row.insertCell(0);
    var cell2 = row.insertCell(1);
}
    </script>
 </head>
 <body>

 <table id="myTable" align='center' cellspacing=0 cellpadding=5 border=1> 
 <form method="post">
 <tr>
 <th>advertiser</th>
 <th>impressions</th>
 </tr>
 <td>

 <select name="advertiser" id="advertiser_row1">
            <option value="">Select advertiser</option>
            <option value = "Brita ">Brita</option>
            <option value = "Sammontana">Sammontana</option>
    </select>

 </td>

<td><input type="text" name="impressions" id="impressions_row1" value="1000000"></td>


 <td>
<button onclick="myFunction()">Add Row</button>
</td>
<td>
 <button type="submit" name="btn-save">Save</button>
 </td>
</tr>
</form>
</table>

</body>
</html>