0

You might say that this question already asked many time! You right, but those dont give me solution. I am trying to insert inserted data and two var into database using trigger on insert event, code is here:

< html > <body > 
<?php 

$s = $_POST['sent'];
echo "Entered sentence : $s";
$a = array();
$b = array();
if (preg_match_all('/[^=]*=([^;@]*)/', shell_exec("/home/technoworld/Videos/LinSocket/client '$s'"), $matches))
//if (preg_match_all('/[^=]*=([^;@]*)/', shell_exec('/home/technoworld/Videos/LinSocket/client '.escapeshellarg($s)), $matches))
{
    $a[] = (int) $matches[1][0];    //optionally cast to int
    $b[] = (int) $matches[1][1];
}

$x = (int) $matches[1][0];
$y = (int) $matches[1][1];

$p=10;
$q=20;
echo $x;
echo "<br/>";
echo $p;
echo "<br/>";


//---------------DB stuff --------------------

$con = mysqli_connect('127.0.0.1:3306', 'root', 'root', 'test');
if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: ".mysqli_connect_error();
}


$sql2 = "CREATE TRIGGER MysqlTrigger AFTER INSERT ON table1 FOR EACH ROW BEGIN INSERT INTO temp VALUES (NEW.sent,'".$x."','".$p."');";
mysqli_query($con,$sql2);

$sql1 = "INSERT INTO table1 (sent)VALUES('$_POST[sent]')";

if (!mysqli_query($con, $sql1)) {
    die('Error: '.mysqli_error($con));
}
echo "1 record added";
mysqli_close($con);

?>
</html > </body >

Data is inserted in table1 successfuly and trigger event also works. But on trigger NEW.sent gets inserted while rest two var always inserted as "ZERO"- 0. Looking at various solutions given on web I tried with :

(NEW.sent,'$x','$p');";
(NEW.sent,"$x","$p");";

and other but every time it insert 0 for this two values.

Data type of $x and $p is Integer and In table I have taken int values for this two paramter. What is the issue?

2
  • If the database fields of those variables are int shouldn't you be inserting them without the single quotes? Commented Jul 9, 2013 at 13:11
  • @TolgaEvcimen: Yeah it should, but it inserts '0' for that case. I am new to PHP so not much aware about the problem here! Commented Jul 9, 2013 at 17:52

1 Answer 1

1

What I suggest is:

(NEW.sent, ".$x.", ".$p.");";
1
  • This also does not work! problem is something different. When I created the trigger code in php file above then also it was working fine. Because the part that was creating trigger is Mysql trigger DELIMITER $$ CREATE /*[DEFINER = { user | CURRENT_USER }]*/ TRIGGER test.MysqlTrigger` AFTER INSERT ON test.table1 FOR EACH ROW BEGIN INSERT INTO temp VALUES(New.sent,'$x','$y'); END$$ DELIMITER ;`. Here How could I retrieve '$x' and '$y'? Commented Jul 10, 2013 at 6:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.