spanster90 Posted June 15, 2011 Share Posted June 15, 2011 I have phpMyAdmin. What I would like is if the username field for my table is empty, to have it change this field to Anonymous. Is this possible by changing the structure of this field in any way? Quote Link to comment https://forums.phpfreaks.com/topic/239451-if-field-is-empty-insert-value/ Share on other sites More sharing options...
fugix Posted June 15, 2011 Share Posted June 15, 2011 start with a query to grab the username row, then check if it is empty, then replace the empty field with the value Anonymous $query = mysql_query("SELECT username from table_name WHERE column_name = column_name") or die(mysql_error()); $row = mysql_fetch_array($query); if($row['username'] == '') { $result = mysql_query(UPDATE table_name SET username = 'Anonymous' WHERE coulumn_name = column_name) or die(mysql_error()); } Quote Link to comment https://forums.phpfreaks.com/topic/239451-if-field-is-empty-insert-value/#findComment-1230115 Share on other sites More sharing options...
mikosiko Posted June 15, 2011 Share Posted June 15, 2011 more simple: mysql_query("UPDATE table_name SET username = 'Anonymous' WHERE username IS NULL OR username = '' ") or trigger_error('Update Failed : ' . mysql_error(), E_USER_ERROR); just one query... no need to select the records first. if you only use PhpMyAdmin run the provided UPDATE in a SQL tab. you can also alter the table and define 'Anonymous' as the DEFAULT value for that field, in that way you don't need to run the UPDATE every time that a new record is inserted with that field null. Quote Link to comment https://forums.phpfreaks.com/topic/239451-if-field-is-empty-insert-value/#findComment-1230132 Share on other sites More sharing options...
fugix Posted June 15, 2011 Share Posted June 15, 2011 more simple: mysql_query("UPDATE table_name SET username = 'Anonymous' WHERE username IS NULL OR username = '' ") or trigger_error('Update Failed : ' . mysql_error(), E_USER_ERROR); just one query... no need to select the records first. if you only use PhpMyAdmin run the provided UPDATE in a SQL tab. you can also alter the table and define 'Anonymous' as the DEFAULT value for that field, in that way you don't need to run the UPDATE every time that a new record is inserted with that field null. nice, didnt think of that Quote Link to comment https://forums.phpfreaks.com/topic/239451-if-field-is-empty-insert-value/#findComment-1230133 Share on other sites More sharing options...
spanster90 Posted June 15, 2011 Author Share Posted June 15, 2011 you can also alter the table and define 'Anonymous' as the DEFAULT value for that field, in that way you don't need to run the UPDATE every time that a new record is inserted with that field null. I indeed tried this. I set it As Defined: Anonymous but I have scripts that insert to this database and when nothing is inserted to this field, it still shows up empty rather than Anonymous. The scripts work great by the way in cron. I would just prefer the default method, is there a setting I'm missing? Quote Link to comment https://forums.phpfreaks.com/topic/239451-if-field-is-empty-insert-value/#findComment-1230248 Share on other sites More sharing options...
mikosiko Posted June 16, 2011 Share Posted June 16, 2011 I will guess that is most likely because your scripts are inserting an empty string into the field ( '' ) and not a NULL value... default property is good but you always must validate that you are inserting the right thing in your fields Quote Link to comment https://forums.phpfreaks.com/topic/239451-if-field-is-empty-insert-value/#findComment-1230278 Share on other sites More sharing options...
The Little Guy Posted June 16, 2011 Share Posted June 16, 2011 Sql: ALTER TABLE `table_name` CHANGE `first_name` `first_name` CHAR( 15 ) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL DEFAULT 'Anonymous' otherwise go to the field you want to change in myAdmin, and click edit, In there set the default to Anonymous. Now when ever something gets put in there when the name column is not specified in the query it will default to the default value. Quote Link to comment https://forums.phpfreaks.com/topic/239451-if-field-is-empty-insert-value/#findComment-1230800 Share on other sites More sharing options...
nimishprabhu Posted June 16, 2011 Share Posted June 16, 2011 Try inserting NULL value instead of "" in your table.. So your query would be something like : $sql = "INSERT INTO table_name (username,address) VALUES (NULL,'".$address."') WHERE id = ".$id; Hope it helps. Quote Link to comment https://forums.phpfreaks.com/topic/239451-if-field-is-empty-insert-value/#findComment-1230805 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.