mtvaran Posted November 10, 2010 Share Posted November 10, 2010 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> Quote Link to comment Share on other sites More sharing options...
simshaun Posted November 10, 2010 Share Posted November 10, 2010 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) Quote Link to comment Share on other sites More sharing options...
mtvaran Posted November 10, 2010 Author Share Posted November 10, 2010 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']')"; Quote Link to comment Share on other sites More sharing options...
simshaun Posted November 10, 2010 Share Posted November 10, 2010 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) Quote Link to comment Share on other sites More sharing options...
mtvaran Posted November 10, 2010 Author Share Posted November 10, 2010 sorry i do not get the array things. i know im bit stupid. so could anyone pls provide the code for rest of it? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.