|
|
|
จะเขียนฟังก์ชั่นตรวจสอบการแจ้งเตือนเพิ่มข้อมูลซ้ำ ใน Form Insert, Edit , Delete อย่างไง |
|
|
|
|
|
|
|
พอดีทำผม Form Insert edit delete อยากจะทราบว่า จะเขียนการทำงานฟังก์ชั่น เอาไว้ตรวจสอบแจ้งเตือนเพิ่มข้อมูลซ้ำ ไม่ทราบว่า ต้องเอาคำสั่งแทรกไว้บรรทัดไหน ถึงมันจะไม่ติด Error ขึ้นมา จากตัวอย่างตรวจสอบข้อมูลซ้ำลิงค์นี้ https://www.thaicreate.com/php/php-mysql-check-already-exist-add-insert-record.html
process.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title></title>
</head>
<body>
<?php
$servername = "localhost";
$username = "root";
// $password = "rsrc2totdb2017";
$password = "";
$dbname = "sam_db";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
mysqli_set_charset($conn, "utf8");
$username=isset($_POST['username']) ? $_POST['username'] : '';
$attribute=isset($_POST['attribute']) ? $_POST['attribute'] : '';
$op=isset($_POST['op']) ? $_POST['op'] : '';
$value=isset($_POST['value']) ? $_POST['value'] : '';
$edit=isset($_POST['edit']) ? $_POST['edit'] : '';
$id=isset($_POST['id']) ? $_POST['id'] : '';
if ($id!='') {
$sql = "UPDATE radreply SET username='".$username."',
value='".$value."'
WHERE id='".$id."'
";
}else{
$sql = "INSERT INTO radreply (id, username,attribute,op,value)
VALUES ('', '$username', 'Framed-IP-Address', ':=', '$value')";
}
if(mysqli_query($conn, $sql)) {
if($id!=''){
echo "<script>";
echo "alert('Updated successfully');";
echo "window.location.href='index.php';";
echo "</script>";
}else{
echo "<script>";
echo "alert('New record created successfully');";
echo "window.location.href='index.php';";
echo "</script>";
}
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>
</body>
</html>
index.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport” content=”width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edg">
<title></title>
</head>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js" integrity="sha384-smHYKdLADwkXOn1EmN1qk/HfnUcbVRZyYmZ4qpPea6sjB/pTJ0euyQp0Mk8ck+5T" crossorigin="anonymous"></script>
<center>
<body>
<?php
$servername = "localhost";
$username = "root";
// $password = "rsrc2totdb2017";
$password = "";
$dbname = "sam_db";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
mysqli_set_charset($conn, "utf8");
?>
<header class="container" align="center">
<h1><b>Fix IP กับ MacAddress</b></h1>
</header>
<center><table class="table" border="1" height="50px" width="90%">
<tr>
<td width="50%"><center>Fix IP</center></td>
<td width="10%"><center>MAC Adress</center></td>
<td width="15%"><center>ตั้งค่า</center></td>
<tr>
<?php
$sql = "SELECT * FROM radreply";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) { ?>
<tr>
<td><center><?php echo $row["username"]; ?></center></td>
<td><center><?php echo $row["value"]; ?></center></td>
<td><a href="Form_insert.php?id=<?php echo $row["id"];?>">Edit</a>
<a href="del.php?id=<?php echo $row["id"];?>">Delete</a>
</td>
</tr>
<?php
}
} else {
echo "0 results";
}
mysqli_close($conn);
?>
</table></center>
<form action="Form_insert.php" method="post" class="form-horizontal">
<button type="submit" class="btn btn-primary" name="submit">เพิ่มข้อมูล</button>
</form>
</body>
</center>
</html>
Form_insert.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport” content=”width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edg">
<title></title>
</head>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js" integrity="sha384-smHYKdLADwkXOn1EmN1qk/HfnUcbVRZyYmZ4qpPea6sjB/pTJ0euyQp0Mk8ck+5T" crossorigin="anonymous"></script>
<center>
<body>
<?php
$servername = "localhost";
$username = "root";
// $password = "rsrc2totdb2017";
$password = "";
$dbname = "sam_db";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
mysqli_set_charset($conn, "utf8");
$id=isset($_GET['id']) ? $_GET['id']:'';
if($id!==''){
$sql = "SELECT * FROM radreply Where id='".$id."'";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
}else{
$sql = "SELECT * FROM radreply Where id='".$id."'";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
}
?>
<h1><?php if($id!='') {echo "แก้ไขข้อมูล";}else{echo "เพิ่มข้อมูล";}?></h1>
<br>
<form method="post" id="form" enctype="multipart/form-data" action="process.php">
<?php if($id!=''){?>
<input type="hidden" name="id" size="30" value="<?php echo $row["id"];?>">
<?php } ?>
<div class="form-group row">
<label class="col-sm-2 col-form-label">Fix IP</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="username" placeholder="IP" value="<?php echo $row["username"];?>">
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 col-form-label">Mac</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="value" placeholder="xx-xx-xx-xx-xx-xx" value="<?php echo $row["value"];?>">
</div>
</div>
<div class="form-group row">
<div class="col-sm-10">
<input type="submit" value="<?php if($id!='') {echo "แก้ไขข้อมูล";}else{echo "เพิ่มข้อมูล";}?>">
</div>
</div>
</form>
</body>
</center>
</html>
Tag : PHP, XAMPP, Windows
|
ประวัติการแก้ไข 2019-09-17 10:24:07
|
|
|
|
|
Date :
2019-09-17 10:14:09 |
By :
eak444 |
View :
681 |
Reply :
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
เช็คอะไรซ้ำ ก็แค่ไป select count ดู ก็ได้แล้ว
ตัวอย่างแบบง่ายๆ เอาไปปรับใช้เองนะครับ
Code (PHP)
$full_name="Peter";
$sql = "select * from table_name where full_name='$full_name'";
if(count($sql)>0){
//duplicated exit();
}else{
//do insert, update
}
|
|
|
|
|
Date :
2019-09-17 11:11:14 |
By :
mongkon.k |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 01
|