I have a loop that iterates as many times as per the number entered. The loop iterates and generates as many forms as required dynamically. I want to use the form data collected, to be stored in a database.
My loop is running. Forms are getting generated as many times as the number specified. Data is getting inserted into database but the table column is showing blank data. Rows are being created but the column is blank.
I tried a lot but couldn't find whats wrong with the code. Any help will be appreciated.
<form action = '<?php echo $_SERVER['PHP_SELF']; ?>' method = 'post'>
How many questions ?
<input type = 'number' name = 'number1'>
<input type = 'submit' value = 'go'>
</form>
<?php
$number = $_POST['number1'];
?>
<?php
for($i = 1; $i<$number; $i++)
{
?>
<form action = '<?php echo $_SERVER['PHP_SELF']; ?>' method = 'post'>
<input type = 'text' name = 'name<?php echo $i; ?>'>
<?php
}
?>
<input type = 'submit' value = 'submit'>
<?php
for($i = 1; $i<$number; $i++)
{
$namee = $_POST['name'.$i];
$user = 'root' ;
$password = 'password';
$host = 'localhost' ;
$database = 'database1';
$db = mysqli_connect($host, $user, $password, $database) or die('Error querying') ;
$query = "INSERT INTO table1(question)" . "VALUES ('$namee')" ;
mysqli_query($db, $query) ;
mysqli_close($db);
}
?>
Edit: Oh well, as you edited your question..
The answer is here: $_POST['name.$i'];
Change it to $_POST['name'.$i];
and read about concatenation here.
Edit2: You are not checking if you have variables sent. When you hit first submit button, you are not giving any text to the $_POST['name*']
because that form is being created. When you hit submit with filled names, you don't have numbers in the post. Here you have the rewritten script. Look for everything there. You don't need to create mysql connection X times, once is enought. Also see as I filter the functions for you because you are not using PDO statements. You can see the intval()
function right here. Homework for you will be to find out how to filter $name
. Then also checking if user really filled in the form is also really important. This script is not even in 50% as good as it could be. If you will get to the point where the script is OK, read about PDO statements & input arrays & so on..
<?php
$number = intval($_POST['number1']);
$forms = intval($_POST['forms']);
if($forms)
{
$user = 'root' ;
$password = 'password';
$host = 'localhost' ;
$database = 'database1';
$db = mysqli_connect($host, $user, $password, $database) or die('Error querying') ;
for($i = 1; $i <= $number; $i++)
{
$name = $_POST['name'.$i]; //how would you filter that one?
if(!empty($name))
mysqli_query($db, "INSERT INTO table1(question)" . "VALUES ('$name')") ;
}
mysqli_close($db);
}
?>
<form action = '<?php echo $_SERVER['PHP_SELF']; ?>' method = 'post'>
How many questions ?
<input type = 'number' name = 'number1' value='<?= $number ?>'>
<input type = 'submit' value = 'go'>
</form>
<?php
if($number > 0)
{
echo '<form action="'.$_SERVER['PHP_SELF'].'" method="post">'; //X forms is also not needed, one is enough
for($i = 1; $i<$number; $i++)
{
echo '<input type = "text" name = "name'.$i.'">';
}
echo '
<input type = "submit" value = "submit">
<input type="hidden" name="forms" value="1" />
</form>
';
}
?>