如何向SQL数据库插入动态值


How to insert dynamic values to the SQL database?

我有一个关于SQL数据库动态值的问题。我使用JavaScript创建动态表(包括表单),但是我不知道如何使用SQL语言将动态值插入到SQL数据库中。现在,我只能在SQL数据库中插入一个值。

JavaScript for addRow
<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动态表,所以造成了一个非常严重的问题....

从JavaScript Addrow中,尝试使用单引号而不是引号来避免由于连接而引起的混淆:

sel.options[0]= new Option("<?php

mysql_connect('localhost', 'root', 'root') or die('Could not connect: ' . mysql_error()); mysql_select_db('partition');

直到最后一行,PHP值使用单引号