我需要帮助编写动态表的值到SQL的插入,这是我的PHP代码
<?php
require_once('../../../mysqlConnector/mysql_connect.php');
$query= "INSERT INTO spotreportref (spotreportID,numfire,typefire,numdrug,typedrug) VALUES (); ";
$resultID=mysqli_query($dbc,$query);
echo $id;
echo $txtnumfire;
echo $txttypefire;
echo $txtnumdrug;
echo $txttypedrug;
}?>
这是我的添加脚本,我只需要将值保存到我不知道该怎么做的数据库中。
<script type=text/javascript>
var addButton=document.getElementById("add-button");
//listen to add button. If clicked, store the entered data and append them in the display
addButton.addEventListener('click', add_row, false);
function add_row(){
event.preventDefault();
var new_numfire=document.getElementById("txtnumfire").value;
var new_typefire=document.getElementById("txttypefire").value;
var new_numdrug=document.getElementById("txtnumdrug").value;
var new_typedrug=document.getElementById("txttypedrug").value;
var table=document.getElementById("data_table");
var table_len=(table.rows.length)-1;
var row = table.insertRow(table_len).outerHTML=
"<tr id= 'row"+table_len+"'>\
<td id='numfire_row"+table_len+"'>"+new_numfire+"</td>\
<td id='typefire_row"+table_len+"'>"+new_typefire+"</td>\
<td id='numdrug_row"+table_len+"'>"+new_numdrug+"</td>\
<td id='typedrug_row"+table_len+"'>"+new_typedrug+"</td>\
<td><input type='button' id='edit_button"+table_len+"</td>\
<input type='button' id='save_button"+table_len+"' value='Save' class='save' onclick='save_row("+table_len+")'> \
<input type='button' value='Delete' class='delete' onclick='delete_row("+table_len+")'>\
</td>\
</tr>";
}
</script>
答案 0 :(得分:0)
出于安全原因,您应该使用预准备语句,所以......
$query= "INSERT INTO spotreportref (spotreportID,numfire,typefire,numdrug,typedrug) VALUES (?,?,?,?,?); ";
$result=$dbc->mysqli_prepare($query);
$result->bind_param("issss", $id, $txtnumfire, $txttypefire, $txtnumdrug, $txttypedrug);
$result->execute();
问题是如何获取$ id等的值。如果你发布它们,它将是$_POST[]