Jump to content

php - mysql not doing what i want it to do


patawic

Recommended Posts

function defaultstyle($id)
{
$theme = mysql_query("SELECT * FROM themes WHERE id = '".$id."'");
$count = mysql_num_rows(mysql_query("SELECT * FROM themes"));
for($i=0;$i<$count;$i++){
mysql_query("UPDATE themes SET enabled = '1' WHERE id = '".$i."'");
}
	while($style = mysql_fetch_array($theme))
	{ if($style[enabled] == '1') { 
		echo "<center>This theme is already set to default</center>";
	} else { 
		echo "<center>The theme $style[name] is now the default theme for the CMS</center>"; 
		mysql_query("UPDATE themes SET enabled = '1' WHERE id = '".$id."'");
	}
}
}

 

The updating part where it set's enabled to "1" works fine, but the code before it

for($i=0;$i<$count;$i++){
mysql_query("UPDATE themes SET enabled = '1' WHERE id = '".$i."'");
}

That code is meant to load every mysql record and set the "enabled" row to 0, but its not doing that.

 

How can i fix this?

 

Link to comment
Share on other sites

wait nevermind, stupid mistake

for($i=0;$i<$count;$i++){
mysql_query("UPDATE themes SET enabled = '1' WHERE id = '".$i."'");
}

should of been

for($i=0;$i<$count;$i++){
mysql_query("UPDATE themes SET enabled = '0' WHERE id = '".$i."'");
}

Link to comment
Share on other sites

The updating part where it set's enabled to "1" works fine, but the code before it

for($i=0;$i<$count;$i++){
mysql_query("UPDATE themes SET enabled = '1' WHERE id = '".$i."'");
}

That code is meant to load every mysql record and set the "enabled" row to 0, but its not doing that.

 

How can i fix this?

 

By learning how to do it the right way. There is absolutely no need to do a loop. Besides, that's a pretty lame attempt at trying to update all the records since it would be possible for an id number to be skipped if you were ever to delete a record. And, that just might be your problem. To update every record, simply leave off the WHERE clause

 

UPDATE themes SET enabled = '0'

Link to comment
Share on other sites

Persoanlly, I think determining if the style has already been seleted previously has no value. Just state that the selected ID has been set. But, in any event, here is a better way to do what you are after

function defaultstyle($id)
{
    //Set new style to enabled
    mysql_query("UPDATE themes SET enabled = '1' WHERE id = '{$id}'");

    //Set all others to disabled
    mysql_query("UPDATE themes SET enabled = '0' WHERE id <> '{$id}'");

    //Determine if the new id was already set
    if(mysql_affected_rows()==0)
    {
        echo "<center>This theme is already set to default</center>";
    }
    else
    {
        echo "<center>The theme $style[name] is now the default theme for the CMS</center>";
    }
}

Link to comment
Share on other sites

The updating part where it set's enabled to "1" works fine, but the code before it

for($i=0;$i<$count;$i++){
mysql_query("UPDATE themes SET enabled = '1' WHERE id = '".$i."'");
}

That code is meant to load every mysql record and set the "enabled" row to 0, but its not doing that.

 

How can i fix this?

 

By learning how to do it the right way. There is absolutely no need to do a loop. Besides, that's a pretty lame attempt at trying to update all the records since it would be possible for an id number to be skipped if you were ever to delete a record. And, that just might be your problem. To update every record, simply leave off the WHERE clause

 

UPDATE themes SET enabled = '0'

 

 

doing that didnt work. it didnt update the record.

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.