如何将值从动态生成的输入插入数据库


How to insert values into a database from input that is dynamically generated

如果用户验证了某个值,则此处的表单显示输入,因此需要一个动态表单字段:

<form class="form-horizontal"  method="POST" action="ajouterProduit4" name="formulaire">                
<div class="panel panel-default tabs">                            
<ul class="nav nav-tabs" role="tablist">
	<li class="active"><a href="ajouterProduit2"><button name="btn2" style="background: transparent; border: none;">Fiche Technique</button></a></li>
	
</ul>
<div class="panel-body tab-content">
											
	<div class="tab-pane active" id="tab-second">
	
		<?php 
			$reqt1 = "SELECT c.Libelle,c.ID FROM caracteristique c,fichetechnique f WHERE c.fichetechniqueID=f.ID AND c.fichetechniqueID='$categorie' LIMIT 0,10";
			$reqt2 = "SELECT c.Libelle,c.ID FROM caracteristique c,fichetechnique f WHERE c.fichetechniqueID=f.ID AND c.fichetechniqueID='$categorie' LIMIT 10,10";
			$rest1=mysqli_query($conne,$reqt1);
			$rest2=mysqli_query($conne,$reqt2);
		?>
		<div class="col-md-6" id="txtHint">
			<?php
				while ($rowt1= mysqli_fetch_row($rest1)) {
			?>
			<div class="form-group">
				<label class="col-md-3 control-label"><?php echo $rowt1[0] ?></label>
				<div class="col-md-9">                                            
					<div class="input-group">
						<span class="input-group-addon"><span class="fa fa-pencil"></span></span>
						<input  name="rowt1"   type="text" class="form-control" />
						
					</div>                                            
					
				</div>
			</div> 
				<?php }  ?>
		</div>
		
		<div class="col-md-6">
			<?php
				while ($rowt2= mysqli_fetch_row($rest2)) {
			?>
		<div class="form-group">
			<label class="col-md-3 control-label"><?php echo $rowt2[0] ?></label>
			<div class="col-md-9">                                            
				<div class="input-group">
					<span class="input-group-addon"><span class="fa fa-pencil"></span></span>
					<input name="rowt2" type="text" class="form-control"/>
				</div>                                            
			</div>
		</div>
			<?php } ?>
		</div>
	   
	</div>                                        
   
</div>
			<div class="panel-footer">
				<input type="reset" class="btn btn-default" value="Réinitialiser">
				<button name="envoyer" class="btn btn-primary pull-right">Etape 3 <span class="fa fa-arrow-right fa-right"></span></button>
			</div>	
</div>
</div>                                
							
</form>

但是,当不清楚数据可能是什么(特别是类别)时,我不知道如何将数据插入数据库

在伪代码中,它将是("插入到类别(值)值($value)")

注意:我只使用过程式php

您可以使用 HTML 输入数组动态生成表单。

<?php $fruits = ['apple', 'orange', 'strawberry', 'pear'] ?>
<form method="POST" action="action.php">
    <?php for ($i=0; $i < count($fruits); $i++): ?>
        <input type="text" name="fruits[<?php echo $i ?>]" value="<?php echo $fruits[$i] ?>" />
    <?php endfor ?>
    <input type="submit" value="Submit" />
</form>

在上面的示例中,生成名称为 fruits 的输入类型。然后,您将表单提交到action.php(请参阅下面的代码)。

<?php
$fruits = $_POST['fruits'];
var_dump($fruits);

当您在浏览器上运行它时,结果如下。

array (size=4)
  0 => string 'apple' (length=5)
  1 => string 'orange' (length=6)
  2 => string 'strawberry' (length=10)
  3 => string 'pear' (length=4)

希望对您有所帮助。

使用此表单:

<form class="form-horizontal"  method="POST" action="ajouterProduit4" name="formulaire">                
<div class="panel panel-default tabs">                            
<ul class="nav nav-tabs" role="tablist">
    <li class="active"><a href="ajouterProduit2"><button name="btn2" style="background: transparent; border: none;">Fiche Technique</button></a></li>
</ul>
<div class="panel-body tab-content">
    <div class="tab-pane active" id="tab-second">
        <?php 
            $reqt1 = "SELECT c.Libelle,c.ID FROM caracteristique c,fichetechnique f WHERE c.fichetechniqueID=f.ID AND c.fichetechniqueID='$categorie' LIMIT 0,10";
            $reqt2 = "SELECT c.Libelle,c.ID FROM caracteristique c,fichetechnique f WHERE c.fichetechniqueID=f.ID AND c.fichetechniqueID='$categorie' LIMIT 10,10";
            $rest1=mysqli_query($conne,$reqt1);
            $rest2=mysqli_query($conne,$reqt2);
        ?>
        <div class="col-md-6" id="txtHint">
            <?php
                while ($rowt1= mysqli_fetch_row($rest1)) {
            ?>
            <div class="form-group">
                <label class="col-md-3 control-label"><?php echo $rowt1[0] ?></label>
                <div class="col-md-9">                                            
                    <div class="input-group">
                        <span class="input-group-addon"><span class="fa fa-pencil"></span></span>
                        <input  name="rowt1[]" value="<?php echo $rowt1[1]; ?>" type="text" class="form-control" />
                    </div>                                            
                </div>
            </div> 
                <?php }  ?>
        </div>
        <div class="col-md-6">
            <?php
                while ($rowt2= mysqli_fetch_row($rest2)) {
            ?>
        <div class="form-group">
            <label class="col-md-3 control-label"><?php echo $rowt2[0] ?></label>
            <div class="col-md-9">                                            
                <div class="input-group">
                    <span class="input-group-addon"><span class="fa fa-pencil"></span></span>
                    <input name="rowt2[]" value="<?php echo $rowt2[1]; ?>" type="text" class="form-control"/>
                </div>                                            
            </div>
        </div>
            <?php } ?>
        </div>
    </div>                                        
</div>
            <div class="panel-footer">
                <input type="reset" class="btn btn-default" value="Réinitialiser">
                <button name="envoyer" class="btn btn-primary pull-right">Etape 3 <span class="fa fa-arrow-right fa-right"></span></button>
            </div>  
</div>
</div>                                
</form>

在服务器端:

<?php 
$rowt1=$_REQUEST['rowt1'];// will return post array due to the declaration in the form as rowt1[]
$rowt2=$_REQUEST['rowt2'];// will return post array due to the declaration in the form as rowt2[]
foreach ($rowt1 as $key => $value) {
    $sql="INSERT INTO TABLENAME1 ('rowt1') VALUES ('".$value."')";
    // run the query
}
foreach ($rowt2 as $key => $value) {
    $sql="INSERT INTO TABLENAME2 ('rowt2') VALUES ('".$value."')";
    // run the query
}
?>

希望这可以解决您的问题。谢谢。