使用数组if else语句的反复制表单

时间:2017-05-12 07:16:04

标签: php mysql arrays if-statement

我有类似RNG滚动表的代码,因此当您输入用户名时,它将发送在页面加载时生成的用户数据和秘密隐藏号码(rng)。

但是我有一个问题,在30-35座位上滚动时,RNG的数量有时会重复并且耗时。

我尝试用if-else语句和带有array_push函数的数组解决问题,让已经输入的数字存储在数组中,然后用if语句检查数字是否在数组上,如果存储的话它必须返回“重新”“随机数”,否则继续进行。

这是我的索引代码:

<!DOCTYPE html>
<html>
<head>
    <title>Simple CRUD by TUTORIALWEB.NET</title>
    <style>
    .kursi{
        width: 70px;
        height: 50px;
        background-color: red;
        padding: 5px 5px 5px 5px;
    }

    .kotakkursi{
        width: 80px;
        height: 60px;
        padding: 10px 2px 10px 2px;
        float: right;
    }

    .kotakkursigenap{
        width: 80px;
        height: 60px;
        padding: 10px 2px 10px 2px;
        float: left;
    }

    .kotakduakursi{
        width: 168px;
        height: 80px;
        padding: 10px 50px 10px 50px;
        float: right;
    }

    .kontbesar{
        width: 840px;
        height: 400px;
    }
    </style>
</head>
<body>
<div class="kontbesar">
        <?php
        function rng(){
            $x = rand(1,35);
        }

        //iclude file koneksi ke database
        include('koneksi.php');

        //query ke database dg SELECT table siswa diurutkan berdasarkan NIS paling besar
        $query = mysql_query("SELECT * FROM siswa ORDER BY siswa_nomer") or die(mysql_error());

        if(mysql_num_rows($query) == 0){
            echo '<tr><td colspan="6">Tidak ada data!</td></tr>';
        }

        else{
            $no = 1;
            while($data = mysql_fetch_assoc($query)){
                $siswa_nomer = $data['siswa_nomer'];
                if($siswa_nomer < "11"){
                    echo "          
                    <div class='kotakkursi'>
                    <div class='kursi'>"
                    .$data['siswa_nama'].
                    "<br>"
                    .$data['siswa_nomer'].
                    "</div>
                    </div>";
                }
                elseif($siswa_nomer > "10" && $siswa_nomer < "21"){
                    echo "
                    <div class='kotakkursigenap'>
                    <div class='kursi'>"
                    .$data['siswa_nama'].
                    "<br>"
                    .$data['siswa_nomer'].
                    "</div>
                    </div>";
                }

                elseif($siswa_nomer > "20" && $siswa_nomer < "30"){
                    echo "
                    <div class='kotakkursi'>
                    <div class='kursi'>"
                    .$data['siswa_nama'].
                    "<br>"
                    .$data['siswa_nomer'].
                    "</div>
                    </div>";
                }

                elseif($siswa_nomer > "30" ){
                    echo "
                    <div class='kotakkursigenap'>
                    <div class='kursi'>"
                    .$data['siswa_nama'].
                    "<br>"
                    .$data['siswa_nomer'].
                    "</div>
                    </div>";
                }

                }
                $no++;
            }
        ?>

        </div>
        <form action="tambah-proses.php" method="post">
                <input type="hidden" name="nomer" required value="<?PHP echo $rng; ?>"><br>
                Nama: <input type="text" name="nama" size="30" required>
                <input type="submit" name="tambah" value="Tambah" onclick="rng()">
    </form>

</body>
</html>

这是我处理表单操作的文件(抱歉有点长,因为我不会手动愚蠢if-else直到35):

<HTML>
<body onload="gohome()">
<?php
//mulai proses tambah data

//cek dahulu, jika tombol tambah di klik
if(isset($_POST['tambah'])){
    include('koneksi.php');
    $nama   = $_POST['nama'];
    $nomer  = $_POST['nomer'];

    $input = mysql_query("INSERT INTO siswa VALUES('$nama', '$nomer')") or die(mysql_error());

    $arr = array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);

    $rng = rand(1, 35);
    function rng(){
    if($nomer != $arr[0] && $arr[0] = 0){
    if($input){

    $query = mysql_query($con, "SELECT * FROM siswa WHERE nomer='".$nomer."'");
    if(mysql_num_rows($query) > 0){
    echo "roll ulang already exists";
    }

    else{

    mysql_query("INSERT INTO siswa VALUES('$nama', '$nomer')") or die(mysql_error());
    if (!mysql_query($con,$sql)){
    die('Error: ' . mysql_error($con));
    }
    }
    }

    else{

    echo 'Gagal menambahkan data! ';    //Pesan jika proses tambah gagal
    echo 'Kembali'; //membuat Link untuk kembali ke halaman tambah

    }
    array_push($arr, $nomer);
    }
    ##########################################################################


    elseif($nomer != $arr[1] && $arr[1] = 0){
    if($input){

    $query = mysql_query($con, "SELECT * FROM siswa WHERE nomer='".$nomer."'");
    if(mysql_num_rows($query) > 0){
    echo "roll ulang already exists";
    }

    else{

    mysql_query("INSERT INTO siswa VALUES('$nama', '$nomer')") or die(mysql_error());
    if (!mysql_query($con,$sql)){
    die('Error: ' . mysql_error($con));
    }
    }
    }

    else{

    echo 'Gagal menambahkan data! ';    //Pesan jika proses tambah gagal
    echo 'Kembali'; //membuat Link untuk kembali ke halaman tambah

    }
    array_push($arr, $nomer);
    }
    ##########################################################################


    elseif($nomer != $arr[2] && $arr[2] = 0){
    if($input){

    $query = mysql_query($con, "SELECT * FROM siswa WHERE nomer='".$nomer."'");
    if(mysql_num_rows($query) > 0){
    echo "roll ulang already exists";
    }

    else{

    mysql_query("INSERT INTO siswa VALUES('$nama', '$nomer')") or die(mysql_error());
    if (!mysql_query($con,$sql)){
    die('Error: ' . mysql_error($con));
    }
    }
    }

    else{

    echo 'Gagal menambahkan data! ';    //Pesan jika proses tambah gagal
    echo 'Kembali'; //membuat Link untuk kembali ke halaman tambah

    }
    array_push($arr, $nomer);
    }
    ##########################################################################


    elseif($nomer != $arr[3] && $arr[3] = 0){
    if($input){

    $query = mysql_query($con, "SELECT * FROM siswa WHERE nomer='".$nomer."'");
    if(mysql_num_rows($query) > 0){
    echo "roll ulang already exists";
    }

    else{

    mysql_query("INSERT INTO siswa VALUES('$nama', '$nomer')") or die(mysql_error());
    if (!mysql_query($con,$sql)){
    die('Error: ' . mysql_error($con));
    }
    }
    }

    else{

    echo 'Gagal menambahkan data! ';    //Pesan jika proses tambah gagal
    echo 'Kembali'; //membuat Link untuk kembali ke halaman tambah

    }
    array_push($arr, $nomer);
    }
    ##########################################################################


    elseif($nomer != $arr[4] && $arr[4] = 0){
    if($input){

    $query = mysql_query($con, "SELECT * FROM siswa WHERE nomer='".$nomer."'");
    if(mysql_num_rows($query) > 0){
    echo "roll ulang already exists";
    }

    else{

    mysql_query("INSERT INTO siswa VALUES('$nama', '$nomer')") or die(mysql_error());
    if (!mysql_query($con,$sql)){
    die('Error: ' . mysql_error($con));
    }
    }
    }

    else{

    echo 'Gagal menambahkan data! ';    //Pesan jika proses tambah gagal
    echo 'Kembali'; //membuat Link untuk kembali ke halaman tambah

    }
    array_push($arr, $nomer);
    }
    ##########################################################################


    elseif($nomer != $arr[5] && $arr[5] = 0){
    if($input){

    $query = mysql_query($con, "SELECT * FROM siswa WHERE nomer='".$nomer."'");
    if(mysql_num_rows($query) > 0){
    echo "roll ulang already exists";
    }

    else{

    mysql_query("INSERT INTO siswa VALUES('$nama', '$nomer')") or die(mysql_error());
    if (!mysql_query($con,$sql)){
    die('Error: ' . mysql_error($con));
    }
    }
    }

    else{

    echo 'Gagal menambahkan data! ';    //Pesan jika proses tambah gagal
    echo 'Kembali'; //membuat Link untuk kembali ke halaman tambah

    }
    array_push($arr, $nomer);
    }
    ##########################################################################


    elseif($nomer != $arr[6] && $arr[6] = 0){
    if($input){

    $query = mysql_query($con, "SELECT * FROM siswa WHERE nomer='".$nomer."'");
    if(mysql_num_rows($query) > 0){
    echo "roll ulang already exists";
    }

    else{

    mysql_query("INSERT INTO siswa VALUES('$nama', '$nomer')") or die(mysql_error());
    if (!mysql_query($con,$sql)){
    die('Error: ' . mysql_error($con));
    }
    }
    }

    else{

    echo 'Gagal menambahkan data! ';    //Pesan jika proses tambah gagal
    echo 'Kembali'; //membuat Link untuk kembali ke halaman tambah

    }
    array_push($arr, $nomer);
    }
    ##########################################################################


    elseif($nomer != $arr[7] && $arr[7] = 0){
    if($input){

    $query = mysql_query($con, "SELECT * FROM siswa WHERE nomer='".$nomer."'");
    if(mysql_num_rows($query) > 0){
    echo "roll ulang already exists";
    }

    else{

    mysql_query("INSERT INTO siswa VALUES('$nama', '$nomer')") or die(mysql_error());
    if (!mysql_query($con,$sql)){
    die('Error: ' . mysql_error($con));
    }
    }
    }

    else{

    echo 'Gagal menambahkan data! ';    //Pesan jika proses tambah gagal
    echo 'Kembali'; //membuat Link untuk kembali ke halaman tambah

    }
    array_push($arr, $nomer);
    }
    ##########################################################################


    elseif($nomer != $arr[8] && $arr[8] = 0){
    if($input){

    $query = mysql_query($con, "SELECT * FROM siswa WHERE nomer='".$nomer."'");
    if(mysql_num_rows($query) > 0){
    echo "roll ulang already exists";
    }

    else{

    mysql_query("INSERT INTO siswa VALUES('$nama', '$nomer')") or die(mysql_error());
    if (!mysql_query($con,$sql)){
    die('Error: ' . mysql_error($con));
    }
    }
    }

    else{

    echo 'Gagal menambahkan data! ';    //Pesan jika proses tambah gagal
    echo 'Kembali'; //membuat Link untuk kembali ke halaman tambah

    }
    array_push($arr, $nomer);
    }
    ##########################################################################


    elseif($nomer != $arr[9] && $arr[9] = 0){
    if($input){

    $query = mysql_query($con, "SELECT * FROM siswa WHERE nomer='".$nomer."'");
    if(mysql_num_rows($query) > 0){
    echo "roll ulang already exists";
    }

    else{

    mysql_query("INSERT INTO siswa VALUES('$nama', '$nomer')") or die(mysql_error());
    if (!mysql_query($con,$sql)){
    die('Error: ' . mysql_error($con));
    }
    }
    }

    else{

    echo 'Gagal menambahkan data! ';    //Pesan jika proses tambah gagal
    echo 'Kembali'; //membuat Link untuk kembali ke halaman tambah

    }
    array_push($arr, $nomer);
    }
    ##########################################################################


    elseif($nomer != $arr[10] && $arr[10] = 0){
    if($input){

    $query = mysql_query($con, "SELECT * FROM siswa WHERE nomer='".$nomer."'");
    if(mysql_num_rows($query) > 0){
    echo "roll ulang already exists";
    }

    else{

    mysql_query("INSERT INTO siswa VALUES('$nama', '$nomer')") or die(mysql_error());
    if (!mysql_query($con,$sql)){
    die('Error: ' . mysql_error($con));
    }
    }
    }

    else{

    echo 'Gagal menambahkan data! ';    //Pesan jika proses tambah gagal
    echo 'Kembali'; //membuat Link untuk kembali ke halaman tambah

    }
    array_push($arr, $nomer);
    }
    ##########################################################################


    elseif($nomer != $arr[11] && $arr[11] = 0){
    if($input){

    $query = mysql_query($con, "SELECT * FROM siswa WHERE nomer='".$nomer."'");
    if(mysql_num_rows($query) > 0){
    echo "roll ulang already exists";
    }

    else{

    mysql_query("INSERT INTO siswa VALUES('$nama', '$nomer')") or die(mysql_error());
    if (!mysql_query($con,$sql)){
    die('Error: ' . mysql_error($con));
    }
    }
    }

    else{

    echo 'Gagal menambahkan data! ';    //Pesan jika proses tambah gagal
    echo 'Kembali'; //membuat Link untuk kembali ke halaman tambah

    }
    array_push($arr, $nomer);
    }
    ##########################################################################


    elseif($nomer != $arr[12] && $arr[12] = 0){
    if($input){

    $query = mysql_query($con, "SELECT * FROM siswa WHERE nomer='".$nomer."'");
    if(mysql_num_rows($query) > 0){
    echo "roll ulang already exists";
    }

    else{

    mysql_query("INSERT INTO siswa VALUES('$nama', '$nomer')") or die(mysql_error());
    if (!mysql_query($con,$sql)){
    die('Error: ' . mysql_error($con));
    }
    }
    }

    else{

    echo 'Gagal menambahkan data! ';    //Pesan jika proses tambah gagal
    echo 'Kembali'; //membuat Link untuk kembali ke halaman tambah

    }
    array_push($arr, $nomer);
    }
    ##########################################################################


    elseif($nomer != $arr[13] && $arr[13] = 0){
    if($input){

    $query = mysql_query($con, "SELECT * FROM siswa WHERE nomer='".$nomer."'");
    if(mysql_num_rows($query) > 0){
    echo "roll ulang already exists";
    }

    else{

    mysql_query("INSERT INTO siswa VALUES('$nama', '$nomer')") or die(mysql_error());
    if (!mysql_query($con,$sql)){
    die('Error: ' . mysql_error($con));
    }
    }
    }

    else{

    echo 'Gagal menambahkan data! ';    //Pesan jika proses tambah gagal
    echo 'Kembali'; //membuat Link untuk kembali ke halaman tambah

    }
    array_push($arr, $nomer);
    }
    ##########################################################################


    elseif($nomer != $arr[14] && $arr[14] = 0){
    if($input){

    $query = mysql_query($con, "SELECT * FROM siswa WHERE nomer='".$nomer."'");
    if(mysql_num_rows($query) > 0){
    echo "roll ulang already exists";
    }

    else{

    mysql_query("INSERT INTO siswa VALUES('$nama', '$nomer')") or die(mysql_error());
    if (!mysql_query($con,$sql)){
    die('Error: ' . mysql_error($con));
    }
    }
    }

    else{

    echo 'Gagal menambahkan data! ';    //Pesan jika proses tambah gagal
    echo 'Kembali'; //membuat Link untuk kembali ke halaman tambah

    }
    array_push($arr, $nomer);
    }
    ##########################################################################


    elseif($nomer != $arr[15] && $arr[15] = 0){
    if($input){

    $query = mysql_query($con, "SELECT * FROM siswa WHERE nomer='".$nomer."'");
    if(mysql_num_rows($query) > 0){
    echo "roll ulang already exists";
    }

    else{

    mysql_query("INSERT INTO siswa VALUES('$nama', '$nomer')") or die(mysql_error());
    if (!mysql_query($con,$sql)){
    die('Error: ' . mysql_error($con));
    }
    }
    }

    else{

    echo 'Gagal menambahkan data! ';    //Pesan jika proses tambah gagal
    echo 'Kembali'; //membuat Link untuk kembali ke halaman tambah

    }
    array_push($arr, $nomer);
    }
    ##########################################################################


    elseif($nomer != $arr[16] && $arr[16] = 0){
    if($input){

    $query = mysql_query($con, "SELECT * FROM siswa WHERE nomer='".$nomer."'");
    if(mysql_num_rows($query) > 0){
    echo "roll ulang already exists";
    }

    else{

    mysql_query("INSERT INTO siswa VALUES('$nama', '$nomer')") or die(mysql_error());
    if (!mysql_query($con,$sql)){
    die('Error: ' . mysql_error($con));
    }
    }
    }

    else{

    echo 'Gagal menambahkan data! ';    //Pesan jika proses tambah gagal
    echo 'Kembali'; //membuat Link untuk kembali ke halaman tambah

    }
    array_push($arr, $nomer);
    }
    ##########################################################################


    elseif($nomer != $arr[17] && $arr[17] = 0){
    if($input){

    $query = mysql_query($con, "SELECT * FROM siswa WHERE nomer='".$nomer."'");
    if(mysql_num_rows($query) > 0){
    echo "roll ulang already exists";
    }

    else{

    mysql_query("INSERT INTO siswa VALUES('$nama', '$nomer')") or die(mysql_error());
    if (!mysql_query($con,$sql)){
    die('Error: ' . mysql_error($con));
    }
    }
    }

    else{

    echo 'Gagal menambahkan data! ';    //Pesan jika proses tambah gagal
    echo 'Kembali'; //membuat Link untuk kembali ke halaman tambah

    }
    array_push($arr, $nomer);
    }
    ##########################################################################


    elseif($nomer != $arr[18] && $arr[18] = 0){
    if($input){

    $query = mysql_query($con, "SELECT * FROM siswa WHERE nomer='".$nomer."'");
    if(mysql_num_rows($query) > 0){
    echo "roll ulang already exists";
    }

    else{

    mysql_query("INSERT INTO siswa VALUES('$nama', '$nomer')") or die(mysql_error());
    if (!mysql_query($con,$sql)){
    die('Error: ' . mysql_error($con));
    }
    }
    }

    else{

    echo 'Gagal menambahkan data! ';    //Pesan jika proses tambah gagal
    echo 'Kembali'; //membuat Link untuk kembali ke halaman tambah

    }
    array_push($arr, $nomer);
    }
    ##########################################################################


    elseif($nomer != $arr[19] && $arr[19] = 0){
    if($input){

    $query = mysql_query($con, "SELECT * FROM siswa WHERE nomer='".$nomer."'");
    if(mysql_num_rows($query) > 0){
    echo "roll ulang already exists";
    }

    else{

    mysql_query("INSERT INTO siswa VALUES('$nama', '$nomer')") or die(mysql_error());
    if (!mysql_query($con,$sql)){
    die('Error: ' . mysql_error($con));
    }
    }
    }

    else{

    echo 'Gagal menambahkan data! ';    //Pesan jika proses tambah gagal
    echo 'Kembali'; //membuat Link untuk kembali ke halaman tambah

    }
    array_push($arr, $nomer);
    }
    ##########################################################################


    elseif($nomer != $arr[20] && $arr[20] = 0){
    if($input){

    $query = mysql_query($con, "SELECT * FROM siswa WHERE nomer='".$nomer."'");
    if(mysql_num_rows($query) > 0){
    echo "roll ulang already exists";
    }

    else{

    mysql_query("INSERT INTO siswa VALUES('$nama', '$nomer')") or die(mysql_error());
    if (!mysql_query($con,$sql)){
    die('Error: ' . mysql_error($con));
    }
    }
    }

    else{

    echo 'Gagal menambahkan data! ';    //Pesan jika proses tambah gagal
    echo 'Kembali'; //membuat Link untuk kembali ke halaman tambah

    }
    array_push($arr, $nomer);
    }
    ##########################################################################


    elseif($nomer != $arr[21] && $arr[21] = 0){
    if($input){

    $query = mysql_query($con, "SELECT * FROM siswa WHERE nomer='".$nomer."'");
    if(mysql_num_rows($query) > 0){
    echo "roll ulang already exists";
    }

    else{

    mysql_query("INSERT INTO siswa VALUES('$nama', '$nomer')") or die(mysql_error());
    if (!mysql_query($con,$sql)){
    die('Error: ' . mysql_error($con));
    }
    }
    }

    else{

    echo 'Gagal menambahkan data! ';    //Pesan jika proses tambah gagal
    echo 'Kembali'; //membuat Link untuk kembali ke halaman tambah

    }
    array_push($arr, $nomer);
    }
    ##########################################################################


    elseif($nomer != $arr[22] && $arr[22] = 0){
    if($input){

    $query = mysql_query($con, "SELECT * FROM siswa WHERE nomer='".$nomer."'");
    if(mysql_num_rows($query) > 0){
    echo "roll ulang already exists";
    }

    else{

    mysql_query("INSERT INTO siswa VALUES('$nama', '$nomer')") or die(mysql_error());
    if (!mysql_query($con,$sql)){
    die('Error: ' . mysql_error($con));
    }
    }
    }

    else{

    echo 'Gagal menambahkan data! ';    //Pesan jika proses tambah gagal
    echo 'Kembali'; //membuat Link untuk kembali ke halaman tambah

    }
    array_push($arr, $nomer);
    }
    ##########################################################################


    elseif($nomer != $arr[23] && $arr[23] = 0){
    if($input){

    $query = mysql_query($con, "SELECT * FROM siswa WHERE nomer='".$nomer."'");
    if(mysql_num_rows($query) > 0){
    echo "roll ulang already exists";
    }

    else{

    mysql_query("INSERT INTO siswa VALUES('$nama', '$nomer')") or die(mysql_error());
    if (!mysql_query($con,$sql)){
    die('Error: ' . mysql_error($con));
    }
    }
    }

    else{

    echo 'Gagal menambahkan data! ';    //Pesan jika proses tambah gagal
    echo 'Kembali'; //membuat Link untuk kembali ke halaman tambah

    }
    array_push($arr, $nomer);
    }
    ##########################################################################


    elseif($nomer != $arr[24] && $arr[24] = 0){
    if($input){

    $query = mysql_query($con, "SELECT * FROM siswa WHERE nomer='".$nomer."'");
    if(mysql_num_rows($query) > 0){
    echo "roll ulang already exists";
    }

    else{

    mysql_query("INSERT INTO siswa VALUES('$nama', '$nomer')") or die(mysql_error());
    if (!mysql_query($con,$sql)){
    die('Error: ' . mysql_error($con));
    }
    }
    }

    else{

    echo 'Gagal menambahkan data! ';    //Pesan jika proses tambah gagal
    echo 'Kembali'; //membuat Link untuk kembali ke halaman tambah

    }
    array_push($arr, $nomer);
    }
    ##########################################################################


    elseif($nomer != $arr[25] && $arr[25] = 0){
    if($input){

    $query = mysql_query($con, "SELECT * FROM siswa WHERE nomer='".$nomer."'");
    if(mysql_num_rows($query) > 0){
    echo "roll ulang already exists";
    }

    else{

    mysql_query("INSERT INTO siswa VALUES('$nama', '$nomer')") or die(mysql_error());
    if (!mysql_query($con,$sql)){
    die('Error: ' . mysql_error($con));
    }
    }
    }

    else{

    echo 'Gagal menambahkan data! ';    //Pesan jika proses tambah gagal
    echo 'Kembali'; //membuat Link untuk kembali ke halaman tambah

    }
    array_push($arr, $nomer);
    }
    ##########################################################################


    elseif($nomer != $arr[26] && $arr[26] = 0){
    if($input){

    $query = mysql_query($con, "SELECT * FROM siswa WHERE nomer='".$nomer."'");
    if(mysql_num_rows($query) > 0){
    echo "roll ulang already exists";
    }

    else{

    mysql_query("INSERT INTO siswa VALUES('$nama', '$nomer')") or die(mysql_error());
    if (!mysql_query($con,$sql)){
    die('Error: ' . mysql_error($con));
    }
    }
    }

    else{

    echo 'Gagal menambahkan data! ';    //Pesan jika proses tambah gagal
    echo 'Kembali'; //membuat Link untuk kembali ke halaman tambah

    }
    array_push($arr, $nomer);
    }
    ##########################################################################


    elseif($nomer != $arr[27] && $arr[27] = 0){
    if($input){

    $query = mysql_query($con, "SELECT * FROM siswa WHERE nomer='".$nomer."'");
    if(mysql_num_rows($query) > 0){
    echo "roll ulang already exists";
    }

    else{

    mysql_query("INSERT INTO siswa VALUES('$nama', '$nomer')") or die(mysql_error());
    if (!mysql_query($con,$sql)){
    die('Error: ' . mysql_error($con));
    }
    }
    }

    else{

    echo 'Gagal menambahkan data! ';    //Pesan jika proses tambah gagal
    echo 'Kembali'; //membuat Link untuk kembali ke halaman tambah

    }
    array_push($arr, $nomer);
    }
    ##########################################################################


    elseif($nomer != $arr[28] && $arr[28] = 0){
    if($input){

    $query = mysql_query($con, "SELECT * FROM siswa WHERE nomer='".$nomer."'");
    if(mysql_num_rows($query) > 0){
    echo "roll ulang already exists";
    }

    else{

    mysql_query("INSERT INTO siswa VALUES('$nama', '$nomer')") or die(mysql_error());
    if (!mysql_query($con,$sql)){
    die('Error: ' . mysql_error($con));
    }
    }
    }

    else{

    echo 'Gagal menambahkan data! ';    //Pesan jika proses tambah gagal
    echo 'Kembali'; //membuat Link untuk kembali ke halaman tambah

    }
    array_push($arr, $nomer);
    }
    ##########################################################################


    elseif($nomer != $arr[29] && $arr[29] = 0){
    if($input){

    $query = mysql_query($con, "SELECT * FROM siswa WHERE nomer='".$nomer."'");
    if(mysql_num_rows($query) > 0){
    echo "roll ulang already exists";
    }

    else{

    mysql_query("INSERT INTO siswa VALUES('$nama', '$nomer')") or die(mysql_error());
    if (!mysql_query($con,$sql)){
    die('Error: ' . mysql_error($con));
    }
    }
    }

    else{

    echo 'Gagal menambahkan data! ';    //Pesan jika proses tambah gagal
    echo 'Kembali'; //membuat Link untuk kembali ke halaman tambah

    }
    array_push($arr, $nomer);
    }
    ##########################################################################


    elseif($nomer != $arr[30] && $arr[30] = 0){
    if($input){

    $query = mysql_query($con, "SELECT * FROM siswa WHERE nomer='".$nomer."'");
    if(mysql_num_rows($query) > 0){
    echo "roll ulang already exists";
    }

    else{

    mysql_query("INSERT INTO siswa VALUES('$nama', '$nomer')") or die(mysql_error());
    if (!mysql_query($con,$sql)){
    die('Error: ' . mysql_error($con));
    }
    }
    }

    else{

    echo 'Gagal menambahkan data! ';    //Pesan jika proses tambah gagal
    echo 'Kembali'; //membuat Link untuk kembali ke halaman tambah

    }
    array_push($arr, $nomer);
    }
    ##########################################################################


    elseif($nomer != $arr[31] && $arr[31] = 0){
    if($input){

    $query = mysql_query($con, "SELECT * FROM siswa WHERE nomer='".$nomer."'");
    if(mysql_num_rows($query) > 0){
    echo "roll ulang already exists";
    }

    else{

    mysql_query("INSERT INTO siswa VALUES('$nama', '$nomer')") or die(mysql_error());
    if (!mysql_query($con,$sql)){
    die('Error: ' . mysql_error($con));
    }
    }
    }

    else{

    echo 'Gagal menambahkan data! ';    //Pesan jika proses tambah gagal
    echo 'Kembali'; //membuat Link untuk kembali ke halaman tambah

    }
    array_push($arr, $nomer);
    }
    ##########################################################################


    elseif($nomer != $arr[32] && $arr[32] = 0){
    if($input){

    $query = mysql_query($con, "SELECT * FROM siswa WHERE nomer='".$nomer."'");
    if(mysql_num_rows($query) > 0){
    echo "roll ulang already exists";
    }

    else{

    mysql_query("INSERT INTO siswa VALUES('$nama', '$nomer')") or die(mysql_error());
    if (!mysql_query($con,$sql)){
    die('Error: ' . mysql_error($con));
    }
    }
    }

    else{

    echo 'Gagal menambahkan data! ';    //Pesan jika proses tambah gagal
    echo 'Kembali'; //membuat Link untuk kembali ke halaman tambah

    }
    array_push($arr, $nomer);
    }
    ##########################################################################


    elseif($nomer != $arr[33] && $arr[33] = 0){
    if($input){

    $query = mysql_query($con, "SELECT * FROM siswa WHERE nomer='".$nomer."'");
    if(mysql_num_rows($query) > 0){
    echo "roll ulang already exists";
    }

    else{

    mysql_query("INSERT INTO siswa VALUES('$nama', '$nomer')") or die(mysql_error());
    if (!mysql_query($con,$sql)){
    die('Error: ' . mysql_error($con));
    }
    }
    }

    else{

    echo 'Gagal menambahkan data! ';    //Pesan jika proses tambah gagal
    echo 'Kembali'; //membuat Link untuk kembali ke halaman tambah

    }
    array_push($arr, $nomer);
    }
    ##########################################################################


    elseif($nomer != $arr[34] && $arr[34] = 0){
    if($input){

    $query = mysql_query($con, "SELECT * FROM siswa WHERE nomer='".$nomer."'");
    if(mysql_num_rows($query) > 0){
    echo "roll ulang already exists";
    }

    else{

    mysql_query("INSERT INTO siswa VALUES('$nama', '$nomer')") or die(mysql_error());
    if (!mysql_query($con,$sql)){
    die('Error: ' . mysql_error($con));
    }
    }
    }

    else{

    echo 'Gagal menambahkan data! ';    //Pesan jika proses tambah gagal
    echo 'Kembali'; //membuat Link untuk kembali ke halaman tambah

    }
    array_push($arr, $nomer);
    }
    ##########################################################################

    else{
        return 0;
    }
    }
    /*
    else{   //jika tidak terdeteksi tombol tambah di klik

    //redirect atau dikembalikan ke halaman tambah
    echo '<script>window.history.back()</script>';

    }*/

?>
<script>setTimeout(function(){window.location.href='index.php'},2000);</script>
</body>
</HTML>

如果需要,可以使用SQL存储输入的名称和随机数:

-- phpMyAdmin SQL Dump
-- version 3.4.5
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Apr 19, 2017 at 08:16 AM
-- Server version: 5.5.16
-- PHP Version: 5.3.8

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;

--
-- Database: `sigizi`
--

-- --------------------------------------------------------

--
-- Table structure for table `siswa`
--

CREATE TABLE IF NOT EXISTS `siswa` (
    `siswa_nama` varchar(50) NOT NULL,
    `siswa_nomer` int(2) NOT NULL,
  PRIMARY KEY (`siswa_nomer`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=11 ;

--
-- Dumping data for table `siswa`
--

-- INSERT INTO `siswa` (`siswa_nama`, `siswa_nis`) VALUES
-- (7, 0, 'Abied Maulana Sofa', 'XII', 'Teknik Komputer dan Jaringan'),
-- (10, 0, 'Hofsatur Rosita', 'XI', 'Akuntansi');

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

0 个答案:

没有答案