2016-06-10 41 views
2

嗨,大家iv一直在嘗試約一個小時,找到一個簡單的代碼,這使得我的「添加聯繫人」形式檢查是否沒有重複的字段「ext」,但我似乎無法得到它工作:(重複項目的消息框

基本上它需要檢查,如果已經有相同值的分機號碼,然後給出一個消息說「分機號碼已經存在」

<?php 
mysql_connect("localhost", "root", "password") or die(mysql_error()); 
mysql_select_db("phonebook") or die(mysql_error()); 
$mode = $_GET['mode']; 
$checkSql="select count(id) as eCount from address"; 
$result = mysql_query($checkSql); 
$row = mysql_fetch_assoc($result); 
if($row['eCount'] == 999) { 
    $disable = 1; 
} 
switch($mode) { 
    case 'add': 
    ?> 
<h2>Add Contact</h2> 
<form name="form1" action="<?=$_SERVER['PHP_SELF'];?>?mode=added" method="post"> 
    <div align="center"> 
     <table class="searchable"> 
      <tr><td>Extension:</td><td><div align="left"> 
       <input type="text" name="ext" /> 
      </div></td></tr> 
      <tr><td>Name:</td><td><div align="left"> 
       <input type="text" name="name" /> 
      </div></td></tr> 
      <tr><td>Department:</td><td><div align="left"> 
       <input type="text" name="department" /> 
      </div></td></tr> 
      <tr><td>Email:</td><td><div align="left"> 
       <input type="text" name="email" /> 
      </div></td></tr> 
      <tr><td>Cellphone:</td><td><div align="left"> 
       <input type="text" name="phone" /> 
      </div></td></tr> 
      <tr><td colspan="2" align="center"><a href="javascript:history.go(-1);">Back</a> | <input name="Submit" type="submit" id="Submit" value="Add New Contact" <?php if($disable ==1){?>disabled<?php } ?>/></td></tr> 
      <input type="hidden" name="mode" value="added"> 
     </table> 
    </div> 
</form> 
<?php 
break; 
case 'added': 
    $name = $_POST['name']; 
    $phone = $_POST['phone']; 
    $email = $_POST['email']; 
    $department = $_POST['department']; 
    $ext = $_POST ['ext']; 
    $sql = "INSERT INTO address (ext, name, department ,email, phone) VALUES ('" . $ext . "','" . $name . "','" . $department . "', '" . $email . "', '" . $phone . "')"; 
    mysql_query($sql); 
    header('location: ' . $_SERVER['PHP_SELF']); 
break; 
+0

注意:'mysql_ *'功能已被棄用,他們已經從PHP 7刪除,你的代碼將停止在升級到該版本的工作。您不應使用它們編寫新代碼,而應使用['mysqli_ *'或PDO](http://php.net/manual/en/mysqlinfo.api.choosing.php)。 –

回答

1

這應該做的工作

$checkSql="select count(id) as eCount from address where ext = " . $_POST['ext']; 

但是,您正在使用不推薦使用的MySQL版本。考慮更新到MySQLi或PDO。

您也可以更新您的代碼以提供錯誤消息。例如:

if($row['eCount'] > 0) { 
    echo "Extension Number already exists"; 
    $mode = 'add'; 
} 

這將檢查是否已存在分機號碼,打印錯誤消息並再次顯示錶單。

+0

謝謝先生完美的作品:) –

+0

如果可能的話,你能幫助我多做一件事嗎? –

+1

當然,問我:) –

0

我不明白你爲什麼使用開關。我沒有使用它,但正如你所提到的,我在添加擴展否前檢查,如果已經存在,那麼wii會給出一條消息,否則將添加爲新記錄。

index.php 



    <?php 
     $message = ''; 
     mysql_connect("localhost", "root", "root") or die(mysql_error()); 
     mysql_select_db("phonebook") or die(mysql_error()); 
     if (isset($_POST['submit'])){ 
     $name = $_POST['name']; 
     $phone = $_POST['phone']; 
     $email = $_POST['email']; 
     $department = $_POST['department']; 
     $ext = $_POST['ext']; 
     $checkSql = "select count(id) as eCount from address where ext = " . $_POST['ext'].""; 
     $result = mysql_query($checkSql); 
     $data=mysql_fetch_assoc($result); 
     if($data['eCount'] == 0){ 
// as you have check it to 999 so if you want that it should be less than or equal to 999 times only then you can check `$data['eCount']<= 999` then do entry otherwise error message 
     $name = $_POST['name']; 
     $phone = $_POST['phone']; 
     $email = $_POST['email']; 
     $department = $_POST['department']; 
     $ext = $_POST ['ext']; 
     $sql = "INSERT INTO address (ext, name, department ,email, phone) VALUES ('" . $ext . "','" . $name . "','" . $department . "', '" . $email . "', '" . $phone . "')"; 
     mysql_query($sql); 
     $message = "Entery has been done successfully"; 
     $_POST = array(); 
     }else { 

      $message = "Selected extension number $ext already exist"; 
     } 
     } 
     ?> 

     <h2>Add Contact</h2> 
     <form name="form1" action="" method="post"> 
      <div align="center"> 
       <table class="searchable"> 
       <tr><td colspan="2"><h3><?php echo $message;?></h3></td></tr> 
        <tr><td>Extension:</td><td><div align="left"> 
         <input type="text" name="ext" value="<?php if(isset($_POST['ext'])){echo $_POST['ext'];}?>" /> 
        </div></td></tr> 
        <tr><td>Name:</td><td><div align="left"> 
         <input type="text" name="name" value="<?php if(isset($_POST['name'])){echo $_POST['name'];}?>" /> 
        </div></td></tr> 
        <tr><td>Department:</td><td><div align="left"> 
         <input type="text" name="department" value="<?php if(isset($_POST['department'])){echo $_POST['department'];}?>"/> 
        </div></td></tr> 
        <tr><td>Email:</td><td><div align="left"> 
         <input type="text" name="email" value="<?php if(isset($_POST['email'])){echo $_POST['email'];}?>"/> 
        </div></td></tr> 
        <tr><td>Cellphone:</td><td><div align="left"> 
         <input type="text" name="phone" value="<?php if(isset($_POST['phone'])){echo $_POST['phone'];}?>" /> 
        </div></td></tr> 
        <tr><td colspan="2" align="center"><a href="javascript:history.go(-1);">Back</a> | <input name="submit" type="submit" id="Submit" value="Add New Contact"/></td></tr> 

       </table> 
      </div> 
     </form> 
1
Add this below code to below $ext = $_POST ['ext']; and i hope you close the bracket '}' of switch case if yes then remove last bracket from my solution code i hope it's helpfull for you 


$check_ext ="SELECT * FROM address WHERE ext = ".$ext; 
    $con = mysql_connect("localhost", "root", "password") or die(mysql_error()); 
    $checked_ext = mysqli_query($con,$check_ext); 
    $data_chk = mysqli_fetch_array($checked_ext, MYSQLI_NUM); 
    if($data_chk[0]>1) 
    {echo "Extension Number already exists";} 
    else{ 
    $sql = "INSERT INTO address (ext, name, department ,email, phone) VALUES ('" . $ext . "','" . $name . "','" . $department . "', '" . $email . "', '" . $phone . "')"; 
    mysql_query($sql); 
    header('location: ' . $_SERVER['PHP_SELF']); 
    } 
    break; 

}