Lumio Posted April 13, 2007 Share Posted April 13, 2007 Hello! I made a real big user-table with many columns. So I wan't to make a function that sets a line to the default. For instance: I got a table like user_id, user_name (default = ""), user_password (default = ""), user_getpms (default = 1) So how can I update a line where user_id = 2 (for example) without writing every column. But the user_id should be the same like it was before. Do anyone understand me? Quote Link to comment https://forums.phpfreaks.com/topic/46849-solved-set-all-to-default/ Share on other sites More sharing options...
tauchai83 Posted April 13, 2007 Share Posted April 13, 2007 i have no idea about what are you typing here. Can be more detailed? post your code that you wrote. Quote Link to comment https://forums.phpfreaks.com/topic/46849-solved-set-all-to-default/#findComment-228356 Share on other sites More sharing options...
bubblegum.anarchy Posted April 13, 2007 Share Posted April 13, 2007 I am not sure but I think default values apply only during inserts... consider this: DELETE FROM table_name WHERE user_id = $id; INSERT INTO table_name SET user_id = $id; The default values should be set during the insert so make sure all your column definitions have appropriate default values. Quote Link to comment https://forums.phpfreaks.com/topic/46849-solved-set-all-to-default/#findComment-228377 Share on other sites More sharing options...
MadTechie Posted April 13, 2007 Share Posted April 13, 2007 Not sure if your looking for something like this but it was fun to create <?php $theStuff =array("Name" => "NewName", "Age" = > "15"); SetToDatabase($theStuff); function SetToDatabase($theValue) { $table = "Test"; //Get list of Fields $result = mysql_query("DESCRIBE `".$table."`"); $Data = array(); while ($row = mysql_fetch_array($result,MYSQL_NUM)) { if( isset($theValue[$row[0]]) ) { $Data[$row[0]] = $theValue[$row[0]]; }else{ $Data[$row[0]] = ""; //empty unset values } } //Insert // $Fields = array_keys($Data); // $TheSet = "(".implode(",",$Fields).")"; // $TheValue = "(".implode(",",$Data).")"; // $SQLq = "INSERT INTO $table $TheSet VALUES $TheValue;" //Update $UPDATE = "UPDATE $table SET "; foreach($Fields as $F => $V) { $UPDATE .= "`$F` = '$V'"; } $SQLq = $UPDATE." " //<-- Where Blar $result = mysql_query($SQLq ); } ?> it probably has a few bugs as its written from the top of myhead but .. yeah Quote Link to comment https://forums.phpfreaks.com/topic/46849-solved-set-all-to-default/#findComment-228388 Share on other sites More sharing options...
Lumio Posted April 13, 2007 Author Share Posted April 13, 2007 Thanks! I think that's it, what I was looking for Thanks to all Quote Link to comment https://forums.phpfreaks.com/topic/46849-solved-set-all-to-default/#findComment-228425 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.