如何将数组输入存储到数据库中? MYSQL PHP表单输入foreach

时间:2022-04-26 15:41:43

In this project, I am making an attendance user interface for the teacher. Here teacher can update student's marks which will directly update into the MYSQL database. Below shows the code that I used to echo out the list of students from the database. Here, I have used array i.e. name="gradeEdit[]" in input section.

在这个项目中,我正在为老师制作一个考勤用户界面。在这里,老师可以更新学生的标记,这些标记将直接更新到MYSQL数据库中。下面显示了我用来回显数据库中学生列表的代码。这里,我在输入部分使用了数组,即name =“gradeEdit []”。

teacherGrades.php

<?php
$con=mysqli_connect("#", "#", "", "#");
if (mysqli_connect_errno())
 {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
 }

$result = mysqli_query ($con,"SELECT ");
echo ' <form method="POST" action="teacherGrades.php"> 
  <table class="tableEchoPupil" border="1">
<tr>
<th>Name</th>
<th>Edit</th>
</tr>';

while($row = mysqli_fetch_array($result))

{
echo "<tr>
<td>" . $row['name'] . "</td>
<td> <input type='text' name='gradeEdit[]' /> </td> 
</tr>"; }

echo "<input class='gradeSubmit' type='submit' name='btnSubmit' value='Submit'>"; 
echo "</table>"; 
echo "</form> <br/>"; 
 ?>

Below shows the code that is stored in the beginning of this page [teacherGrades.php ]. When the teacher inputs new grades and click the submit button from the previous code, the grades has to updated in the database. However the problem is that, I am not able to update it properly. I think there is a few problem behind my code, can you please check and help me. If there are further questions, I am ready to answer.

下面显示了存储在本页开头的代码[teacherGrades.php]。当教师输入新成绩并单击上一代码中的提交按钮时,必须在数据库中更新成绩。但问题是,我无法正确更新它。我认为我的代码背后有一些问题,请您检查并帮助我。如果还有其他问题,我准备回答。

Thank you in advance.

先谢谢你。

 <?php 
 $con=mysqli_connect("#","#","","#");
 if (mysqli_connect_errno()) {
   echo "Failed to connect to MySQL: " . mysqli_connect_error();
 }
 if ($_POST['btnSubmit']){

 $gradeEdit = $_POST['gradeEdit']; 

    foreach ($gradeEdit as $key => $value) {

      $grades = implode(',', $gradeEdit);

      $query = "UPDATE classroom_student_teacher SET marks = '$grades'
 WHERE teacher_id = $userid AND classroom_id = $id";

  $close = mysqli_query($con, $query); 
  }
  }
   ?> 

3 个解决方案

#1


1  

You should pass student name or id in your input box. Which means row-1_key => row-1_value.

您应该在输入框中传递学生姓名或ID。这意味着row-1_key => row-1_value。

<input type='text' name='gradeEdit[".$row['student_id']."]' />

the above line is for form input. Let me know Are you updating data for teacher or for all students. State me table structure.

上面的行是用于表单输入。让我知道您是否正在更新教师或所有学生的数据。陈述我的表结构。

If you want to save array value. use the below line

如果要保存数组值。使用以下行

foreach ($gradeEdit as $key => $value) {

//use $key as row id and marks will be your $value. So change your query like this below.

$row_id=$key+1;
$query = "UPDATE classroom_student_teacher SET marks = '$value'
WHERE student_id=$row_id AND teacher_id = $userid AND classroom_id = $id";

AND teacher_id = $userid AND classroom_id = $id why you using this condition

AND teacher_id = $ userid AND classroom_id = $ id您使用此条件的原因

$close = mysqli_query($con, $query); 
}

#2


0  

2 methods,

the json_encode is a possibility, but it might decode differently.

json_encode是一种可能性,但它可能以不同的方式解码。

one option which sounds better to me would be to use serialize and unserialize, this will return the objects just as you put them.

对我来说听起来更好的一个选项是使用序列化和反序列化,这将返回对象,就像你放置它们一样。

e.g.

serialize($var);
unserialize($var);

It wil just make a serialized string of your array or objects. In your case JSON could be fine too.

它只会创建一个数组或对象的序列化字符串。在你的情况下,JSON也可以。

http://php.net/manual/en/function.serialize.php

#3


0  

To store data in you database you can use php own functions.

要在您的数据库中存储数据,您可以使用php自己的函数。

$conn=mysqli_connect("example.com","testuser","passwort","your_db");
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql = "INSERT INTO Your_Table (First, Second, Array)";
$sql .= VALUES ('First', 'Second',Your_Array)";
mysqli_query($conn, $sql);
mysqli_close($con);

To put your array in the database you can user the php function implode.

要将数组放入数据库,您可以使用php函数implode。

$array = array('First', 'Second', 'Last');
$comma_separated = implode(",", $array);

Wich will return you a string like this - First, Second, Last

Wich会给你一个像这样的字符串 - First,Second,Last

#1


1  

You should pass student name or id in your input box. Which means row-1_key => row-1_value.

您应该在输入框中传递学生姓名或ID。这意味着row-1_key => row-1_value。

<input type='text' name='gradeEdit[".$row['student_id']."]' />

the above line is for form input. Let me know Are you updating data for teacher or for all students. State me table structure.

上面的行是用于表单输入。让我知道您是否正在更新教师或所有学生的数据。陈述我的表结构。

If you want to save array value. use the below line

如果要保存数组值。使用以下行

foreach ($gradeEdit as $key => $value) {

//use $key as row id and marks will be your $value. So change your query like this below.

$row_id=$key+1;
$query = "UPDATE classroom_student_teacher SET marks = '$value'
WHERE student_id=$row_id AND teacher_id = $userid AND classroom_id = $id";

AND teacher_id = $userid AND classroom_id = $id why you using this condition

AND teacher_id = $ userid AND classroom_id = $ id您使用此条件的原因

$close = mysqli_query($con, $query); 
}

#2


0  

2 methods,

the json_encode is a possibility, but it might decode differently.

json_encode是一种可能性,但它可能以不同的方式解码。

one option which sounds better to me would be to use serialize and unserialize, this will return the objects just as you put them.

对我来说听起来更好的一个选项是使用序列化和反序列化,这将返回对象,就像你放置它们一样。

e.g.

serialize($var);
unserialize($var);

It wil just make a serialized string of your array or objects. In your case JSON could be fine too.

它只会创建一个数组或对象的序列化字符串。在你的情况下,JSON也可以。

http://php.net/manual/en/function.serialize.php

#3


0  

To store data in you database you can use php own functions.

要在您的数据库中存储数据,您可以使用php自己的函数。

$conn=mysqli_connect("example.com","testuser","passwort","your_db");
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql = "INSERT INTO Your_Table (First, Second, Array)";
$sql .= VALUES ('First', 'Second',Your_Array)";
mysqli_query($conn, $sql);
mysqli_close($con);

To put your array in the database you can user the php function implode.

要将数组放入数据库,您可以使用php函数implode。

$array = array('First', 'Second', 'Last');
$comma_separated = implode(",", $array);

Wich will return you a string like this - First, Second, Last

Wich会给你一个像这样的字符串 - First,Second,Last