php - Why my mysql transaction is not working properly? -
i've been reading , gathering information 2 days , give up. have no clue why piece of simple code not succeeding.
i want insert data 1 form 2 tables , yes know there same problems described here , there, said i'm familiar them , need ask more questions.
the problem in query somewhere, @ least believe is.
here goes:
unset($err); //variables $host = 'my.server.com'; $user = '123'; $pass = 'password'; $dbname = '123'; $err = array(); $error_form = false; $img = "sth/sth.jpg"; //connecting database using mysqli application programming interface $con = mysqli_connect($host, $user, $pass, $dbname); if (!validate()) { if (!$con) { echo "connection failed : <br />" . $new_con->connect_errno . "<br />" . $new_con->connect_error; exit; } else { echo "connected! <br />"; } var_dump($name); echo "<br />"; var_dump($email); echo "<br />"; var_dump($img); echo "<br />"; $query= "start transaction; insert `123`.`table1` (`name1`,`name2`) values ('". $name . "','". $email ."'); insert `123`.`table2` (`table1_id`,`name3`,`name4`) values (last_insert_id(),'". $story . "','". $img ."'); commit;"; var_dump(mysqli_query($con,$query)); echo "<br />"; $_post["name"] = ""; $_post["email"] = ""; $_post["story"] = ""; } //form validation function validate() { global $name, $email, $story, $err, $error_form; if($_server['request_method']=="post") { if(isset($_post["name"]) && !empty($_post["name"])) { $name = htmlspecialchars($_post["name"]); } else { $err[0] = "name missing."; $error_form = true; } if(isset($_post["email"]) && !empty($_post["email"])) { if (filter_var($_post["email"], filter_validate_email)) { $email = htmlspecialchars($_post["email"]); } else { $err[1] = "email verified incorrect."; $error_form = true; } } else { $err[1] = "email missing."; $error_form = true; } if(isset($_post["story"]) && !empty($_post["story"])) { $story = htmlspecialchars($_post["story"]); } else { $err[2] = "your story not contain characters, can't submited."; $error_form = true; } } return $error_form; }
everything confuses me happens here:
$query= "start transaction; insert `123`.`table1` (`name1`,`name2`) values ('". $name . "','". $email ."'); insert `123`.`table2` (`table1_id`,`name3`,`name4`) values (last_insert_id(),'". $story . "','". $img ."'); commit;"; var_dump(mysqli_query($con,$query));
i've tried select id from table1 table , set @value instead of last_insert_id(). i've tried run 2 queries...many different solutions.
i found out when dump mysqli_query($con,$query) gives false every time unless don't use transaction, simple queries, need them.
last thing should use pdo instead of mysqli? why?
and
why use mysqli object oriented style instead of procedural one?
every appreciated. more understand achieve effect here.
be aware first post here, not first visit.
you can 1 query @ time mysqli_query
@ mysqli_multi_query()
http://www.w3schools.com/php/func_mysqli_multi_query.asp
$query= "start transaction; insert `123`.`table1` (`name1`,`name2`) values ('". $name . "','". $email ."'); insert `123`.`table2` (`table1_id`,`name3`,`name4`) values (last_insert_id(),'". $story . "','". $img ."'); commit;"; var_dump(mysqli_multi_query($con,$query));
Comments
Post a Comment