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
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)

Link to comment
Share on other sites

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']')";

 

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.