2017-06-14 37 views
0

我想从这个简单的html网格数据推入sql,但我不能这样做。在一开始,我得到了var var impressions没有索引的错误。我修正了这一点。当我只使用广告客户价值时,我可以将数据推送到MySQL,而使用第二个值时,我无法成功。你能解释我做错了什么吗? 在此先感谢如何将数据从html网格推送到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> 
+0

仅供参考,[你不应该在新代码中使用'mysql_ *'函数](http://stackoverflow.com/questions/12859942/)。他们不再被维护[并被正式弃用](https://wiki.php.net/rfc/mysql_deprecation)。看到[红盒](http://php.net/manual/en/function.mysql-connect.php)?学习[*准备的语句*](https://en.wikipedia.org/wiki/Prepared_statement),并使用[PDO](http://php.net/pdo)或[MySQLi](http:// php.net/mysqli) - [这篇文章](http://php.net/manual/en/mysqlinfo.api.choosing.php)将帮助你决定哪一个最适合你。 –

+0

你的脚本存在[SQL注入攻击]的风险(http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)看看发生了什么事[Little鲍比表](http://bobby-tables.com/)即使[如果你逃避投入,它不安全!](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around -mysql-real-escape-string)使用[prepared parameterized statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php)。 –

+1

你不知道什么是错的,因为你不检查代码中的错误。永远不要假设代码总是完美无缺地工作。 –

回答

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> 
+0

嘿,谢谢你的回复。问题在于我需要让用户在单元格中输入日期,并且应该可以编辑内容。另外我有一个按钮添加行,插入一个新的原料,但不工作,如果使用表单类型作为输入。会发布完整的代码作为我自己的回复 –

+0

在这种情况下,使用第二个字段并取出禁用的属性。如果你想手动插入行,你实际上可以将输入类型设置为一个数组。所以name =“advertiser []”或name =“impressions []”,那么当你在后端解析时,你可以做一个foreach。(顺便提一下,如果只提交了一行,浏览器会将其作为文本发送,而不是作为数组发送,因此您需要检查两者的代码) https://johnrockefeller.net/html- input-forms-sending-in-an-array-in-php/ – Jake

+0

感谢您的回复。如果我去第二个选项删除禁用的属性,单元格是一个窗体,输入的最后一个值不是“存储”在网格中,我需要显示该值。此外,与添加行按钮关联的函数不会工作(可能是我的错误,我要重新检查代码)。再次感谢,对不起,如果我要求平庸的问题 –

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> 
相关问题