我有这样的功能。在我的程序中为凭证制作随机代码的功能:
<?php
function rcode() {
//rancangan kode
$fak = mysql_query("select * from master where master.npm = '$_SESSION[username]'"); // dapat dari inputan
while ($fakk = mysql_fetch_array($fak)) {
$_SESSION['id_fak'] = $fakk['id_fak'];
};
$fakultas = "0" . $_SESSION['id_fak'];
$jur = mysql_query("select id from master where master.npm = '$_SESSION[username]'"); // dapat dari inputan
while ($jurr = mysql_fetch_array($jur)) {
$_SESSION['id'] = $jurr['id'];
};
$jurusan = $_SESSION['id'];
$result = mysql_query("
SHOW TABLE STATUS LIKE 'konfirmasi'
");
$data = mysql_fetch_assoc($result);
$next_increment = $data['Auto_increment'];
//$next_increment = 99999;
if ($next_increment < 10) {
$voucher = $fakultas . "00" . $jurusan . "0" . "0" . $next_increment;
echo $voucher;
} else if ($next_increment < 100 and $next_increment >= 10) {
$voucher = $fakultas . "00" . $jurusan . "0" . $next_increment;
echo $voucher;
} else if ($next_increment < 1000 and $next_increment >= 100) {
$voucher = $fakultas . "00" . $jurusan . $next_increment;
echo $voucher;
} else if ($next_increment < 10000 and $next_increment >= 1000) {
$depan = substr($next_increment, 0, 1);
$sisa = substr($next_increment, 1, 3);
$voucher = $fakultas . "0" . $depan . $jurusan . $sisa;
echo $voucher;
} else if ($next_increment >= 10000) {
$puluhribuan = substr($next_increment, 0, 1);
$ribuan = substr($next_increment, 1, 1);
$sisa = substr($next_increment, 2, 3);
$voucher = $fakultas . $puluhribuan . $ribuan . $jurusan . $sisa;
echo $voucher;
}
//echo rand();
}
?>
所以,我尝试在我的sql上插入,但它应该可以工作。 这是代码:
<?php
rcode();
?>
<?php
if (isset($_POST['konfirmasi'])) {
$voucherr = rcode();
mysql_query("insert into konfirmasi(voucher) values('$voucherr')");
} else {
?>
<form action="tampilbarcode.php" action="POST">
<input type="hidden" name="username" value="<?php echo $_SESSION['username']; ?>" />
<input type="submit" name="konfirmasi" value="CETAK PDF">
</form>
<?php
}
?>
请帮助我,这是我的论文计划。
答案 0 :(得分:1)
您应该在return $voucher
函数的末尾执行echo $voucher
而不是rcode()
。
答案 1 :(得分:0)
简单......
echo $voucher;
不起作用,因为函数必须返回一些东西。所以为了获得回报,你必须使用return $ voucher;您将获得$ voucher
$voucherr = rcode();
然后您可以将该值发送到插入查询。