× This forum is read only. As of July 23, 2019, the UserSpice forums have been closed. To receive support, please join our Discord by clicking here. Thank you!

  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
problem with update function
#1
Hey I was wondering if anyone could tell me why...

<pre>
Code:
$db = DB::getInstance();
    
$customMessage = Input::get('message');
$leagueID = Input::get('id');

        $userfields=array('leagueCustom'=>$customMessage);
            $db->update('leagues',1,$userfields);
</pre>


..doesn't work. The field 'leagueCustom' exist and I know the values are being passed because

Code:
$query = $db->query("UPDATE
leagues
Code:
SET
leagueCustom
Code:
= ? WHERE
leagues
Code:
.
leagueID
Code:
= ?", array($customMessage,$leagueID));

works perfectly fine.

Thank you!
  Reply
#2
I don't have a whole lot of context to go on, but from what I can tell.

Code:
$db->update('leagues',1,$userfields);

Will always try to update the table leagues id field of 1. The 1 you are hard coding in there is the id on the table of leagues.

When you are writing your query out, you are passing in the leagueID properly. My gut says that the update query should be...

Code:
$db->update('leagues',$leagueID),$userfields);

Does that work?
  Reply
#3
Sorry I originally had $leagueID in that value. I was trying to manipulate every value to see if I could get the query to pass. Is there any information I could give to better context?

I've created a leagueAdmin.php which passes variables from a form. Echoing these variables shows they are being passed.

[Image: QXuWkJD.png]

  Reply
#4
Can you paste all of your php over on hastebin.com and share the link here so I can replicate it?
  Reply
#5
of course!

league.php is where you would manipulate league

http://hastebin.com/iyidayokej.xml

if you were the owner of the league it would show you a form where you can manipulate the values of the leagues table
.
The values are passed to this page:
http://hastebin.com/izuqegezit.xml

if i missed anything please let me know!
  Reply
#6
As to why the update isn't being processed properly, it's pretty much one of those things you have to step through. It's almost always related to the id not getting passed from your form as just a single number. I would almost always do something like
Code:
$leagueID = Input::get('id');
Code:
dnd($leagueID) //Dump and Die

to make sure you're really getting the id properly from your form.

Same thing with your custom message. Sorry to keep asking you questions, but when you say the update fails...what's failing. Are you getting a blank field in the db? Are you getting no change to the current field? Is it throwing an error?

Your code looks fine off the top of my head other than the 1 for the id.

Also, are you just wondering why the update method isn't working when the manual query works? I will say that for some reason the update method feels "pickier" to me sometimes. I use the full query every once in a while when I don't want to diagnose that.

I'll keep giving input though once I hear back from you. @Brian might have some ideas also.
  Reply
#7
You are the man, I'm totally fine with using the query now that I understand mysql queries a little better. I have read you shouldn't pass values straight to queries, is there a benefit from using the update function over the query other than time saved?

When I say it fails I mean that there are no change to the current field! No errors though.

On the dump and die topic, I've noticed that if I create a new variable like:

Code:
$league = $db->get('leagues',array('leagueID', '=',$leagueKey));

and then immediately another

Code:
$player = $db->get('player',array('id', '=',$playKey));

I can't access the $league unless I repeat the $db->get.

Would dnd allow me to retain the information from my get?

I have managed to get all my scripts to work with query command, however I'm using alpha 3 (because I can't get alpha 4 to work) and I just realized the registration system isn't working so that's my next step.

You guys have created a great system and if I can figure out these minor problems(other than the join.php, thats really putting me through a loop. Should have tested it before I started doing everything else...) I'm confident I can use this script for my website. Once my websites done, I plan to write some tutorials for people who are less familar with php but want to use your system! So thank you again!
  Reply
#8
And sorry if my code is messy!
  Reply
#9
The update function automatically does the same thing as the ? in your prepared (bound) sql statement, so you're pretty much good to go. I use the update method a lot in foreach loops and things like that. It's quick when you're doing something simple, but what you're doing works just as well and is more readable.

dnd kills the page so it's more for tracking down errors. Basically it is a very simple helper that does a preformatted(one instance per line) var_dump and then kills the page so you get exactly what was put in the variable before anything else mucks with it. You can also do dump($variable); to do a preformatted var_dump without killing the page. I use those two helper functions constantly when trying to diagnose issues.

That's funny that you noticed $db->get. I never documented that method in the db class. I really don't use it enough to figure out why you're losing the info. I'll play with it. That doesn't seem right. I'll check back with you on that.

I'm also confused as to why Alpha 4 isn't working for you. I'm about to release 4.1 final, so I'm really interested in helping you track that down.


  Reply
#10
So query over get? That should be perfectly fine, I just like the format of the get over writing out query every time. Not worth the head ache though!

I'm running some errands to get ready for Game of Thrones but then I will jump into alpha 4 to see if I can't get a little more information on whats getting mucked up.

Thanks again!
  Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)