mercredi 20 janvier 2016

Incorrect logic in the conditions for inserting values in PHP

I am trying to add an id with its corresponding position in the database. I have the following conditions:

  • if the id has is already existing in the database, don't add
  • if a certain 'position' is already existing in the database, don't add
  • the rest of the available positions can be added multiple times

The positions are the following:

  • Captain *
  • Secretary *
  • Treasurer *
  • Book Record Keeper *
  • Sweeper
  • Messenger *
  • Utility *
  • Service Driver *
  • Leaders
  • Judge

The one with the asterisks are the ones that should only be inserted once in the databases. The others can be inserted many times. The rule still remains, one id occurrence in the database.

So if id number 5 is added to the database with position Secretary... number 5 cannot be added again as well as secretary cannot be added even if another id number is attributed to it like id number 4 and position secretary... But if id number 90 and 91 and 92 would be added to the database with position 'Leaders'... it would be added.

The html dropdown code:

 <div class="item form-group">
    <label class="control-label col-md-3 col-sm-3 col-xs-12">Position<span class="required">*</span></label>
            <div class="col-md-6 col-sm-6 col-xs-12">
                <select id = "status" name = "position" class="form-control" required>
                    <option value="">Choose..</option>
                    <option value="Captain">Captain</option>
                    <option value="Secretary">Secretary</option>
                    <option value="Treasurer">Treasurer</option>
                    <option value="Book Record Keeper">Book Record Keeper</option>
                    <option value="Judge">Judge</option>
                    <option value="Leaders">Leaders</option>
                    <option value="Service Driver">Service Driver</option>
                    <option value="Utility">Utility</option>
                    <option value="Messenger">Messenger</option>
                    <option value="Sweeper">Sweeper</option>
                </select> 
            </div>
    </div>

Here is my php code:

<?php

include ("config.php"); //this is working just fine
if(isset($_POST['addPosition'])) {

        $id = $_POST['person_id'];
        $position = $_POST['position'];

        $test = mysqli_query($conn, "SELECT COUNT(*) AS count_value FROM table_position WHERE bar_position='Barangay Captain';");
        $data = mysqli_fetch_assoc($test);
        echo $data['count_value']." <br>";

        $test1 = mysqli_query($conn, "SELECT COUNT(*) AS count_value1 FROM table_position WHERE bar_position='Secretary';");
        $data1 = mysqli_fetch_assoc($test1);
        echo $data1['count_value1']." <br>";

        $test2 = mysqli_query($conn, "SELECT COUNT(*) AS count_value2 FROM table_position WHERE bar_position='Treasurer';");
        $data2 = mysqli_fetch_assoc($test2);
        echo $data2['count_value2']." <br>";

        $test3 = mysqli_query($conn, "SELECT COUNT(*) AS count_value3 FROM table_position WHERE bar_position='Book Record Keeper';");
        $data3 = mysqli_fetch_assoc($test3);
        echo $data3['count_value3']." <br>";

        $test4 = mysqli_query($conn, "SELECT COUNT(*) AS count_value4 FROM table_position WHERE bar_position='Barangay Messenger';");
        $data4 = mysqli_fetch_assoc($test4);
        echo $data4['count_value4']." <br>";

        $test5 = mysqli_query($conn, "SELECT COUNT(*) AS count_value5 FROM table_position WHERE bar_position='Service Driver';");
        $data5 = mysqli_fetch_assoc($test5);
        echo $data5['count_value5']." <br>";

        $test6 = mysqli_query($conn, "SELECT COUNT(*) AS count_value6 FROM table_position WHERE bar_position='Barangay Utility';");
        $data6 = mysqli_fetch_assoc($test6);
        echo $data6['count_value6']." <br>";

        $test_id = mysqli_query($conn, "SELECT COUNT(*) AS count_id FROM table_position WHERE Person_idPerson='$id';");
        $data_id = mysqli_fetch_assoc($test_id);
        echo $data_id['count_id'];


        if ($data_id['count_id'] == 1 or $data['count_value'] == 1 or $data1['count_value1'] == 1 or $data2['count_value2'] == 1 or $data3['count_value3'] == 1 or $data4['count_value4'] == 1 or $data5['count_value5'] == 1 or $data6['count_value6'] == 1) {
            echo "error!";
        }


        else {
            $insertPos = mysqli_query($conn, "INSERT INTO table_position(Person_idPerson, bar_position) VALUES ('$id', '$position');");
        }


        mysqli_close($conn);


}


?>

There is something wrong in the logic of my code and I tried fixing it for hours now. I'm still familiarizing php. Can you please help me? What parts should I change? Your help will be much appreciated.

Aucun commentaire:

Enregistrer un commentaire