【问题标题】:Save runtime created textbox value in database table在数据库表中保存运行时创建的文本框值
【发布时间】:2012-09-21 11:37:14
【问题描述】:

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

这是我的代码:

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

【问题讨论】:

    标签: php javascript mysql html


    【解决方案1】:

    试试这个

    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());
                }
        }
    

    【讨论】:

      【解决方案2】:
      <?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>
      

      【讨论】:

        【解决方案3】:

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

        <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];
        }
        

        【讨论】:

        • 当我使用 它将关键字数组存储到数据库中
        • 请指导我应该使用什么
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-23
        • 2012-07-07
        • 1970-01-01
        • 1970-01-01
        • 2012-03-24
        • 1970-01-01
        相关资源
        最近更新 更多