cainam29 Posted April 19, 2013 Share Posted April 19, 2013 can someone help figured out how to resolve this error: Notice: Undefined variable: here is my php page that gets called by my main page once Delete button is clicked... <?php require 'include/DB_Open.php'; $ids = isset($_POST['id']) ? $_POST['id'] : ''; if (!empty($ids)) { //implode the id's separated by commaas $ids_to_be_deleted = implode("'ars_no, phone_number, category_1, category_2, status, create_date, resolved_date, trouble_type_priority, ban_type, employee_id_name'", $ids); $query = "DELETE FROM tbl_main WHERE (ars_no, phone_number, category_1, category_2, status, create_date, resolved_date, trouble_type_priority, ban_type, employee_id_name) IN ($ars_no, $phone_number, $category_1, $category_2, $status, $create_date, $resolved_date, $trouble_type_priority, $ban_type, $employee_id_name)"; //now run your query using mysql_query $myData = mysql_query($query); }; include 'include/DB_Close.php'; ?> i am getting Notice: Undefined variable: from this line, $query = "DELETE FROM tbl_main WHERE (ars_no, phone_number, category_1, category_2, status, create_date, resolved_date, trouble_type_priority, ban_type, employee_id_name) IN ($ars_no, $phone_number, $category_1, $category_2, $status, $create_date, $resolved_date, $trouble_type_priority, $ban_type, $employee_id_name)"; need help in defining the variable here... Quote Link to comment Share on other sites More sharing options...
Jessica Posted April 19, 2013 Share Posted April 19, 2013 (edited) You define a variable thusly: $var = 'value'; What's probably more important is A. which variable and B. what value you want it to have. Showing us only 1/10 of the error is kind of silly. If that's your entire code then you have some major issues. Like what do you think all of those variables should be? Edit: Furthermore, your DELETE query is way wrong. Edited April 19, 2013 by Jessica Quote Link to comment Share on other sites More sharing options...
cainam29 Posted April 19, 2013 Author Share Posted April 19, 2013 hi thanks for the response...the code that i've posted here is my php page that is being called by my main php page thru ajax...what it will do is to delete a row that was checked. can u please try to define what is wrong with my code? Quote Link to comment Share on other sites More sharing options...
Jessica Posted April 19, 2013 Share Posted April 19, 2013 All of those variables are undefined. Are you confused about what the word undefined means? For one thing, you only need to use the ids to delete the rows, not all that other stuff. You have a weird combination of an INSERT mixed with DELETE. Quote Link to comment Share on other sites More sharing options...
seandisanti Posted April 19, 2013 Share Posted April 19, 2013 1) why create $ids_to_be_deleted if you're not using it? 2) I really don't think your WHERE statement is valid, but even if it is, that is quite possibly the worst WHERE statement you could ever use. Think about it, you're giving it two sets of values, you're not comparing each member of each set with its relative, you're looking for the presence of each member of group a in group b, regardless of what field group b has the value for. Imagining the situation with numeric values may make it make more sense. say you've got 2 groups of numbers, (1,2,3,4) and (4,3,2,1) They're obviously different groups. but checking that each member of the sequence is in the group would select both of them. Quote Link to comment Share on other sites More sharing options...
cainam29 Posted April 19, 2013 Author Share Posted April 19, 2013 okay...ive edited it to this: <?php require 'include/DB_Open.php'; $ids = isset($_POST['id']) ? $_POST['id'] : ''; if (!empty($ids)) { //implode the id's separated by commas $ids_to_be_deleted = implode('ars_no, phone_number, category_1, category_2, status, create_date, resolved_date, trouble_type_priority, ban_type, employee_id_name', $ids); $query = "DELETE FROM tbl_main WHERE (ars_no, phone_number, category_1, category_2, status, create_date, resolved_date, trouble_type_priority, ban_type, employee_id_name) IN ($ids_to_be_deleted)"; but it doesn't seem to be deleting the rows from my table... Quote Link to comment Share on other sites More sharing options...
seandisanti Posted April 19, 2013 Share Posted April 19, 2013 (edited) Alrighty... let's step through this then. let's say $_POST['id'] contains array(1,2). $ids_to_be_deleted then contains '1ars_no, phone_number, category_1, category_2, status, create_date, resolved_date, trouble_type_priority, ban_type, employee_id_name2' That's obviously garbage, but just for laughs lets keep on... $query now contains the string '"DELETE FROM tbl_main WHERE (ars_no, phone_number, category_1, category_2, status, create_date, resolved_date, trouble_type_priority, ban_type, employee_id_name) IN (1ars_no, phone_number, category_1, category_2, status, create_date, resolved_date, trouble_type_priority, ban_type, employee_id_name2)" hopefully you can see how that would never, could never work as selection criteria for a delete or a select or anything else. If you're trying to delete a record or records that match an array of id's, just do it like this: $ids = implode(',',$_POST['id']); $query = "DELETE from tbl_main WHERE id IN(" . $ids . ")"; Edited April 19, 2013 by seandisanti Quote Link to comment Share on other sites More sharing options...
cainam29 Posted April 19, 2013 Author Share Posted April 19, 2013 @seandisanti please check if i got the codes correct as u suggested... <?php require 'include/DB_Open.php'; $ids = isset($_POST['id']) ? $_POST['id'] : ''; if (!empty($ids)) { //implode the id's separated by commaas $ids = implode('ars_no, phone_number, category_1, category_2, status, create_date, resolved_date, trouble_type_priority, ban_type, employee_id_name', $_POST['id']); $query = "DELETE from tbl_main WHERE id IN(" . $ids . ")"; //now run your query using mysql_query $myData = mysql_query($query); }; include 'include/DB_Close.php'; ?> do i still need to replace the id here with my values on this line: $query = "DELETE from tbl_main WHERE id IN(" . $ids . ")"; Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted April 19, 2013 Share Posted April 19, 2013 Did you really read seandisanti's reply how to use the implode php function? Is it true ? $ids = implode('ars_no, phone_number, category_1, category_2, status, create_date, resolved_date, trouble_type_priority, ban_type, employee_id_name', $_POST['id']); Quote Link to comment Share on other sites More sharing options...
seandisanti Posted April 19, 2013 Share Posted April 19, 2013 in my example, records are being delated from the table if the value contained in their 'id' field is contained in array orginally held in $_POST['id'] Personally, I would say to change it from 'DELETE ...' to 'SELECT * ....' while you sort out your query. that way you can see what records would be deleted, without actually deleting them. Quote Link to comment Share on other sites More sharing options...
cainam29 Posted April 19, 2013 Author Share Posted April 19, 2013 will try that...thanks... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.