How to not insert a blank value into the database

Results 1 to 11 of 11
  1. #1
    DRGunZ 2 Creator wesman2232 is offline
    MemberRank
    Jan 2007 Join Date
    Erie, PALocation
    4,872Posts

    How to not insert a blank value into the database

    PHP Code:
    <?php
           $clanname 
    anti_injection($_POST['clanname']);
           
    $getclanstats mssql_query("SELECT EmblemUrl FROM Clan WHERE Name = '$clanname'");
           
    $getx mssql_fetch_assoc($getclanstats);
        if(
    anti_injection($_POST['emblem']) == "")
        {
            
    $emblem $getx['EmblemUrl'];
        }
        elseif(
    $getx['EmblemUrl'] == "NULL")
        {
            
    $emblem "NULL";
        }
        else
        {
            
    $emblem anti_injection($_POST['emblem']);
        }
    ?>
    How do I get this to not insert a blank value into the database if the value in there is NULL when the user input is blank, as to not change it?
    Last edited by wesman2232; 24-12-12 at 09:12 AM.


  2. #2

    Re: How to not insert a blank value into the database

    Not sure how you're using $emblem:

    PHP Code:
    <?php
        $clanName 
    anti_injection($_POST['clanname']);
        
    $clanQuery mssql_query("SELECT EmblemUrl FROM Clan WHERE Name = '$clanName'");
        
    $clanData mssql_fetch_assoc($clanQuery);
        
    $emblemPost anti_injection($_POST['emblem']);
        
    $emblemDB null;
        
        if (
    $emblemPost == "" || $emblemPost == null)
            
    $emblemDB $clanData['EmblemUrl'];
        if (
    $clanData['EmblemUrl'] == null || $clanData['EmblemUrl'] == "")
            
    $emblemDB NULL;

        if (
    $emblemDB != null)
        {
            
    // use $emblemDB
        
    }
    ?>
    It's been a long time since I've ever written PHP code for mssql queries..
    So if there are any mistakes, feel free to correct me.

  3. #3
    DRGunZ 2 Creator wesman2232 is offline
    MemberRank
    Jan 2007 Join Date
    Erie, PALocation
    4,872Posts

    Re: How to not insert a blank value into the database

    PHP Code:
              <div class="adm_tit"><?php echo $admingmp['53']; ?></div>
          <form action="index.php?act=admin&amp;do=clans" method="post">
          <table width="350" align="center" height="60" border="0" style="font-weight:bold; color:#FF3300;">
           <tr><td><?php echo $admingmp['42'].' :'?></td><td><input type="text" name="clanname" class="tbl_colbor" /></td></tr>
           <tr><td><?php echo $admingmp['54'].' :'?></td><td><input type="text" name="wins" class="tbl_colbor" /></td></tr>
           <tr><td><?php echo $admingmp['55'].' :'?></td><td><input type="text" name="losses" class="tbl_colbor" /></td></tr>
           <tr><td><?php echo $admingmp['56'].' :'?></td><td><input type="text" name="draws" class="tbl_colbor" /></td></tr>
           <tr><td><?php echo $admingmp['57'].' :'?></td><td><input type="text" name="points" class="tbl_colbor" /></td></tr>
           <tr><td><?php echo $admingmp['179'].' :'?></td><td><input type="text" name="emblem" class="tbl_colbor" /></td></tr>
           <tr><td></td><td width="130" align="right"><input type="submit" name="changestats" value="<?php echo $admingmp['4']; ?>" /></td></tr>
          </table>
          </form>
          <div class="tbl_errmes">
          <?php
          
    if(isset($_POST['changestats'])){
           
    $clanname anti_injection($_POST['clanname']);
           
    $getclanstats mssql_query("SELECT Wins, Losses, Draws, Point, EmblemUrl FROM Clan WHERE Name = '$clanname'");
           
    $getx mssql_fetch_assoc($getclanstats);
        if(
    anti_injection($_POST['wins']) == "")
        {
           
    $wins $getx['Wins'];
        }
        else
        {
               
    $wins anti_injection($_POST['wins']);
        }
                if(
    anti_injection($_POST['losses']) == "")
            {
               
    $losses $getx['Losses'];
            }
            else
            {
               
    $losses anti_injection($_POST['losses']);
            }
                if(
    anti_injection($_POST['draws']) == "")
                {
                   
    $draws $getx['Draws'];
                }
                else
                {
                       
    $draws anti_injection($_POST['draws']);
                }
            if(
    anti_injection($_POST['points']) == "")
            {
               
    $points $getx['Point'];
            }
            else
            {
                   
    $points anti_injection($_POST['points']);
            }
        if(
    anti_injection($_POST['emblem']) == "")
        {
           
    $emblem $getx['EmblemUrl'];
        }
        elseif(
    $getx['EmblemUrl'] == "NULL")
        {
           
    $emblem "NULL";
        }
        else
        {
               
    $emblem anti_injection($_POST['emblem']);
        }
            
    $sql mssql_query("SELECT Name FROM Clan WHERE Name = '$clanname'");
            if(!
    $clanname){
             echo 
    $admingmp['60'];
            }elseif(
    mssql_num_rows($sql)<>0){
            
    $sql2 mssql_query("UPDATE Clan SET Point='$points', Wins='$wins', Losses='$losses', Draws='$draws', EmblemUrl='$emblem' WHERE Name='$clanname'");
             echo 
    $admingmp['59'];
            }else{
              echo 
    $admingmp['58'];
            }
          }
          
    ?>
    Here's the full code for you.
    This is what's happening:
    When nothing is set in Emblem input, it takes whatever data that is already in the database and uses it in the update query as to not reset anything to 0.
    Say I put nothing in the emblem input because I don't want to update it and the database value for Clan's EmblemUrl is NULL, it takes the NULL value, but sets it as a blank value when updating the row.
    Last edited by wesman2232; 24-12-12 at 10:18 AM.

  4. #4
    Praise the Sun! Solaire is offline
    MemberRank
    Dec 2007 Join Date
    Undead BurgLocation
    2,862Posts

    Re: How to not insert a blank value into the database

    Check if mssql_num_rows returns at least more than 1 row or verify whether $getx is false or not. If it's false, there's no returned row from previous query. Also, NULL values in the database equal the PHP null value, or at least MySQL did. "" is an empty string and is not the same as null.

  5. #5
    DRGunZ 2 Creator wesman2232 is offline
    MemberRank
    Jan 2007 Join Date
    Erie, PALocation
    4,872Posts

    Re: How to not insert a blank value into the database

    Quote Originally Posted by Wizkidje View Post
    Check if mssql_num_rows returns at least more than 1 row or verify whether $getx is false or not. If it's false, there's no returned row from previous query. Also, NULL values in the database equal the PHP null value, or at least MySQL did. "" is an empty string and is not the same as null.
    Yeah. Let's say I don't have anything in the EmblemUrl for my clan in the DB because it isn't set, so the value is NULL.
    I'm using this script to update some other parts of my clan (Wins, Losses, etc) but I don't want to update Emblem yet.
    I use the script and set everything except Emblem. It works for everything I set, but Emblem is now instead of NULL, it is blank.

    Should I set it to it's own individual if else statement? If $getx['EmblemUrl'] == NULL then $emblem = "NULL" else $emblem = $_POST['emblem'] ?

  6. #6
    Praise the Sun! Solaire is offline
    MemberRank
    Dec 2007 Join Date
    Undead BurgLocation
    2,862Posts

    Re: How to not insert a blank value into the database

    Quote Originally Posted by wesman2232 View Post
    Yeah. Let's say I don't have anything in the EmblemUrl for my clan in the DB because it isn't set, so the value is NULL.
    I'm using this script to update some other parts of my clan (Wins, Losses, etc) but I don't want to update Emblem yet.
    I use the script and set everything except Emblem. It works for everything I set, but Emblem is now instead of NULL, it is blank.

    Should I set it to it's own individual if else statement? If $getx['EmblemUrl'] == NULL then $emblem = "NULL" else $emblem = $_POST['emblem'] ?
    You should use something similar to this:

    "UPDATE Clan SET EmblemURL = " . ($getx['EmblemUrl'] == null ? "NULL" : "'" . $_POST['emblem'] . "'") so it'll turn into either EmblemURL = NULL or EmblemURL = '(emblem url)'.

    Also, maybe simply removing the EmblemURL from the query syntax when $getx['EmblemURL'] is null might work as well, dependant on how you want it.

  7. #7
    DRGunZ 2 Creator wesman2232 is offline
    MemberRank
    Jan 2007 Join Date
    Erie, PALocation
    4,872Posts

    Re: How to not insert a blank value into the database

    Quote Originally Posted by Wizkidje View Post
    You should use something similar to this:

    "UPDATE Clan SET EmblemURL = " . ($getx['EmblemUrl'] == null ? "NULL" : "'" . $_POST['emblem'] . "'") so it'll turn into either EmblemURL = NULL or EmblemURL = '(emblem url)'.

    Also, maybe simply removing the EmblemURL from the query syntax when $getx['EmblemURL'] is null might work as well, dependant on how you want it.
    The latter would be the best way to go at it as so I don't complicate the problem any more.
    And since I'm still learning as I go on PHP, what is the ? used for in the query?
    " . ($getx['EmblemUrl'] == null ? "NULL" : "'" . $_POST['emblem'] . "'

  8. #8
    Pee Aitch Pee Dave is offline
    MemberRank
    Mar 2011 Join Date
    The NetherlandsLocation
    722Posts

    Re: How to not insert a blank value into the database

    The ? basically means "if true".
    And the : means "else".

    PHP Code:
    if($getx['EmblemUrl'] == null)
    {
      return 
    'NULL';
    }
    else
    {
      return 
    "'" $_POST['emblem'] . "'";

    PHP Shorthand If/Else Using Ternary Operators (?:)

  9. #9
    Hi, I'm Omar! Vusion is offline
    MemberRank
    Jan 2011 Join Date
    HereLocation
    1,658Posts

    Re: How to not insert a blank value into the database

    Quote Originally Posted by wesman2232 View Post
    The latter would be the best way to go at it as so I don't complicate the problem any more.
    And since I'm still learning as I go on PHP, what is the ? used for in the query?
    " . ($getx['EmblemUrl'] == null ? "NULL" : "'" . $_POST['emblem'] . "'
    It's a ternary operation, it has nothing to do with the query.

    ((condition) ? true : false)

    Trying to understand what you're trying to do but;
    Code:
    <?php
    $emblem = ((anti_injection($_POST['emblem'])) ? ", EmblemURL = '".$anti_injection($_POST['emblem'])."'" : "");
    $sql2 = mssql_query("UPDATE Clan SET Point='$points', Wins='$wins', Losses='$losses', Draws='$draws' ".$emblem." WHERE Name='$clanname'");
    ?>
    I'm aware of how messed up that is, but I have a bad headache.
    Last edited by Vusion; 26-12-12 at 01:19 PM.

  10. #10
    Praise the Sun! Solaire is offline
    MemberRank
    Dec 2007 Join Date
    Undead BurgLocation
    2,862Posts

    Re: How to not insert a blank value into the database

    Quote Originally Posted by SuperWaffle View Post
    The ? basically means "if true".
    And the : means "else".

    PHP Code:
    if($getx['EmblemUrl'] == null)
    {
      return 
    'NULL';
    }
    else
    {
      return 
    "'" $_POST['emblem'] . "'";

    PHP Shorthand If/Else Using Ternary Operators (?:)
    This, except it doesn't necessarily return the operands so you might as well do a statement or anything else.

    As for your query, something like this would be best:
    UPDATE Clan SET Points '$points' " . ($getx['EmblemUrl'] != null ? ", EmblemURL = '" . $_POST['emblem'] . "'" : ""), Wins = '$Wins', etc.

  11. #11
    DRGunZ 2 Creator wesman2232 is offline
    MemberRank
    Jan 2007 Join Date
    Erie, PALocation
    4,872Posts

    Re: How to not insert a blank value into the database

    Thanks guys for the help. I got it working the way I wanted to last night by basically removing the update to EmblemUrl if the input is blank. I do like all the other answers, as I didn't know you could do it like that. I'll use this for future reference.



Advertisement