当输入名称类似于 name="fieldname[] 时,如何将值插入数据库?

发布于 2024-12-13 15:56:45 字数 1404 浏览 1 评论 0 原文

我有以下表单,我必须将表单值插入数据库。但当字段名称类似于 - name="fieldname[]" 时,我不知道如何插入这些值。为了更好地理解,请检查以下内容。

我正在使用 Codeigniter。我有视图、控制器和模型,我尝试了插入值的常用方法,但它不起作用。

我的一位朋友建议我使用类似 mysql_real_escape_string($value) 的东西,但我不知道如何使用它。请您帮帮我好吗?

提前致谢:)

   <table> 
     <form >

   <tr>

      <td> Student ID: <input type="text" name="studentid[]" value="" /> </td>
      <td> Grade <input type="text" name="grade[]" value="" /></td>
      <td>Obtained Mark  <input type="text" name="obtainedmark[]" value="" /></td>
    </tr>

    <tr>

     <td> Student ID: <input type="text" name="studentid[]" value="" /> </td>
     <td> Grade <input type="text" name="grade[]" value="" /></td>
     <td>Obtained Mark  <input type="text" name="obtainedmark[]" value="" /></td>

    </tr>


  <tr>
     <td> <input type="submit" value="Submit" name="Submit" /></td>
 </tr>

 </form>
 </table>

编辑

    foreach($_POST['studentid'] as $studentid) {
      // do something

    $data = array(              
    'subjectname' =>"",
    'subjectname' => $studentid             

    );                  
    $results=$this->db->insert('subject', $data);                      

I have the following form, I have to insert the form values into database. But I don't how to insert these values when the field names are like - name="fieldname[]". For better understanding please check the following.

I am using Codeigniter. I have Views, controller and model, I have tried the usual way of inserting value but its not working.

One of my friends suggested me to use something like- mysql_real_escape_string($value) but I don't know how to use it. Would you please kindly help me?

Thanks in Advance :)

   <table> 
     <form >

   <tr>

      <td> Student ID: <input type="text" name="studentid[]" value="" /> </td>
      <td> Grade <input type="text" name="grade[]" value="" /></td>
      <td>Obtained Mark  <input type="text" name="obtainedmark[]" value="" /></td>
    </tr>

    <tr>

     <td> Student ID: <input type="text" name="studentid[]" value="" /> </td>
     <td> Grade <input type="text" name="grade[]" value="" /></td>
     <td>Obtained Mark  <input type="text" name="obtainedmark[]" value="" /></td>

    </tr>


  <tr>
     <td> <input type="submit" value="Submit" name="Submit" /></td>
 </tr>

 </form>
 </table>

Edit

    foreach($_POST['studentid'] as $studentid) {
      // do something

    $data = array(              
    'subjectname' =>"",
    'subjectname' => $studentid             

    );                  
    $results=$this->db->insert('subject', $data);                      

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

只是我以为 2024-12-20 15:56:45

其工作方式是为具有该名称的每个字段获取一个值数组。你说有两个

   <td> Student ID: <input type="text" name="studentid[]" value="" /> </td>

,当你这样做时,

 foreach($_POST['studentid'] as $studentid) {

  $data = array(              
  'subjectid' =>"",
  'subjectname' => $studentid             

   );                  
 $results=$this->db->insert('subject', $data);

你输入两个学生 ID。您需要再次执行其他输入。但是等等?如果没有学生姓名,您将如何构建 sql 查询?我可能会做类似

$studentids = addslashes(htmlentities($_POST['studentid']));
$subjectnames = addslashes(htmlentities($_POST['subjectnames']));
$obtainedmarks = addslashes(htmlentities($_POST['obtainedmarks']));

for($i = 0; $i < count($studentids); $i++){
 $data = array(              
  'subjectid' =>$studentids[$i],
  'subjectname' => $subjectnames[$i],             
'obtainedmarks' => $obtainedmarks[$i] 
   );                  
 $results=$this->db->insert('subject', $data);
}

让我知道这是否有意义的事情。

The way this works is you are getting an array of values for each field that has that name. You say there are two of these

   <td> Student ID: <input type="text" name="studentid[]" value="" /> </td>

and when you do

 foreach($_POST['studentid'] as $studentid) {

  $data = array(              
  'subjectid' =>"",
  'subjectname' => $studentid             

   );                  
 $results=$this->db->insert('subject', $data);

you input two student ids. You need to do it again your other inputs. But wait? How will you construct your sql query without the student names? I would probably do something like

$studentids = addslashes(htmlentities($_POST['studentid']));
$subjectnames = addslashes(htmlentities($_POST['subjectnames']));
$obtainedmarks = addslashes(htmlentities($_POST['obtainedmarks']));

for($i = 0; $i < count($studentids); $i++){
 $data = array(              
  'subjectid' =>$studentids[$i],
  'subjectname' => $subjectnames[$i],             
'obtainedmarks' => $obtainedmarks[$i] 
   );                  
 $results=$this->db->insert('subject', $data);
}

Let me know if that makes sense.

定格我的天空 2024-12-20 15:56:45

这些是数组,您可以通过以下代码(示例)访问它们:

foreach($_POST['studentid'] as $studentid) {
  // do something
}

进行编辑。这只是一个例子,我不知道这是否有效。尝试一下吧。

foreach($_POST as $key=>$data) {
    foreach($data as $field) {
        $arr[$key][] = $field;
    }
}

These are Arrays, you can access them by the following code (example):

foreach($_POST['studentid'] as $studentid) {
  // do something
}

for your edit. this is just en example, idk whether this works or not. Just try around with it.

foreach($_POST as $key=>$data) {
    foreach($data as $field) {
        $arr[$key][] = $field;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文