php mysql 问题,在插入前检查记录是否存在 [英] php mysql issue with check if record exist before insert

查看:57
本文介绍了php mysql 问题,在插入前检查记录是否存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对下面给出的代码有一点问题.当我使用 name="staff_number[]" 时,它会插入一切正常的记录,即使它已经在数据库表中,当我使用 name="staff_number" 它确实检查记录并给我警告框,但是当插入记录时,如果它不在数据库中,它只存储员工编号的第一个数字,例如员工编号是 12345,它只存储 1.任何人都可以帮忙在这个记录中,我认为只有一个小问题我无法解决.

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 代码:

<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 代码:

$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>';
}

插入查询

$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>';
 }

推荐答案

你的 $_POST['staff_number'] 实际上是一个 array.

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

所以你必须像这里的 $_POST['staff_number'][0] 一样访问它,0 是一个索引号.

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

这篇关于php mysql 问题,在插入前检查记录是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆