【发布时间】:2019-06-22 03:36:57
【问题描述】:
我在使用 PHP 和 SQL 生成二维码时被困在这里。基本上,我在从数据库中获取数据时使用他们的 id 和其他信息为学生生成代码。但问题是当我点击提交按钮时,表格数据存储在二维码中,然后它只生成最后一个学生的二维码。
我在每一行上创建提交按钮,以便当我点击提交按钮时,将该特定行的数据提交到 QR 图像。但问题是,当我单击任何行的提交按钮时,它会生成表中最后一个学生的 qr 图像。例如,如果 1 表中有 10 个学生,则生成 id 为 10 且位于表最后一行的学生的图像。
代码可在此处获得:
<?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 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) 在 PHP 端您尝试访问“student_id”。它来自哪里?,(2)您应该在发送之前正确缩进您的代码,(3)您应该创建重现您的问题的代码的最小版本。这将帮助您调试,如果您不成功 - 将帮助其他人帮助您。
-
我已经编辑了可以成功下载 QR 图像的代码,但是使用此代码我只能下载 id 位于末尾的学生的 QR 图像,即使我单击任何学生的提交按钮也是如此。如果您能解决我的问题并查看此代码。谢谢!
-
如果你真的能回答你提出的问题,那么人们帮助你会容易得多。您上面的所有评论都是重新陈述您的问题,我们已经知道了。