0

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

  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

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

?>
Dharman
  • 30,962
  • 25
  • 85
  • 135
krish
  • 120
  • 14
  • 1
    use `for` loop and also `limit` the `row count` with some `logic` otherwise user will `mess up` with `large number` entering in `input` . – JYoThI Aug 04 '17 at 09:59
  • I would expect the `Id` column to be an autoincrement column! Is that the case with this table? – RiggsFolly Aug 04 '17 at 10:00

2 Answers2

1

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>";
    }
Deblugger
  • 153
  • 1
  • 11
  • sir first time i added 5 records in table.but when next time i trying to add 5 more records they also starts from 1 to five..but i want to add them after 5 like 6 to 10 – krish Aug 04 '17 at 10:44
  • so you need a select before the loop where you get the max. number on the table and `for($i = $maxNumber; $i <= $Numbers + $maxNumber; $i++)` – Deblugger Aug 04 '17 at 10:45
-1

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 -

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>";

}
GYaN
  • 2,327
  • 4
  • 19
  • 39
  • 1
    ok sir i'll do like this..but sir if first time i added 5 rows in table.next time i need to add more rows the rows agin starts from 1 ..but i need to add lines after 5 – krish Aug 04 '17 at 10:41