0

I have created database with more column. in view.php i have defined inputs like:

<?php
if(isset($_POST["submit"])){
$name = $_POST["name"];
$valueA = $_POST["valueA"];
$valueB = $_POST["valueB"];
$valueC = $_POST["valueC"];
$valueD = $_POST["valueD"];
...

some inputs may be empty and this sql commad is not function:

$sqlCommand = "SELECT * FROM `tableG` WHERE `columnA` = '$valueA ' AND `columnB` = '$valueB' AND `columnC` = '$valueC' AND `columnD` = '$valueD' ORDER BY `tableG`.`columnA` ASC";

How i can buil sql command automaticaly by not empty inputs?

Thanks for help

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
Icecko
  • 3
  • 3
  • Your query is a string. so you can build it gradually. Start with the fixed part, and then only append other parts if you need them. Test whether the input is empty before deciding whether to append that bit of the SQL to the string or not. And please use parameterised queries and prepared statements. What you have shown above is very naive and vulnerable to SQL injection attacks – ADyson Dec 31 '20 at 13:07
  • I do not see solution in attached link. What happend when some some inputs is empty. – Icecko Dec 31 '20 at 14:33
  • That answer is providing a more generic and flexible object oriented solution. I didn't suggest it. If you follow my suggestion above you will get something more like what you are thinking of. Did you try my idea? – ADyson Dec 31 '20 at 14:44
  • 1
    When creating the query string dynamically in the loop, **you can add a simple condition** and only add those fields which are not empty – Your Common Sense Dec 31 '20 at 15:15
  • 1
    Please do not edit solution announcements into the question. Create yourself an answer instead or accept one of the existing answers, which is not possible for being closed as duplicate. Consider contacting the user who closed it to point out how this is different from the proposed duplicate. If you can you can also edit the question to clarify the difference and it might be reopened, get helpful answers or at least allow you to add your own solution. – Yunnosch Jan 04 '21 at 14:49

0 Answers0