Jump to content

use php to save an empty input box as NULL


Cultureshock

Recommended Posts

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.

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.

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?

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.

  • 11 months later...

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. :)

 

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.