在数据库表中保存运行时创建的文本框值

时间:2012-09-21 11:37:14

标签: php javascript mysql html

我创建了一个表单,其中包含一个文本框,另一个添加另一个按钮&保存按钮,点击事件添加另一个按钮生成新文本框但是当我点击保存按钮时新生成的文本框值无法保存到数据库中请任何人都可以指导我

这是我的代码:

<?php
    global $Hostname;   
    global $Username;   
    global $Password;           
    global $Database_name;  

function getConnection()    
{
    $Hostname = "localhost";    
    $Username ="root";
    $Password ="";
    $Database_name="labdata";

    $oMysqli = new mysqli($Hostname,$Username,$Password,$Database_name);    //create connection object.

    return($oMysqli);   
}
if(isset($_POST['submit']))
{
    $TestParameters = $_POST['testparameters'];
    $InsertQuery = "INSERT INTO test_table VALUES('$TestParameters')";
    $oMysqli=getConnection();
    $oMysqli->query($InsertQuery);

    //print_r($InsertQuery);exit();
    if(!$InsertQuery)
                {
                    die('Could not enter data:' . mysql_error());
                }
}
?>
<html>
<head>
<title>TestData</title>
<script type="text/javascript"> 
function create_row()   
{
      var newtr=document.createElement("tr");   
      var newtd=document.createElement("td");  
    var output="<input type=\"text\" name=\"testparameters\">";
      newtd.innerHTML=output;  
      newtr.appendChild(newtd); 
      document.getElementById("table1body").appendChild(newtr);

}
</script>
 </head>
<body>
<form name="testdetails" method="post" target="_self" action="<?php $_PHP_SELF ?>">

<label for="Testparameter">Testparameter</label>
<input type="text" name="testparameters"></input>
    <table id="table1body">
           <tr>     
     <td><input type="button" name="button" value="Add Test Parameter" onclick="create_row()"> 
          </tr>
    </table>
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>

3 个答案:

答案 0 :(得分:2)

试试这个

if(isset($_POST['submit']))
    {

        $length = count($_POST['testparameters']);

        for($i=0; $i< $length; $i++){
            $TestParameters=$_POST['testparameters'][$i];
            if(!empty($TestParameters)){
            $InsertQuery = "INSERT INTO color(name) VALUES('$TestParameters')";
            $result=mysql_query($InsertQuery) or die(mysql_error());
        }
        }
        if(!$InsertQuery)
        {
            die('Could not enter data:' . mysql_error());
            }
    }

答案 1 :(得分:1)

<?php
$n=0;
if(isset($_GET['button1']))
{




$n++;
     echo "hai";
      echo "<br><input type='text' name='+i+'>";

}
     if(isset($_POST['submit']))
{

      $con=mysqli_connect("localhost","root","","ram123");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
//print_r($_POST);
  $count = count($_POST);
 for($x=1;$x<$count;$x++){  
     $sql="INSERT INTO  names (name)VALUES('$_POST[$x]')";

    if (!mysqli_query($con,$sql))
      {
      die('Error: ' . mysqli_error($con));
      }

 }
 /* 
$sql="INSERT INTO  names (name)VALUES('$_POST[1]')";

if (!mysqli_query($con,$sql))
  {
  die('Error: ' . mysqli_error($con));
  }
echo "1 record added";

mysqli_close($con);
*/
}
?>

<html>
<head>
<title>TestData</title>
<script type="text/javascript"> 
var i = 1;
function changeIt()
{

my_div.innerHTML = my_div.innerHTML +"<br><input type='text' name="+i+">";
i++;
}

</script>
 </head>
<body>
<form name="testdetails" method="post" target="_self" action="<?php $_PHP_SELF ?>">





     <input type="button" name="button1" value="Add Test Parameter" onclick="changeIt()"> 



<input type="submit" name="submit" value="submit">
<div id="my_div"></div>
</form>
</body>
</html>

答案 2 :(得分:0)

您对所有文本框使用相同的名称

<input type="text" name="testparameters"></input>

使用数组,从中你将获得所有文本框的值

<input type="text" name="testparameters[]"></input>

迭代文本框值的代码

$length = count($_POST['testparameters']);
for($i=0; $i< $length; $i++){
    echo $_POST['testparameters'][$i];
}