I am inserting multiple line values from a html table form in to sql but it's only inserting the last table form value in my database. I can't figure out where the problem is.
Can you help me out with this?
This is my PHP:
$result = mysql_query("SELECT * FROM ex_marks WHERE session='$session' and cl_name='$cl_name' and cl_section='$cl_section' and subject='$subj' and exam='$exam' and date='$date' and roll_no='$rollno' and obtainmarks='$marks'");
if (mysql_num_rows($result) == 0)
{
mysql_query("INSERT INTO ex_marks(mid, session, cl_name, cl_section, name, fname, status, date, exam, roll_no, subject, obtainmarks, maxmarks, passmarks)
VALUES('', '$session', '$cl_name', '$cl_section', '$name','$fname', '$attendance', '$date', '$exam', '$rollno', '$subj','$marks','$maxmarks','$passmarks')") or die(mysql_error());
echo "<script type='text/javascript'>alert('Submitted Successfully!')</script>";
}
else
{
echo "<script type='text/javascript'>alert('Already Exist!')</script>";
}
i try to add fro each loop in this but that also not work and add only 1st table data in databse...
you should try using the correct syntax when using sql. since words like date is part of sql it needs proper syntax.
try:
<?php
include('session.php');
include('connect.php');
$msg="";
$opr="";
if(isset($_GET['opr']))
$opr=$_GET['opr'];
if(isset($_GET['rs_id']))
$id=$_GET['rs_id'];
if($opr=="del")
{
$del_sql=mysql_query("DELETE FROM stu_tbl WHERE stu_id=$id");
if($del_sql)
$msg="Record Deleted... !";
else
$msg="Could not Delete :".mysql_error();
}
if(isset($_POST['btn_sub']))
{
foreach($_POST['marks'] as $row=>$Act)
{
$session=$_GET['session'];
$cl_name=$_GET['cl_name'];
$cl_section=$_GET['cl_section'];
$exam=$_GET['exam'];
$date=$_GET['date'];
$subj = $_GET['subj'];
$marks = mysql_real_escape_string($Act);
$maxmarks = mysql_real_escape_string($_POST['maxmarks'][$row]);
$passmarks = mysql_real_escape_string($_POST['passmarks'][$row]);
$name = mysql_real_escape_string($_POST['name'][$row]);
$rollno = mysql_real_escape_string($_POST['rollno'][$row]);
$fname = mysql_real_escape_string($_POST['fname'][$row]);
$attendance = mysql_real_escape_string($_POST['attendance'][$row]);
$result = mysql_query("SELECT * FROM `ex_marks` WHERE `session`='$session' and `cl_name`='$cl_name' and `cl_section`='$cl_section' and `subject`='$subj' and `exam`='$exam' and `date`='$date' and `roll_no`='$rollno' and `obtainmarks`='$marks'");
if (mysql_num_rows($result) == 0)
{
mysql_query("INSERT INTO `ex_marks` (`mid`, `session`, `cl_name`, `cl_section`, `name`, `fname`, `status`, `date`, `exam`, `roll_no`, `subject`, `maxmarks`, `passmarks`, `obtainmarks`)
VALUES('', '$session', '$cl_name', '$cl_section', '$name','$fname', '$attendance', '$date', '$exam', '$rollno', '$subj', '$maxmarks','$passmarks','$marks')") or die(mysql_error());
echo "<script type='text/javascript'>alert('Submitted Successfully!')</script>";
}
else
{
echo "<script type='text/javascript'>alert('Already Exist!')</script>";
}
}
}
?>
we may need to see your file with form details to be able to diagnose the problem further.
but you should still fix the sql syntax like i mentioned above.
Thayla was saying the last row of form was getting inserted, so if its not a loop problem then I'm guessing its a form problem.
At a guess I would say that the form data isn't being put into an array, Guessing its overwriting the last row into a string instead.
The form is all good. (apart from the missing form opening)
I think by the looks of things that:
$maxmarks = mysql_real_escape_string($_POST['maxmarks'][$row]);
$passmarks = mysql_real_escape_string($_POST['passmarks'][$row]);
Just tested
print_r($_POST['marks']); returns fine
It is what i said maxmarks and passmarks are empty
its because $row is marks:
foreach($_POST['marks'] as $row=>$Act)
so:
$_POST['maxmarks'][$row];
does not make seance as it is nothing to do with marks and ideally shouldn't even be in the loop
(its a separate input above the table)