SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    using isset within MySQL statement

    Hi Guys,

    Is it possible to do something like the following (I have not tested the code but it does not look like it will work to me):

    PHP Code:
    mysql_query("insert into table (field1) values ('".isset($_POST['field1']) ? mysql_real_escape_string($_POST['field1'])."', : 'NULL', ."')") 

  2. #2
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This should work:
    PHP Code:
    mysql_query("insert into table (field1) values ('".isset($_POST['field1']) ? mysql_real_escape_string($_POST['field1']) : NULL "')"); 
    Though i have not tested too.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  3. #3
    An average geek earl-grey's Avatar
    Join Date
    Mar 2005
    Location
    Ukraine
    Posts
    1,403
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can, but to make code readable, you may want to store to a variable:
    PHP Code:
    $field1 = isset($_POST['field1']) ? mysql_real_escape_string($_POST['field1']) : NULL;
    mysql_query("insert into table (field1) values ('$field1')"); 
    Or use the @ operator:
    PHP Code:
    mysql_query("insert into table (field1) values ('" . @$_POST['field1'] . "')"); 

  4. #4
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by rajug View Post
    This should work:
    PHP Code:
    mysql_query("insert into table (field1) values ('".isset($_POST['field1']) ? mysql_real_escape_string($_POST['field1']) : NULL "')"); 
    Though i have not tested too.
    Hmm, I just tried the above code and the MySQL error given is:

    Code:
    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '> 3' at line 1

  5. #5
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am confused that from where that greater than symbol came?? Did you enter that symbol in $POST['field1']??? Otherwise it should not give any error. Did you try to echo the sql query before executing??
    PHP Code:
    echo "insert into table (field1) values ('".isset($_POST['field1']) ? mysql_real_escape_string($_POST['field1']) : NULL "')"
    See whether this is any > symbol or not?
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  6. #6
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes the $POST['field1'] value is > 3...so how can I make it work?

  7. #7
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    I would go with earls-grey's solution purely because if you have 3 or 4 condition it would make the code very difficult to follow!

    The error is probably coming from the first isset.
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  8. #8
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes it's because the value of the $POST['field1'] is '> 3'. But I am escaping it, so not sure why it's not working?

  9. #9
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes i also could not run it in my system in the way that you want. Previously i just posted it without testing. Without any long thinking regarding this to suggest you that please go with early-grey's way. What is the problem with that way?
    PHP Code:
    $val = isset($_POST['field1']) ? mysql_real_escape_string($_POST['field1']) : NULL;
    mysql_query("INSERT INTO test SET test='$val'") or die(mysql_error()); 
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •