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?

 

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'

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>";
    }
}

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.

Archived

This topic is now archived and is closed to further replies.

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