大家好,我一共试了大约一个小时才找到一个简单的代码,这使我的“添加联系人”表单检查是否没有字段“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 :(得分: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';
}
这将检查分机号码是否已存在,打印错误消息,然后再次显示该表格。
答案 1 :(得分: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;
}
答案 2 :(得分: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>