Jump to content

Recommended Posts

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...

 

Link to comment
https://forums.phpfreaks.com/topic/277148-notice-undefined-variable/
Share on other sites

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 by Jessica

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.

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. 

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...

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 by seandisanti

@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 . ")";

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']);

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.