我的问题是当我编辑我的表格时有3行并保存数据,其插入3次,因为foreach循环而提供3个javascript警报
我不知道问题在哪里
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>";
}
}
}
请参阅此代码
答案 0 :(得分:0)
不要一直循环回复javascript。
knownIPs
$ i将决定何时发送成功消息。
答案 1 :(得分:0)
You set $flg value. By using value alert once.
if(isset($_POST['btn_sub']))
{
$flg=0;
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());
$flg=1;
}
else
{
$flg=0;
exit;
}
}
if($flg==1)
{
echo "<script type='text/javascript'>alert('Submitted Successfully!')</script>";
}
else
{
echo "<script type='text/javascript'>alert('Already Exist!')</script>";
}
}