i need code for insert multiple rows into database please help me
sudharshan9xweb 0 Newbie Poster
veedeoo 474 Junior Poster Featured Poster
Hi,
It would help, if you can show us your codes. It does not matter if it is working or not.
sudharshan9xweb 0 Newbie Poster
<?php
for($n=0;$n<3;$n++)
{
if(isset($_POST['roll']))
{
echo "<br>Updating";
$telugu=$_POST['telugu'];
$hindi=$_POST['hindi'];
$english=$_POST['english'];
$maths=$_POST['maths'];
$science=$_POST['science'];
$social=$_POST['social'];
$gk=$_POST['gk'];
$class = $_POST['class'];
$sql="insert into student_mark (student_id,student_class,telugu,hindi,english,maths,science,social,gk)
VALUES ('$student_id','$class','$telugu','$hindi','$english','$maths','$science','$social','$gk')";
$res=mysql_query($sql);
}
}
?>
<input type="hidden" value="<?php echo "$class"; ?>" name="class" />
</div>
<div class="bodytext4" id="table2">
<span class="bodytext9">
<?php echo "$class"; ?>
</span><br />
</div>
<div class="bodytext4" style="width:50px;" id="table2">
<span class="bodytext9"><input type="text" name="telugu" style="width:25px;" /></span> </div>
<div class="bodytext4" style="width:50px;" id="table2">
<span class="bodytext9"><input type="text" name="hindi" style="width:25px;" /></span> </div>
<div class="bodytext4" style="width:50px;" id="table2">
<span class="bodytext9"><input type="text" name="english" style="width:25px;" /></span> </div>
<div class="bodytext4" style="width:50px;" id="table2">
<span class="bodytext9"><input type="text" name="maths" style="width:25px;" /></span> </div>
<div class="bodytext4" style="width:50px;" id="table2">
<span class="bodytext9"><input type="text" name="science" style="width:25px;" /></span> </div>
<div class="bodytext4" style="width:50px;" id="table2">
<span class="bodytext9"><input type="text" name="social" style="width:25px;" /></span> </div>
<div class="bodytext4" style="width:50px;" id="table2">
<span class="bodytext9"><input type="text" name="gk" style="width:25px;" /></span> </div>
</div>
<?php
} ?>
<input type="submit" name="roll" value="insert" class="signin" />
</form>
</div>
diafol
Your form seems to only support a single record post. I'd suggest a javascript-driven "add row" on the form which should enable you to dynamically add new rows.
Get the js to insert these into the form, e.g. on 'add' button press:
<input name="telugu[]" />
<input name="hindi[]" />
(etc)
You then pick up the post data as an array of arrays. For example, say you posted 3 rows...
$val = array();
$count = count($_POST['telugu']);
for($x=0;$x<$count;$x++)
{
//make $_POST vars safe, e.g.
//$telugu = trim(mysql_real_escape_string($_POST['telugu'][$x]));
//etc
$val[] = "('$student_id','$class','$telugu','$hindi','$english','$maths','$science','$social','$gk')";
}
Then you can implode the $val array to create your VALUES clause
There are a number of ways to do this. This is just one. I haven't included any validation.
Edited by diafol
harjinder007 15 Newbie Poster
you can use this code:
you just need to instantiate EE. So for your form targets a result page/template as normal html, for example: <form action="/form-result"> (or whatever), then the quick and dirty way would be simply to include PHP in the template receiving the form.
On that page (receiving the form), have the following (template must have PHP enabled):
<?php
$EE =& get_instance();
$postUsers = $EE->input->post('user');
foreach($postUsers as $user) {
// Extra Cleaning
$user = $EE->db->escape_str($user);
$user = $EE->security->xss_clean($user);
// Fields and values to save to DB
$dbFields = array(
'user' => $user['name'],
'party' => $user['party'],
'marks' => $user['marks'],
'comments' => $user['comments']
);
// Prefix variable used for good practice, but could hard code
$EE->db->insert($EE->db->dbprefix . 'mytable', $dbFields);
}
?>
I've shown the long method of recreating the array to send to the DB in case you want to modify or add more fields, but you could simply just pass $user (after cleaning) to the database as the array will have the same structure.
If it's multiple POST variables sent by the form (instead of the array you imply), you'd need something like:
foreach($_POST as $var) {
$postUsers = $EE->input->post($var);
A more ideal solution would be developing a plugin that would be called from the resulting template to trigger the above, or an extension that 'listened' for the appropriate form to be submitted, possibly by using the ACT security ability built into EE. But that's probably added an unnecessary level of complexity for your requirement.
best of luck.
diafol
Are you talkign about ExpressionEngine? If so, why would you need to implement that just in order to add a few records to a DB?
subuhi_1 16 Newbie Poster
If I am having a table that contains 8 rows suppose. And from that 8 rows i want to select only 4 rows and these 4 rows has to get inserted to database. How to do this? Also I am having checkbox associated to every row. Please help me urgently!!!
Thanks in advance
rproffitt commented: Just so you know, hiding a question under a 7 year old discussion usually isn't noticed. Make a new discussion. +16
subuhi_1 16 Newbie Poster
No Actually You can say I am having one table which has 100 records. From that records if user selcts 8 records then that 8 records only should be send to database. I am using sql for database and jsp to insert recirds.
Thank you!
subuhi_1 16 Newbie Poster
This is my connectivty code
<%@ page import = "java.sql.*"%>
<%
String chk = "";
String a[]=request.getParameterValues("chk");
for(int i=0;i<a.length;i++){
chk+=a[i]+"";
}
try
{
Statement st = null;
ResultSet rs = null;
Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/katalystz?sessionVariables=sql_mode='NO_ENGINE_SUBSTITUTION'&jdbcCompliantTruncation=false", "root", "root");
String updatecust = "insert into vouchers(chk) values('"+chk+"');";
try
{
int i=st.executeUpdate(updatecust);
if(i>0){
out.print(" saved successfully....!!");
}
else
out.print("Sorry..!! Got an exception.");
}
catch(Exception e)
{
System.out.print(e);
e.printStackTrace();
}
}
catch(Exception e)
{
System.out.print(e);
e.printStackTrace();
}
%>
And this is my Frontend Code
<table border="1" class = "mainTable">
<tr>
<th><input type="checkbox" value="Service Description" id = "chk"/>Service Description</th>
<th>Standalone yes/no</th>
<th>Print at Service level</th>
<th>Service Group</th>
<th>Qualifier</th>
<th>Qualifier Priority</th>
</tr>
<%
try{
connection = DriverManager.getConnection(connectionUrl+database, userid, password);
statement=connection.createStatement();
String sql ="select * from Service_screen";
resultSet = statement.executeQuery(sql);
while(resultSet.next()){
%>
<tr>
<td name = "sDesc"><input type="checkbox" name = "chk"/><%=resultSet.getString("service_description")%></td>
<td ><input value = <%=resultSet.getString("standalone")%> name = "standalone" readonly> </td>
<td ><input type ="text" name = "patSer"></td>
<td ><input type ="text" name = "sGroup"></td>
<td ><input type ="text" name = "qual"></td>
<td><select name = "qualPrio">
<option value="Mandatory">Mandatory</option>
<option value="Optional">Optional</option>
</select></td>
</tr>
<%
}
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
%>
</table>
</form>
</div>
<div>
<input type = "button" value = "Add Service" class = "TransferRows">
<a class="button2" >Add Charge</a>
</div>
<form action ="serCharg.jsp" method = "post">
<center>
<table border = "1" class = "subtable">
<tr>
<th>Service Description</th>
<th>Standalone yes/no</th>
<th>Print at Service level</th>
<th>Service Group</th>
<th>Qualifier</th>
<th>Qualifier Priority</th>
</table>
subuhi_1 16 Newbie Poster
<script>
$(function(){
$(document).on("click",".TransferRows",function(){
var getselValue = $(".mainTable input:checked").parents("tr").clone().appendTo($(".subtable tbody").add(getselValue));
});
});
</script>
This is my script for inserting new row
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.