i am developing a school management system,n came into ambigious situation, how can i add school fee for a student for different academic years into the database in one row?
Two choices. Add 4 fields, one for each of a student's 4 years, or add a table that includes the student's ID number, the year and the fee. The second method is preferred for purity, but the first method will probably result in easier code.
if you want code then here it is mysql_query("Insert into table_name (Col1,Col2,Col3,Col4) VALUES ('Col1_Value','Col2_Value','Col3_Value','Col4_Value')"); PHP:
If you want to add directly: <?php $con = mysql_connect("localhost","peter","abc123"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("my_db", $con); mysql_query("INSERT INTO Persons (FirstName, LastName, Age) VALUES ('Peter', 'Griffin',35)"); mysql_query("INSERT INTO Persons (FirstName, LastName, Age) VALUES ('Glenn', 'Quagmire',33)"); mysql_close($con); ?> PHP: Through Form : <html> <body> <form action="insert.php" method="post"> Firstname: <input type="text" name="firstname" /> Lastname: <input type="text" name="lastname" /> Age: <input type="text" name="age" /> <input type="submit" /> </form> </body> </html> PHP: Insert PHP Code: <?php $con = mysql_connect("localhost","peter","abc123"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("my_db", $con); $sql="INSERT INTO Persons (FirstName, LastName, Age) VALUES ('$_POST[firstname]','$_POST[lastname]','$_POST[age]')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "1 record added"; mysql_close($con); ?> PHP:
Seoexposure and Shubham, You shouldn't be using mysql_* functions. They've been deprecated for a while, look on PHP.net. Use PDO or mysqli
One thing you could do is check the $_POST array and recommended you make sure the variables are cleaned - but then use a foreach() statement and place your insert statement there. That would then essentially loop over the $_POST array and then add it, instead of having to do it line by line. Let me know if this is clear and if not then I will show a working example.
You can use implode and explode to do it. Implode it before inserting to database and explode it after selecting from database. 8->