我需要知道我是否可以在 php 脚本插入该表之前查询和检查数据库表数据?
我已经制作了一个带有 2 个字段并提交按钮(POST)的 html,并且我也可以毫无问题地将数据添加到数据库表并获取其结果,但是我有重复项,我不想拥有它......我想要脚本检查我的数据,输入数据,如果没有重复,则添加它。
请协助。
谢谢
代码
<?php
$con=mysqli_connect("localhost","root","Opera1","railway");
if(mysqli_connect_errno())
{
echo "Failed to connect to MySQL: ", mysqli_connect_error;
}
$data1 = $_POST['data1'];
$data2 = $_POST['data2'];
$sql="replace into pipe (data1, data2)
values ('$data1','$data2')";
if(!mysqli_query($con,$sql))
{
die('Error : ' . mysqli_error($con));
}
echo "Record to Registraton added";
$result = mysqli_query($con,"SELECT * FROM pipe");
echo "<table border='1'>
<tr>
<th>Data1</th>
<th>Data2</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['data1'] . "</td>";
echo "<td>" . $row['data2'] . "</td>";
echo "</tr>";
}
echo "</table><br><br>";
mysqli_close($con);
?>