我将 Mysql 用于我的 php 代码的数据库目的。
我已经在 php 代码中创建了触发器,如下所示,现在我需要在 mysql 中创建它吗?
我的以下将数据插入表中,并显示表的内容。但是我在触发器中执行的操作没有任何改变。触发器有问题吗?
一旦它开始工作正常但在我更改表名后它停止工作虽然我保持表名与我的 php 页面和 mysql 相同。
<html>
<body>
<?php
$id=$_POST['id'];
$fname=$_POST['fname'];
$lname=$_POST['lname'];
$city=$_POST['city'];
$con=mysqli_connect('127.0.0.1:3306' ,'root','root','my_db');
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql1="select * from student";
$result = mysqli_query($con,$sql1);
echo "<table border='1'>
<tr>
<th>Id</th>
<th>Firstname</th>
<th>Lastname</th>
<th>City</th>
</tr>";
while($row = mysqli_fetch_array($result,MYSQLI_ASSOC))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['fname'] . "</td>";
echo "<td>" . $row['lname'] . "</td>";
echo "<td>" . $row['city'] . "</td>";
echo "</tr>";
}
echo "</table>";
**$sql3 = "CREATE TRIGGER MysqlTrigger AFTER INSERT ON student FOR EACH ROW BEGIN INSERT INTO details VALUES ($id,$fname,$lname,$city);";**
mysqli_query($con,$sql3);
$sql5="INSERT INTO student (id,fname, lname, city)
VALUES
('$_POST[id]','$_POST[fname]','$_POST[lname]','$_POST[city]')";
mysqli_query($con,$sql5);
echo "1 record added";
print "<h2>After performing Trigger updated table details</h2>";
echo "<table border='1'>
<tr>
<th>Id</th>
<th>Firstname</th>
<th>Lastname</th>
<th>City</th>
</tr>";
$sql4="select * from details";
$res = mysqli_query($con,$sql4);
while($row = mysqli_fetch_array($res,MYSQLI_ASSOC))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['fname'] . "</td>";
echo "<td>" . $row['lname'] . "</td>";
echo "<td>" . $row['city'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
</body>
</html>