我对SQL数据库的动态值有疑问。 我使用JavaScript来创建动态表(包括表单), 但我知道如何使用SQL语言将动态值插入SQL数据库。 现在,我只能向SQL数据库插入一个值。
适用于addRow的JavaScript
<script language="JavaScript" type="text/javascript">
function addRowToTable()
{
var tbl = document.getElementById('tblSample');
var lastRow = tbl.rows.length;
// if there's no header row in the table, then iteration = lastRow + 1
var iteration = lastRow;
var row = tbl.insertRow(lastRow);
// left cell
var cellLeft = row.insertCell(0);
var che = document.createElement('input');
che.type = 'checkbox';
che.id = 'op'+ iteration;
che.name= 'check';
cellLeft.appendChild(che);
// right cell
var cellRight = row.insertCell(1);
var el = document.createElement('input');
el.type = 'text';
el.name = 'txtRow';
el.id = 'txtRow' + iteration;
el.size = 40;
el.onkeypress = keyPressTest;
cellRight.appendChild(el);
// select cell<font face="@Batang" size="+1"></font>
var cellRightSel = row.insertCell(2);
var sel = document.createElement('select');
sel.name = 'selRow';
sel.id = 'selRow' + iteration;
sel.options[0]= new Option("<?php
mysql_connect("localhost", "root", "root") or
die("Could not connect: " . mysql_error());
mysql_select_db("partition");
$query = "SELECT tile FROM material ORDER BY `material`.`Material_ID` ASC ;";
$result = mysql_query($query);
$row = mysql_fetch_row($result);
mysql_data_seek($result,0);
while ($row=mysql_fetch_row($result))
{
for ($i=0;$i<mysql_num_fields($result);$i++)
{
if ($row[$i]=="FF")
echo($row[$i]);
}
}
mysql_free_result($result);
?>","<br>");
form.php的
<form action="insert.php" method="post">
<table border="1" id="tblSample">
<tr>
<th colspan="3">Tiles</th>
</tr>
<tr>
<td><input type="checkbox" id="op1" name="check">
</td>
<td><input type="text" name="txtRow"
id="txtRow1" size="40"/></td>
<td>
<?php
function tile(){
mysql_connect("localhost", "root", "root") or
die("Could not connect: " . mysql_error());
mysql_select_db("partition");
$query = "SELECT tile FROM material ORDER BY `material`.`Material_ID` ASC ;";
$result = mysql_query($query);
$row = mysql_fetch_row($result);
mysql_data_seek($result,0);
while ($row=mysql_fetch_row($result))
{
for ($i=0;$i<mysql_num_fields($result);$i++)
{
echo("<option>".$row[$i]."</option>");
}
}
mysql_free_result($result);
}
?>
<select name="selRow" id="selRow1">
<?php tile(); ?>
</select>
</td>
</tr>
</table>
<input type="button" value="Add" onClick="addRowToTable();" />
<input type="button" name="delete_button" value="Delete" onClick="deleteAll('check');" />
<input type="submit" value="Submit">
</form>
insert.php
<?php
$link = mysql_connect("localhost","root","root");
mysql_select_db("test", $link);
$sql="insert into Form (Number,Tile)
values ('".$_POST["txtRow"]."','".$_POST["selRow"]."');";
mysql_query($sql, $link);
mysql_close($link);
echo "Success!";
?>
<br>
<?php
$link = mysql_connect("localhost","root","root");
mysql_select_db("test", $link);
$q = "SELECT * FROM Form;";
$rs = mysql_query($q, $link);
if(!$rs){die("Valid result!");}
echo "<table border=1>";
echo "<tr><td>Number</td><td>Tiles</td></tr>";
while($row = mysql_fetch_row($rs))
echo "<tr><td>$row[1]</td><td>$row[2]</td></tr>";
echo "</table>";
mysql_free_result($rs);
?>
我想得到一个示例,将动态值插入SQL数据库
如果有人有一个很好的例子,谢谢!
我创建了JavaScript动态表,因此产生了一个非常严重的问题....
答案 0 :(得分:1)
从JavaScript Addrow中,尝试使用单引号而不是quatation标记,以避免因连接而产生混淆:
sel.options[0]= new Option("<?php
mysql_connect('localhost', 'root', 'root') or die('Could not connect: ' . mysql_error()); mysql_select_db('partition');
直到最后一行,使用单引号作为php值。