0

I'm new at PHP so please dont bust my chops.

I'm building a Knowledge base system, basic CRUD functionality. I'm getting stuck where i need to NULL all values for each column in a table.

This is basically the "Edit a KB" form. I have a checkbox array which on edit I want to clear the KB number from my category column on my table 'category_kb_members'.

My code works fine apart from this bit.

This is my current code:

//Clear all categories for this KB
while($catrow = mysqli_fetch_array($categorylist)){
$catname = "";
$catname = $catrow['name'];
mysqli_query ($dbc, "UPDATE category_kb_members SET $catname=NULL WHERE $catname ='$kbid';");
}

[Please note no need to comment abut SQL injection here as the source is clean from the database.]

"$Categorylist" is an array of Category names - the table I am updating has a column for each category.

"$kbid" is set from a $_GET from the URL and corresponds with the KB number to be removed and is valid and working.

My loop is to query and update each column to NULL the KB number if it exists in the column. The code later goes on to add these back in the the relevant columns.

I have run this query against MYSQL and it works fine SQL side.

Any ideas as to why the UPDATE is not working?

Rich
  • 1
  • 4
  • `$catname ='$kbid'` where is this key ?? – Abdulla Nilam Mar 05 '17 at 13:34
  • It's above... it's set and valid... – Rich Mar 05 '17 at 13:36
  • `$catname=NULL` in here are you sure column name is correct? `$catname` – Abdulla Nilam Mar 05 '17 at 13:37
  • I'm certain as the variable is used to build the list of check boxes. Also, code following this inserts the KBID into the correct columns later on.. – Rich Mar 05 '17 at 13:39
  • "no need to comment abut SQL injection here as the source is clean from the database" - Really? "$kbid is set from a $_GET" – Paul Spiegel Mar 05 '17 at 13:41
  • can you use `mysqli_error($query )` to check for your query response? – AnatPort Mar 05 '17 at 13:42
  • OK fair enough. As I said I'm new to PHP. I will use mysqli_real_escape_string on it... – Rich Mar 05 '17 at 13:42
  • Also an while loop that makes queries it's a possible performance issue, you can easily replace the while loop with a where in clause – Dan Ionescu Mar 05 '17 at 13:50
  • Sometimes people asking for help say "The error can't be in ... because ... so i'm not going to check ...". At this point i say "So you don't need my help. Good luck!". – Paul Spiegel Mar 05 '17 at 13:58
  • Thanks Dan, I'll look at that. Anat, I've added or die(mysqli_error($dbc)); and checked the logs but there does not appear to be anything to do with this query. :( Paul - I only mean that the rest of the code is working fine and there are no other errors in logs about these variable. As I said I'm new to PHP so I could just be missing something simple! – Rich Mar 05 '17 at 14:03
  • Turn all error reporting on: http://stackoverflow.com/questions/22662488/how-to-get-mysqli-error-in-different-environments – Paul Spiegel Mar 05 '17 at 14:07
  • Thanks Paul I'll look at this now... – Rich Mar 05 '17 at 14:09
  • I'm only seeing "Notice" remarks in the browser and these are the ones I can see in my PHP log file anyway. Is there a way to make it more verbose? The Notices are only a couple of Undefined variables for other parts of the page.. – Rich Mar 05 '17 at 14:24
  • Question should be closed as "typographical error / not reproducible" (See the answer). It can't help any other user. – Paul Spiegel Mar 05 '17 at 14:43

1 Answers1

0

Thanks for all you're comments. Credits to all who assisted.

I copied my query for $catname

$categorylist = mysqli_query($dbc,"SELECT name FROM categories");

which was further up the code. Although this variable was already set, and working for above code it must have been cleared somewhere above in my code. If I re-run this part above my loop it works fine. I will have to figure out what is overwritting my variable above.

Thanks to all and "Abdulla" for making me think for a 3rd time.

Rich
  • 1
  • 4