0

I'm having a little problem with the codes given below. When I'm using the name="staff_number[]" then it insert the record with everything ok even if it is already in the database table and when i use name="staff_number" it does check the record and also give me alert box but when insert the record if it is not in the database it stores only the first number of the staff number like the staff no is 12345 it stores only 1. can anyone help in this record i think there is only a minor issue what I'm not able to sort out.

PHP Code:

<select placeholder='Select' style="width:912px;" name="staff_number[]" multiple />
<?php 
$query="SELECT * FROM staff";
$resulti=mysql_query($query);
while ($row=mysql_fetch_array($result)) { ?>
<option value="<?php echo $row['staff_no']?>"><?php echo $row['staff_name']?></option>
<?php } ?>
</select>

Mysql Code:

$prtCheck = $_POST['staff_number'];
$resultsa = mysql_query("SELECT * FROM staff where staff_no ='$prtCheck' ");
$num_rows = mysql_num_rows($resultsa);
if ($num_rows > 0) {
echo "<script>alert('Staff No $prtCheck Has Already Been Declared As CDP');</script>";
$msg=urlencode("Selected Staff ".$_POST['st_nona']." Already Been Declared As CDP");
echo'<script>location.href = "cdp_staff.php?msg='.$msg.'";</script>';
}

Insert Query

$st_nonas    =  $_POST['st_nona'];
$t_result    = $_POST['st_date'];
$p_result    = $_POST['remarks'];
$arrayResult = explode(',', $t_result[0]);
$prrayResult = explode(',', $p_result[0]);     $arrayStnona = $st_nonas;
$countStnona = count($arrayStnona);


for ($i = 0; $i < $countStnona; $i++) {
    $_stnona  = $arrayStnona[$i];
    $_result  = $arrayResult[$i];
    $_presult  = $prrayResult[$i];

    mysql_query("INSERT INTO staff(st_no,date,remarks) 
        VALUES ('".$_stnona."', '".$_result."', '".$_presult."')");
    $msg=urlencode("CDP Staff Has Been Added Successfully");
    echo'<script>location.href = "cdp_staff.php?msg='.$msg.'";</script>';
 }
3
  • Can you make your question much more clear . Commented Jan 2, 2014 at 7:57
  • I don't see any insert parts ... Commented Jan 2, 2014 at 8:00
  • @amit what you want me to clear ??? i am facing problem that when i use array for the staff_number[] the query to verify if record exist or not doesnt work. but when i use staff_number query works fine but when i press Add button it saves only the first number of the Staff Number... Commented Jan 2, 2014 at 8:02

4 Answers 4

3

Your $_POST['staff_number'] is actually an array.

So you have to access it like $_POST['staff_number'][0] here, 0 is a index number.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks alot for your answer. plz tell me one thing only when insert query runs it is not inserting the records which are not in the database
1

If the name of select is staff_number[] then $prtCheck will be a array so your check query must be in a loop to make sure your check condition.

if the name is staff_number then the below code is fine.

Comments

0

The answer of amit is right but I will complete it.

Your HTML form give to your PHP an array due to the use of staff_number[] with [] that it seems legit with the "multiple" attribute. So you have to loop on the given values, you do it with a for and a lot of useless variables without really checking it. From a long time, we have the FOREACH loop structure.

I could help you more if i know what is the 'st_nona', st_date' and 'remarks' values.

1 Comment

st_date is multiple date selection box and st_nona is staff no of the selected user
0

According to your question you are getting difficulty in storing the data. This question is related to $_POST array.

Like your question we have selected following ids from the select : 1,2,3,4 It is only storing 1. This is due to you have not used the loop when inserting the data. Like below:

<?php
foreach($_POST['staffnumber'] as $staffnumber){
$query=mysql_query("select * from staff where staff_number =".$staffnumber);
if(mysql_num_rows($query)>0){
//action you want to perform
}else{
//action you want to perform like entering records etc. as your wish
}
}
?>

And I would like to suggest you that use the unique keys in database for field and use PHP PDO for database, as it is secure and best for OOPs. Let me know if you have any queries.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.