触发器确实在表中进行了更改

时间:2013-07-03 10:01:22

标签: mysql database triggers

我将几个字段插入到数据库中,我可以这样做。我有一个触发器将数据插入表“AFTER INSERT”事件。

<html>
<body>

<?php
$id=$_POST['id'];
$a=$_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 table2";

$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 =“在插入temp2之后创建触发器MysqlTrigger,每次行开始插入临时值(new.id,new.fname,new.lname,new.city,new.city);”;     mysqli_query($ CON,$ SQL3);

$sql5="INSERT INTO temp2 (id,fname, lname, city)
VALUES
('$_POST[id]','$_POST[fname]','$_POST[lname]','$_POST[city]')";
mysqli_query($con,$sql5);



echo "1 record added";

//--------------- Trigger started ------------------

print "<h2>CREATE MySQL Trigger In PHP</h2>";
echo "<table border='1'>
<tr>
<th>Id</th>
<th>Firstname</th>
<th>Lastname</th>
<th>City</th>
<th>Status</th>
</tr>";
$sql4="select * from temp";
$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 "<td>" . $row['stat'] . "</td>";
echo "</tr>";
}
echo "</table>"; 

mysqli_close($con);

?>
</body>
</html>

这会成功将数据插入temp2表,但触发器中的事件不会对临时表进行任何更改。

我在哪里弄错了?

1 个答案:

答案 0 :(得分:2)

来自mySQL manual

delimiter |

CREATE TRIGGER testref BEFORE INSERT ON test1
  FOR EACH ROW BEGIN
    INSERT INTO test2 SET a2 = NEW.a1;
    DELETE FROM test3 WHERE a3 = NEW.a1;
    UPDATE test4 SET b4 = b4 + 1 WHERE a4 = NEW.a1;
  END;
|

delimiter ;

您错过了触发器上的延迟结束


为什么你要使用php创建触发器?你不能在MySQL Workbench上使用phpMyAdmin创建它吗?触发器应该存在于数据库中......如果它不应该是永久性的,你应该将其放在php代码的末尾...