Cultureshock Posted June 4, 2010 Share Posted June 4, 2010 When I save it as is, the mysql data is saved as empty, but not as NULL. I've tried using unset and even $string=NULL, none of which work. The form data I'm saving is to be used on a profile-type page, and if a user doesn't fill out a certain field, I want the data to be saved as NULL. I'm sure the solution is something really obvious. And I can't make the query say field=NULL because the field will not always be empty. Quote Link to comment https://forums.phpfreaks.com/topic/203893-use-php-to-save-an-empty-input-box-as-null/ Share on other sites More sharing options...
dabaR Posted June 4, 2010 Share Posted June 4, 2010 And I can't make the query say field=NULL because the field will not always be empty. Can you write something like db_query("..." . (empty($field_data) ? 'NULL' : db_quote($field_data, 'text')) . "..."); Quote Link to comment https://forums.phpfreaks.com/topic/203893-use-php-to-save-an-empty-input-box-as-null/#findComment-1067911 Share on other sites More sharing options...
Cultureshock Posted June 5, 2010 Author Share Posted June 5, 2010 And I can't make the query say field=NULL because the field will not always be empty. Can you write something like db_query("..." . (empty($field_data) ? 'NULL' : db_quote($field_data, 'text')) . "..."); Well like I said the field will not always be empty (aka won't need to be null.) If someone removes data they once had, I need it to go back to the way the feild was before trhe first entered any data- Null. Quote Link to comment https://forums.phpfreaks.com/topic/203893-use-php-to-save-an-empty-input-box-as-null/#findComment-1068225 Share on other sites More sharing options...
dabaR Posted June 5, 2010 Share Posted June 5, 2010 Well like I said the field will not always be empty (aka won't need to be null.) If someone removes data they once had, I need it to go back to the way the feild was before trhe first entered any data- Null. I still think my solution will work, did you give it a shot? Quote Link to comment https://forums.phpfreaks.com/topic/203893-use-php-to-save-an-empty-input-box-as-null/#findComment-1068353 Share on other sites More sharing options...
Cultureshock Posted June 5, 2010 Author Share Posted June 5, 2010 I still think my solution will work, did you give it a shot? It works fine, but like I said the field won't always need to be emptied. I need a way to update the row, and *if need be* make a field null again. But if the update is simply changed, then the field would not be empty because it would be replaced by the new data. Quote Link to comment https://forums.phpfreaks.com/topic/203893-use-php-to-save-an-empty-input-box-as-null/#findComment-1068384 Share on other sites More sharing options...
dabaR Posted June 5, 2010 Share Posted June 5, 2010 Sorry, I don't understand. Are you saying that it does not save a value when a value is provided? Quote Link to comment https://forums.phpfreaks.com/topic/203893-use-php-to-save-an-empty-input-box-as-null/#findComment-1068387 Share on other sites More sharing options...
Cultureshock Posted June 5, 2010 Author Share Posted June 5, 2010 No, it does; But when a value needs to be NULL, it instead is "" - just blank. Even after using empty() and setting the value to NULL. Mysql saves it as "". Quote Link to comment https://forums.phpfreaks.com/topic/203893-use-php-to-save-an-empty-input-box-as-null/#findComment-1068418 Share on other sites More sharing options...
fenway Posted June 5, 2010 Share Posted June 5, 2010 Huh? Quote Link to comment https://forums.phpfreaks.com/topic/203893-use-php-to-save-an-empty-input-box-as-null/#findComment-1068419 Share on other sites More sharing options...
Cultureshock Posted June 6, 2010 Author Share Posted June 6, 2010 Huh? mysql saves it as blank instead of as Null? And I'd like it to be Null if the data provided is left blank? Quote Link to comment https://forums.phpfreaks.com/topic/203893-use-php-to-save-an-empty-input-box-as-null/#findComment-1068427 Share on other sites More sharing options...
fenway Posted June 7, 2010 Share Posted June 7, 2010 Huh? mysql saves it as blank instead of as Null? And I'd like it to be Null if the data provided is left blank? Then show me the line that you think is translating this incorrectly. Quote Link to comment https://forums.phpfreaks.com/topic/203893-use-php-to-save-an-empty-input-box-as-null/#findComment-1068924 Share on other sites More sharing options...
buskyboy Posted May 8, 2011 Share Posted May 8, 2011 Did you ever get this problem solved I have a similar one. problem removing image from database I hope someone can help me. I have a problem that I cannot solve after 3 days. I have a form with a few text fields, into which a url is added for an image. I can get the image addresses back out of the database and display them. no problem. My problem is when I update the the form text are and try to insert nothing into the Db it always inserts a black string. i have tried to convert these stings to null values but to no avail. any thoughts would be appreciated. Here is some of code For inserting to DB Code: Select all <?php include_once('connect_to_database.php'); if(isset($_POST['save'])) { $issue= $_POST['issue']; $title = $_POST['title']; $content = nl2br($_POST['editor1']); $pic1 = $_POST['pic1']; $pic2 = $_POST['pic2']; $pic3 = $_POST['pic3']; if(!get_magic_quotes_gpc()) { $issue = addslashes($issue); $title = addslashes($title); $content = addslashes($content); } $query="INSERT INTO `newsletter_apm` (`issue_nos` , `story_title` , `story` ,`url1` ,`url2` ,`url3` ) VALUES('$issue','$title', '$content','$pic1','$pic2','$pic3')"; $result=mysql_query($query) or die("Died inserting info into db. Error returned if any: ".mysql_error()); echo "Article '$title' added"; } ?> This works fine. Code for updating. Code: Select all <?php if(isset($_POST['save'])) { $issue= $_POST['issue']; $title = $_POST['title']; $content = ($_POST['editor1']); $pic1 = $_POST['pic1']; $pic2 = $_POST['pic2']; $pic3 = $_POST['pic3']; if(!get_magic_quotes_gpc()) { $issue = addslashes($issue); $title = addslashes($title); $content = addslashes($content); } if ($pic2==""){ $pic2=null;} if ($pic3==""){ $pic3=null;} //$query="UPDATE `newsletter_apm` SET `issue_nos` , `story_title` , `story` ,`url1` ,`url2` ,`url3` $query="UPDATE `newsletter_apm` SET issue_nos ='$issue ' Where id=$id"; $result=mysql_query($query) or die("Died inserting info into db. Error returned if any: ".mysql_error()); $query="UPDATE `newsletter_apm` SET story_title ='$title ' Where id=$id"; $result=mysql_query($query) or die("Died inserting info into db. Error returned if any: ".mysql_error()); $query="UPDATE `newsletter_apm` SET story ='$content ' Where id=$id"; $result=mysql_query($query) or die("Died inserting info into db. Error returned if any: ".mysql_error()); $query="UPDATE `newsletter_apm` SET url1 ='$pic1 ' Where id=$id"; $result=mysql_query($query) or die("Died inserting info into db. Error returned if any: ".mysql_error()); $query="UPDATE `newsletter_apm` SET url2 ='$pic2 ' Where id=$id"; $result=mysql_query($query) or die("Died inserting info into db. Error returned if any: ".mysql_error()); $query="UPDATE `newsletter_apm` SET url3 ='$pic3 ' Where id=$id"; $result=mysql_query($query) or die("Died inserting info into db. Error returned if any: ".mysql_error()); echo "Article '$title' has been updated"; } ?> This will update with new urls no problem but if I delete one leaving a blank it inserts an blank string. code for diaplaying pics is repeating block Code: Select all <tr> <td valign="top" bgcolor="#FF0000" ><!-- // Begin Module: Standard Preheader \ --> <table width="100%" border="0" cellpadding="10" cellspacing="0" > <tbody><tr> <td valign="top" bgcolor="#FFFFFF" > <h4 style="color:#336699; font-size:18pt; font-style:bold;line-height;8pt;font-family:Verdana;margin:0pt;padding-bottom:0pt" ><?php echo $storyheading[$i]; ?></h4> <h2 style="font-family:Verdana; font-weight:lighter; font-style:inherit; font-size:8pt; margin:0pt;"><?php echo $story[$i] ;?></h2></td> <td width="200" valign="top" bgcolor="#FFFFFF" style="background-color:#336699; " > <?php //loop through the pics and only display if there is one in the database to make sure thet a placeholder does not show. if ( $pic[$i ]!= ''){ echo "<img src= $pic[$i] name='pic1' width='200' id='logo26' /><br><br>"; } if ( trim($pic1[$i] !='')){ echo "<img src= $pic1[$i] name='pic1' width='200' /><br><br>"; } if ( $pic2[$i] != ''){ echo "<img src= $pic2[$i] name='pic1' width='200' />"; } ?> </td> </tr> I am trying to make a dynamic mailshot with any number of stories. you can check out a version at http://www.pat-trick.com.au/emailshot/a ... hp?issue=1 the problem is picture placer shows in safari, chrome and most email clients. I hope i make the issue clear. Thanking you in advance. Quote Link to comment https://forums.phpfreaks.com/topic/203893-use-php-to-save-an-empty-input-box-as-null/#findComment-1212297 Share on other sites More sharing options...
fenway Posted May 13, 2011 Share Posted May 13, 2011 You've resurrected a thread from a year ago -- start a new topic. Quote Link to comment https://forums.phpfreaks.com/topic/203893-use-php-to-save-an-empty-input-box-as-null/#findComment-1214947 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.