Jump to content

create text field dynamically!


mtvaran

Recommended Posts

Hi guys i have to create text field & enter data and store in the data base. here im able to create text field but couldn't insert the data. so could anyone please check this code for me.

 

<body>
<form method="POST" action="cell.php">
Enter the number of question
<input type="text" name="question">
<input type="submit">




  <?php
$value=$_POST['question'];

for($i=0;$i<$value;$i++)
{
echo "Question NO:";
echo '<input type="text" name="qno">'."\t";

echo "Enter Marks:";
echo '<input type="text" name="marks">'."\t";
echo "<br>";
}
?>
</form>
<form name="form1" method="post" action="cellresult.php">
  <label>
  <input type="submit" name="Submit" value="Submit">
  </label>
</form>

</body>

 

 

 

cellresult.php

<body>
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("test", $con);

$sql="INSERT INTO cell (QNO,MARKS)
VALUES
('$_POST[qno]','$_POST[marks]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";

mysql_close($con)
?>
</body>

 

Link to comment
https://forums.phpfreaks.com/topic/218308-create-text-field-dynamically/
Share on other sites

Your form elements need to be named so that PHP recognizes them as an array.  To do this, you would rename qno to qno[], marks to marks[].

 

Then you need to alter your PHP script so that it handles $_POST['qno'] and $_POST['marks'] as arrays.  Your SQL will need to be dynamically generated with multiple value lists, e.g.

INSERT INTO cell (QNO,MARKS) VALUES
    (1, 90), (2, 85), (3, 85), (4, 83)

i did change as u said but still error.... im bit new & stupid with php things. :) could you check this for me pls?

{
echo "Question NO:";
echo '<input type="text" name="qno[]">'."\t";

echo "Enter Marks:";
echo '<input type="text" name="marks[]">'."\t";
echo "<br>";
}

$sql="INSERT INTO cell (QNO,MARKS)
VALUES
('$_POST['qno']','$_POST['marks']')";

 

You missed the part about

Then you need to alter your PHP script so that it handles $_POST['qno'] and $_POST['marks'] as arrays.  Your SQL will need to be dynamically generated with multiple value lists, e.g.
INSERT INTO cell (QNO,MARKS) VALUES
    (1, 90), (2, 85), (3, 85), (4, 83)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.