需要用于为特定行输入来自不同用户的列值的代码

时间:2022-03-30 01:34:31

Hey guys I am creating a table in mysql named result which will have 4 fields i.e.

嘿伙计们我在mysql中创建一个名为result的表,它将有4个字段,即

table result

name ,
subject1_score ,
sub2_score ,
sub3_score

Now what I am doing is giving seperate forms to 3 teachers for entering their respective subject scores. For example, *teacher_1* will see all roll numbers and will be required to put subject1_score and then submit. Similarly for other two teachers. So what code I have to use in php and mysql in order to put values in a row for particular student.

现在我正在做的是给3名教师提供单独的表格,以输入他们各自的科目分数。例如,* teacher_1 *将查看所有卷号,并且需要放置subject1_score然后提交。同样对于其他两位老师。那么我必须在php和mysql中使用什么代码才能为特定学生添加值。

2 个解决方案

#1


0  

i'm not sure what name stand for (student or teacher) and i'm assuming that there are 3 subjects and each teacher has to submit the respective score and you want all data to be set in one row like (roll_num, name, subject 1 score, subject 2 score, subject 3 score). if that's ture then this should work for you:

我不确定什么名称(学生或老师),我假设有3个科目,每个老师必须提交相应的分数,你希望所有数据都设置在一行,如(roll_num,name,主题1得分,主题2得分,主题3得分)。如果这是真的,那么这对你有用:

if (isset("name") && isset("roll_number)){

$name = $_GET["name"];
$roll_no = $_GET["roll_number"];

if (isset("subject1_score")){
$subject = "subject1_score";
$score = $_GET['subject1_score'];
}elseif(isset("subject2_score")){
$subject = "subject2_score";
$score = $_GET['subject2_score'];
}elseif(isset("subject3_score")){
$subject = "subject3_score";
$score = $_GET['subject3_score'];
}else{
$subject = "";
$score = "";
}


$con = mysql_connect("host","username","password");
if (!$con) die('Could not connect: ' . mysql_error());
mysql_select_db($con, dbname);

$query1 = "SELECT * FROM result WHERE roll_number='$roll_no'";
$result1 = mysql_query($query1) or die(mysql_error());
$num = mysql_num_rows($result);

if($num == 0 && $subject != ""){
$query2 = "INSERT INTO result(roll_number, name, $subject) VALUES('$roll_no', '$name', '$score')";
mysql_query($query2) or die(mysql_error());
}elseif($num > 0 && $subject != ""){
$query2 = "UPDATE result SET $subject='$score' WHERE roll_number='$roll_no'"
}else{
echo "subject score is empty";
}
mysql_close($con);

#2


0  

There are two approaches, first you either fill the table with student names/roll_nos or second, you keep it blank. But please keep a field for roll numbers as they are unique and can serve you as primary keys.

有两种方法,首先你要么用学生名字/ roll_nos填充表格,要么第二,你把它留空。但请保留一个滚动编号字段,因为它们是唯一的,可以作为主键。

In the first approach, you just have to edit the particular row for the particular roll no for whom the marks have been entered. For this you just need to run mysql's update query, like,

在第一种方法中,您只需编辑已输入标记的特定卷的特定行。为此你只需要运行mysql的更新查询,比如

mysql_query("UPDATE table_name SET subject1_score='$subject!_score' WHERE roll_no='$roll_no'");

and so on. In here you have to fetch the roll no and marks from the form through PHP.

等等。在这里,您必须通过PHP从表单中获取roll no和marks。

In the second approach, you have to first check if the roll no is present in the database table by running a SELECT query and counting the no of rows by `mysql_num_rows'. If the no of rows is more than 0, then you have to update the roll_no by running the update query. If the no of rows returned are 0, you need to insert the date by running the insert query.

在第二种方法中,您必须首先通过运行SELECT查询并通过`mysql_num_rows'计算行数来检查数据库表中是否存在roll no。如果行数不超过0,则必须通过运行更新查询来更新roll_no。如果返回的行数为0,则需要通过运行insert查询来插入日期。

Hope this helps!

希望这可以帮助!

#1


0  

i'm not sure what name stand for (student or teacher) and i'm assuming that there are 3 subjects and each teacher has to submit the respective score and you want all data to be set in one row like (roll_num, name, subject 1 score, subject 2 score, subject 3 score). if that's ture then this should work for you:

我不确定什么名称(学生或老师),我假设有3个科目,每个老师必须提交相应的分数,你希望所有数据都设置在一行,如(roll_num,name,主题1得分,主题2得分,主题3得分)。如果这是真的,那么这对你有用:

if (isset("name") && isset("roll_number)){

$name = $_GET["name"];
$roll_no = $_GET["roll_number"];

if (isset("subject1_score")){
$subject = "subject1_score";
$score = $_GET['subject1_score'];
}elseif(isset("subject2_score")){
$subject = "subject2_score";
$score = $_GET['subject2_score'];
}elseif(isset("subject3_score")){
$subject = "subject3_score";
$score = $_GET['subject3_score'];
}else{
$subject = "";
$score = "";
}


$con = mysql_connect("host","username","password");
if (!$con) die('Could not connect: ' . mysql_error());
mysql_select_db($con, dbname);

$query1 = "SELECT * FROM result WHERE roll_number='$roll_no'";
$result1 = mysql_query($query1) or die(mysql_error());
$num = mysql_num_rows($result);

if($num == 0 && $subject != ""){
$query2 = "INSERT INTO result(roll_number, name, $subject) VALUES('$roll_no', '$name', '$score')";
mysql_query($query2) or die(mysql_error());
}elseif($num > 0 && $subject != ""){
$query2 = "UPDATE result SET $subject='$score' WHERE roll_number='$roll_no'"
}else{
echo "subject score is empty";
}
mysql_close($con);

#2


0  

There are two approaches, first you either fill the table with student names/roll_nos or second, you keep it blank. But please keep a field for roll numbers as they are unique and can serve you as primary keys.

有两种方法,首先你要么用学生名字/ roll_nos填充表格,要么第二,你把它留空。但请保留一个滚动编号字段,因为它们是唯一的,可以作为主键。

In the first approach, you just have to edit the particular row for the particular roll no for whom the marks have been entered. For this you just need to run mysql's update query, like,

在第一种方法中,您只需编辑已输入标记的特定卷的特定行。为此你只需要运行mysql的更新查询,比如

mysql_query("UPDATE table_name SET subject1_score='$subject!_score' WHERE roll_no='$roll_no'");

and so on. In here you have to fetch the roll no and marks from the form through PHP.

等等。在这里,您必须通过PHP从表单中获取roll no和marks。

In the second approach, you have to first check if the roll no is present in the database table by running a SELECT query and counting the no of rows by `mysql_num_rows'. If the no of rows is more than 0, then you have to update the roll_no by running the update query. If the no of rows returned are 0, you need to insert the date by running the insert query.

在第二种方法中,您必须首先通过运行SELECT查询并通过`mysql_num_rows'计算行数来检查数据库表中是否存在roll no。如果行数不超过0,则必须通过运行更新查询来更新roll_no。如果返回的行数为0,则需要通过运行insert查询来插入日期。

Hope this helps!

希望这可以帮助!