dougehe2022 2019-06-22 03:36
浏览 107

通过逐行从数据库中获取数据,使用PHP / MySQL生成QR代码

I am stuck here while generating QR Code using PHP and SQL. Basically I am generating code for students using their ids and other information while fetching data from database. But the problem is that when I click on submit button so that table data store in QR Code, then it only generate the QR Code of of only last one student.

I create submit button on each row so that when I click on submit button then submit the data of that particular row to QR image. But problem is that when I click on submit button of any row then it generate qr image of last student in table. For Example if 1 have 10 students in table, then it generate image of student whose id is 10 and at last row of table.

Code is available here:

<?php
    include('qrlib.php'); 
    function getUsernameFromEmail($student_id) {
        $find = '@';
        $pos = strpos($student_id, $find);
        $username = substr($student_id, 0, $pos);
        return $username;
    }
    if(isset($_POST['submit']) ) {
        $tempDir = 'temp/'; 
        $student_id = $_POST['student_id'];
        $fname =  $_POST['fname'];
        $filename = getUsernameFromEmail($student_id);

        $codeContents = 'mailto:'.$student_id.'?id='.urlencode($student_id).'&fname='.urlencode($fname); 
        QRcode::png($codeContents, $tempDir.''.$filename.'.png', QR_ECLEVEL_L, 5);
    }
?>

Form:

<form id="demo-form2" method="post"  data-parsley-validate class="form-horizontal form-label-left" > 

                                      <table id="datatable" class="table table-striped table-bordered">
                  <thead>
                    <tr>
                      <th>Student Id</th>
                      <th>First Name</th>
                      <th>Actions</th>

                    </tr>
                  </thead>
                      <tbody>
                        <?php
                            include("includes/connection2.php");

                            $sql = "SELECT * FROM student";
                            $result=mysql_query($sql); //rs.open sql,con

                        while ($row=mysql_fetch_array($result))
                        { ?><!--open of while -->
                        <tr>
                            <td><input type="text" value="<?php echo $row['student_id']; ?>" name="student_id" ></td>
                            <td><input type="text" value="<?php echo $row['fname']; ?>" name="fname" ></td>
                        <td class="center">
                                <div class="form-group">
                    <input type="submit" name="submit" class="btn btn-primary submitBtn" style="width:20em; margin:0;" />
                </div>
                            </td>
                            <td class="center">

                            </td>   
                        </tr>
                                                            <?php
                           } //close of while
                        ?>
                      </tbody>
                       <?php
        if(!isset($filename)){
            $filename = "author";
        }
        ?>
        <div class="qr-field">
            <h3>QR Code Result: </h3>
            <center>
                <div class="qrframe" style="border:2px solid black; width:210px; height:210px;">
                    <?php echo '<img src="temp/'. @$filename.'.png" style="width:200px; height:200px;"><br>'; ?>
                </div>
    <a class="btn btn-primary submitBtn" style="width:50px; margin:1px 0;" href="download.php?file=<?php echo $filename; ?>.png ">Download QR Code</a>
            </center>
        </div>
                </table>
                        </div>
                </div>
              </div>
            </div>
</div>
        </div>   
                </form>
  • 写回答

1条回答 默认 最新

  • douyan1903 2019-06-22 13:41
    关注

    You create one submit button for each row, but all of these buttons are inside the same form and therefore they all do the same: submit the entire form, with all of its inputs.

    You store the student_id for each student in its own input tag, so you have one for each student, but all inputs have the same name: "student_id".

    When the form is submitted (regardless of which specific submit button caused it to be submitted) - the data of all inputs is sent, but since they all have the same name - the last value overwrites all the rest. This is why you always get a result only for the last student.

    One way to solve this would be to have separate form for each student, but this will not work with the current table structure.

    Another option is to give each submit button a unique name, and then determine which student was submitted in the PHP side, according to the submit button's name.

    Another option is to not have a submit button per each student, but just one submit button at the bottom of the form. Then, add a radio button for each student with a unique value (probably the student_id). This way, submitting the form will send the student_id of the selected student to the server.

    Probably the "right" way to approach this would be to use JavaScript.

    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀