php - Mysqli multi query not working, despite appearing correctly formed -
i'm working on php function interacts database add specializations particular user. figured using mysqli multi query best way go accomplishing this. unfortunately none of code queries executing correctly. db connection working without hitch, , individual queries when typed databases web app work.
function assign_specialties($id, $specialties) { $conn = new mysqli($globals['servername'], $globals['username'], $globals['password'], 'visio'); if ($conn->connect_error) { die("$conn->connect_errno: $conn->connect_error"); } $query = ""; $id = mysqli_real_escape_string($conn, $id); foreach ($specialties $specialty) { $specialty = mysqli_real_escape_string($conn, $specialty); $query .= " insert member_specialization (member_id, specialization_name) values ('$id', '$specialty');"; } //$query = mysqli_real_escape_string($conn, $query); if ($conn->multi_query($query)) { echo "<script> alert('yes')</script>"; } else { echo "<script> alert('$query')</script>"; } $conn->close(); }
i have each query seperated semicolon. first time i've tried multi query know has user error, can't figure out error is.
http://php.net/manual/en/mysqli.multi-query.php
data inside query should escaped.
you using mysqli_real_escape_string() on entire query, string needs escaped variable $specialty.
additionally you're using both procedural style , object oriented style in function. whichever style prefer practice consistent.
$query = ""; foreach ($specialties $specialty) { $specialty = mysqli_real_escape_string($conn, $specialty); $query .= "insert member_specialization (member_id, specialization_name) values ('$id', '$specialty');"; } mysqli_multi_query($conn, $query);
Comments
Post a Comment