page_form.php
echo '<div style="text-align:center;margin-left:25px;">
<form action="grad.php" method="post">
<table width-"100%">
<tr><th style="padding:12px;">LETTER </th>
<th style="padding:12px;">INTERVAL</th>
<th style="padding:12px;">GT</th>
</tr>';
for($i=0;$i<=5;$i++)
{
echo '<tr><td style="padding:12px;"><input type="text" name="letter_'.$i.'"></td>
<td style="padding:12px;"><input type="text" name="interval'.$i.'"></td>
<td style="padding:12px;"><input type="text" name="gt'.$i.'"></td>
</tr>';
}
echo '<tr><td><input type="submit" name="submit" value="submit"></td></tr>';
echo '</table>
</form>
</div>';
grad.php
$letter = $_POST['letter'];
$interval = $_POST['interval'];
$gt = $_POST['gt'];
$s = mysql_query("INSERT INTO grad_table(letter,markint,gradepoint) VALUES('$letter',$interval,$gt) ");
I have given all the textboxes in a forloop. when I click on submit I need to get all the 5 textbox values, but right now iam getting only the 1st row.
Database Structure
Field Type Collation Attributes Extra
id bigint(10) UNSIGNED AUTO_INCREMENT
letter varchar(255) utf+general_ci
markint bigint(10)
gt bigint(10)
Your textbox names are not
letter, as you’re using them – they areletter_0,letter_1, etc.Also,
intervalis a MySQL reserved-word and you’ll need to escape it using backticks:`interval`.To retrieve all of them, you can hardcode all of their names such as
$letter1 = $_POST['letter_1'];, or you can do it in another loop:One big thing to note here is that you should really validate your user-input before inserting it into the database. First, you can use
isset()before accessing the variables to make sure the user submitted the data. Second, you can usemysql_real_escape_string()to sanitize the string input andintval()for the integers:Now, this doesn’t “validate” your data, but at least it will help prevent SQL injection and simply PHP errors.