2

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

这是我的代码:

<?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>
4

3 回答 3

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());
            }
    }
于 2012-11-23T06:39:12.020 回答
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>
于 2013-10-08T07:46:58.407 回答
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];
}
于 2012-09-21T11:40:20.813 回答