Jump to content

operator not supported for strings error


Sleeper

Recommended Posts

ok guys I've made a script that will allow me to move items and up and a list using weight. The page loads fine the first time, then when you click on the up or down arrow it swaps the weight values and wrights them to the data base. On the same page I have

 

$data = mysql_query("SELECT * FROM columnleft ORDER BY weight ASC") or die(mysql_error());
while($info = mysql_fetch_array($data)) {
$id[] = $info[id];
$weight[] = $info[weight];
}

 

This is allowing me to loop in the upper and lower weights to the current item. So they can be changed. However I get a

 

[17-Mar-2011 18:25:11] PHP Fatal error:  [] operator not supported for strings in dir/page on line 80

 

Line 80 is the

 

$weight[] = $info[weight];

 

So my guess is that its writing the weight to the database and trying to get an array at the same time? So it errors out. So if I'm right is there a way to prioritize so it will finish writing then load the rest?

Link to comment
Share on other sites

I should of clearly stated here, that the pages loads with no errors just fine, then when you click on the up or down arrows it dose right to the database the pages reloads to it self and then errors out on the weight array and goes white.

Link to comment
Share on other sites

although having $info['weight'] is definitely the proper way to do it and I have updated that cause I forgot about it, it dose work with out it. As I said the page loads fine and everything is brought into the array just fine. It's when I submit the form to change the weights around that it reloads the page, runs either

 

if ($moveup == yes) {
$weight=$_POST['weight2'];
$weightb=$_POST['weight'];
$data="UPDATE columnleft SET weight='".$weight."' WHERE ID='$_POST[id]' ";
if (!mysql_query($data,$con)) die('Error: ' . mysql_error());
$data="UPDATE columnleft SET weight='".$weightb."' WHERE ID='$_POST[id2]' ";
if (!mysql_query($data,$con)) die('Error: ' . mysql_error());
}
if ($movedown == yes) {
$weight=$_POST['weight2'];
$weightb=$_POST['weight'];
$data="UPDATE columnleft SET weight='".$weight."' WHERE ID='$_POST[id]' ";
if (!mysql_query($data,$con)) die('Error: ' . mysql_error());
$data="UPDATE columnleft SET weight='".$weightb."' WHERE ID='$_POST[id2]' ";
if (!mysql_query($data,$con)) die('Error: ' . mysql_error());
}

 

up or down depending on what you pick and it dose make the changes to the database. It then moves down the page to where it is asked to create the array again and that's when the error happens. I feel like its not able to create the array because the weight field is being changed at the beginning of the page. I can take the above coding out and put it on a second page like a success page and then come back to the original page and it works just fine, but I want it to stay on the same page. So it seems like I need a pause for it to finish writing before it tries to pull the info again.

Link to comment
Share on other sites

Once again I'm surprised those if statements are working, as surely it needs to read if ($moveup == "yes") (i.e. with quotes) - can't believe it would work otherwise.

 

Anyhow, if you say the code is working when you put it on another page, then you can still do that and make it appear to the user that they are on the same page, just set the form action to a seperate processing page, include your moving code here, then at the bottom of the page, put

 

  header("Location: form.html");  // OR WHATEVER YOUR OTHER PAGE IS CALLED

 

This way, to the user it appears they have never left the page.  It also stops the annoying thing or users refreshing the page and the script running again.

Link to comment
Share on other sites

It will work without the quotes, as long as there is no constant defined by the same name, and it's rather inefficient because php has to look for a constant by that name, then when it can't find one, treat the value as a string, and throw a warning. You'd see those warnings if you had error_reporting set to -1 or E_ALL and display_errors = On.

Link to comment
Share on other sites

I used the header redirect and that did the trick. Thanks.

 

as far as the ' or " in the fields. I was instructed that as long as the value isn't spaced like (==This Value) and its wasn't an add or subtract type thing that it was fine to not have them. And I do have warnings on and I don't see them. but if its that big of a deal ill switch up my habit there.

Link to comment
Share on other sites

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.