Once we create trigger in php page then do we need to create same in Mysql? -
i using mysql database purpose php code.
i have created trigger in php code below, need create in mysql??
my following insert data table, , show content of tables. action performed in trigger not make change. there problem in trigger?
once started working fine after changed table name stopped working though kept table name same php page , 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 connect mysql: " . mysqli_connect_error(); } $sql1="select * 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 each row begin insert details values ($id,$fname,$lname,$city);";** mysqli_query($con,$sql3); $sql5="insert 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 * 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>
short answer - no, don't have because creating in code creates in mysql. have bigger problems.
longer answer -
triggers part of database, , typically wouldn't create trigger code. create triggers same way create tables - create them once in mysql , stick around until drop them.
technically code have work, create trigger
statement succeed first time called. in subsequent executions of script create trigger
error out because trigger exists. since aren't checking errors, script continue on happily.
also, way trigger made, insert same record details
table inserted when trigger created.
finally, have serious security issues code:
- you directly using
post
variables in sql opens sql injection - whatever user site running shouldn't have permissions execute ddl statements
create trigger
Comments
Post a Comment