我如何使用包含数字的php变量在数据库表中创建多行

时间:2021-09-30 23:00:55

This is my code

这是我的代码

<form name="#" method="POST" action="#">
 <input type="text"  id="id" name="tid" />
<input type="text"  id="name" name="name" />
<input type="text"  id="addnumber" name="addnumber">
<input type="submit" name="submit1" id="submit1" value="Save Infomation" style="width: 300px; margin: 0 auto;">                 
</form>

This is Php my code

这是Php我的代码

  if(isset($_POST['submit1'])) {
        $Id=$_POST['id'];
        $Name=$_POST['name'];
        $Numbers=$_POST['addnumber'];

if in$_POST['addnumber'] user enter 30 then i want to create 30 rows in databse table

如果在$ _POST ['addnumber']用户输入30,那么我想在数据库表中创建30行

$insert = mysqli_query($con,"INSERT INTO tablename
                                    (Id,Name,Number) 
                                VALUES('".$Id."','".$Name."','".$Numbers."')");
 echo "<h1> Infomation Saved</h1>";
}

?>

2 个解决方案

#1


2  

if i understood maybe this works for you:

如果我明白这可能适合你:

for($i = 1; $i <= $Numbers; $i++){
        $insert = mysqli_query($con,"INSERT INTO tablename(Id,Name,Number) VALUES('".$Id."','".$Name."','".$i."')");
        echo "<h1> Infomation Saved</h1>";
    }

#2


0  

Instead of Use for loop for query every time first you add all values in one variable and then insert all of them in query. I'm giving you code below -

每次首先将所有值添加到一个变量中,然后在查询中插入所有值,而不是使用for for循环进行查询。我在下面给你代码 -

if(isset($_POST['submit1'])) {
    $Id=$_POST['id'];
    $Name=$_POST['name'];
    $Numbers=$_POST['addnumber'];
    $values = '';
    for($i=1; $i <= $Numbers; $i++){$values .="('".$Id."','".$Name."','".$i."'),"; }
    $values .= ';'
    mysqli_query($con,"INSERT INTO tablename(Id,Name,Number) VALUES".str_replace(',;',';',$values)." 
    echo "<h1> Infomation Saved</h1>";

}

#1


2  

if i understood maybe this works for you:

如果我明白这可能适合你:

for($i = 1; $i <= $Numbers; $i++){
        $insert = mysqli_query($con,"INSERT INTO tablename(Id,Name,Number) VALUES('".$Id."','".$Name."','".$i."')");
        echo "<h1> Infomation Saved</h1>";
    }

#2


0  

Instead of Use for loop for query every time first you add all values in one variable and then insert all of them in query. I'm giving you code below -

每次首先将所有值添加到一个变量中,然后在查询中插入所有值,而不是使用for for循环进行查询。我在下面给你代码 -

if(isset($_POST['submit1'])) {
    $Id=$_POST['id'];
    $Name=$_POST['name'];
    $Numbers=$_POST['addnumber'];
    $values = '';
    for($i=1; $i <= $Numbers; $i++){$values .="('".$Id."','".$Name."','".$i."'),"; }
    $values .= ';'
    mysqli_query($con,"INSERT INTO tablename(Id,Name,Number) VALUES".str_replace(',;',';',$values)." 
    echo "<h1> Infomation Saved</h1>";

}