php multiple parameters in one url only if they are selected

Discussion in 'PHP' started by xbat, Aug 3, 2013.

  1. #1
    I'm trying to build a page that selects multiple parameters in one url. But I only want options to show if certain ones are selected. I'm not sure what the best way is to do this.

    I'm using something like this - http://www.w3schools.com/php/php_ajax_database.asp

    and then in the parameter I was using php?id=1&other2=

    and then I was running the query as

    query select from item WHERE id=$id AND other=$other

    Is there a better way to do this?? should I use if statements?? what I come down to is you have someone that only selects id and doesn't select other or someone who selects just other..
     
    Solved! View solution.
    xbat, Aug 3, 2013 IP
  2. #2
    You could use if statements to build your query, something like this:
    if(!empty($_POST['id']) && empty($_POST['other2'])){
    $query = "SELECT * FROM item WHERE id=$_POST['id'] LIMIT 1";
    }
    if(empty($_POST['id']) && !empty(['other2'])){
    $query = "SELECT * FROM item WHERE other=$_POST['other2'] LIMIT 1";
    }
    if(!empty['id']) && !empty(['other2'])){
    $query = "SELECT * FROM item WHERE id=$_POST['id'] AND other2=$_POST['other2'] LIMIT 1";
    }
    PHP:
     
    ekim941, Aug 4, 2013 IP
  3. xbat

    xbat Well-Known Member

    Messages:
    326
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    105
    #3
    absolutely fantastic ! :) That will help me complete what I am looking to do.
     
    xbat, Aug 4, 2013 IP
  4. HuggyStudios

    HuggyStudios Well-Known Member

    Messages:
    724
    Likes Received:
    20
    Best Answers:
    26
    Trophy Points:
    165
    #4
    HuggyStudios, Aug 4, 2013 IP
  5. xbat

    xbat Well-Known Member

    Messages:
    326
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    105
    #5
    huggyStudios yea I know about that deprecated jazz.. With what he gave me I can still work with that. Its more of the thought or the idea.. And he gave me the idea. The idea was what I was looking for not the exact code. But I'm sure your post will help others. You have some hardcore php passion and there is nothing wrong with looking out for others :)
     
    xbat, Aug 4, 2013 IP
  6. ekim941

    ekim941 Member

    Messages:
    74
    Likes Received:
    7
    Best Answers:
    7
    Trophy Points:
    33
    #6
    I'm not sure why. The question that was posted was about the if statements. The query is fine.
    Now, if the original poster had included a mysql_query() call, I would have changed it to mysqli_query() in my response.
    But I don't want to open the PDO vs mysqli can of worms.
     
    ekim941, Aug 4, 2013 IP
  7. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #7
    Though PDO's ability to pass an array might be more useful than dealing with bindparams as you could build the parameter array first, simplifying the logic.

    $data = $where = [];
    	
    if (
    	isset($_POST['id']) &&
    	!empty($_POST['id'] 
    ) {
    	$where[] = 'id = :id';
    	$data[':id'] = $_POST['id'];
    }
    
    if (
    	isset($_POST['other2'] &&
    	!empty($_POST['other2']
    ) {
    	$where[] = 'other = :other';
    	$data[':other'] = $_POST['other2'];
    }
    
    if (count($where)>0) {
    	$stmt = $db->prepare('
    		SELECT * FROM item
    		WHERE ' . implode('and', $where) . '
    		LIMIT 1
    	');
    	$stmt->execute($data);
    	
    	// handle processing the result here
    	
    } else die('You failed to enter an ID or "other" value');
    Code (markup):
    Honestly, even suggesting dumping $_POST directly into a query string is some serious herpaderp that should NEVER be encouraged... which of course is why that nube predator BS known as W3Schools advocates doing so even when they use mysqli -- which is some serious heavy duty whiskey-tango-foxtrot territory.

    But then there's a reason there's the intervention group...

    -- edit -- also a good idea to test if they are set BEFORE testing if they are empty -- avoid the warnings in the error log since some browsers will omit empty values from the submit. Also, shouldn't these be $_GET instead of $_POST since the OP said they're in the URI?
     
    Last edited: Aug 4, 2013
    deathshadow, Aug 4, 2013 IP
  8. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #8
    ... and where did ANYONE in this thread use the mysql_ functions?

    mysqli, PDO or even the deprecated mysql_ functions the worry there is dumping $_POST values directly into the query string.

    Of course, you're saying the "Mysql part of php" without specifically stating JUST the mysql_ functions -- so I'm wondering if you even know the difference since even the W3Schools article (the only part showing a query being sent) is using mysqli. MySQL access in PHP is NOT deprecated, only the insecure functions that all start with mysql_ like mysql_connect or mysql_query... they are replaced by the mysqli object, mysqli_ function (which IMHO is some dumbass ****), or PDO interfaces -- To mysql in the case of the former, or a wide variety of database engines INCLUDING MySQL using PDO.
     
    deathshadow, Aug 4, 2013 IP
  9. ekim941

    ekim941 Member

    Messages:
    74
    Likes Received:
    7
    Best Answers:
    7
    Trophy Points:
    33
    #9
    You're right, it should be $_GET instead of $_POST. I got thrown off by the mention of selecting options (which I assumed would be a form method="post").

    My understanding was that this question was about the use and structure of the "if" statements, not about the MySQL part of the script.
     
    ekim941, Aug 5, 2013 IP