在我尝试为我在本科论文中创建的数据库创建一个搜索工具时,我需要对用户输入的值进行大量检查,并根据这些值生成并执行适当的MySQL查询
。示例如下:
HTML代码
(part of the whole table) <tr id="tablerow" class=""> <th id="selectth"> <select class="selection" name="Attributes" id="Attributes"> <!-- List of all the available attributes for the user to select for the searching queries --> <option disabled>Options</option> <option value="Name" id="p_Name">Name</option> <option value="ID" id="p_Id">Patient ID</option> <option value="Sex" id="p_Sex">Sex</option> <option value="Email" id="p_Email">Patient Email</option> <option value="Age" id="p_Age">Age ></option> <option value="Agesmaller">Age < </option> <option value="Race" id="p_Race">Race</option> <option value="PhoneNumber" id="p_Phonenum">Phone Number</option> <option value="Comorbidities" id="p_Comorbidities">Comorbidities</option> <option value="EDSS" id="p_eddsscore">EDSS Score</option> <option value="Pregnant" id="p_Pregnant">Is Pregnant</option> <option value="Onsetlocalisation" id="p_Onsetlocalisation">Onset Localisation</option> <option value="Smoker" id="p_Smoker">Is a Smoker</option> <option value="onsetsymptoms" id="p_onsetsymptoms">Onset Symptoms</option> <option value="MRIenhancing" id="p_MRIenhancing">MRI Enhancing Lesions</option> <option value="MRInum" id="p_MRInum">MRI Lesion No.</option> <option value="MRIonsetlocalisation" id="p_MRIonsetlocalisation">MRI Onset Localisation</option> </select> </th>
作为同一个表的另一部分,我有一个按钮,允许用户创建第二行输入,以便进行更具体的搜索,具有 2 个属性,使用 AND 或 OR 语句。
<button type="button" id="new_row_btn" onclick="addRow()" name="new_row">添加额外行</button>
我想保留这篇文章的排序以便收到答案,所以我不会包含 addRow() JavaScript 函数,但如果有必要请评论它,我将编辑尽快。
我的问题如下,我有这两个输入,我想检查用户是否在第二行输入(在“高级”输入中)输入了任何内容,然后根据新值执行适当的操作查询。
目前的 PHP 代码看起来像这样:
PHP代码
if($option == 'Name' && $newoption == 'Email' && !empty($newEmail)){ $sql = "SELECT patients.Patient_id,patients.Patient_name,patients.DOB,patients.Phonenum,patients.Email,MSR.Sex FROM patients,MSR WHERE patients.Patient_id = MSR.NDSnum AND Doctor_ID = $usersid AND patients.Email = '$newEmail' $and_or patients.Patient_name LIKE '%$entry%' ORDER BY Patient_id"; $result = $pdo->query($sql); if ($result->rowCount() > 0) { while ($row = $result->fetch()) { ?> <table id="standard"> <tr> <th>Patient ID</th> <th>Name</th> <th>Date of Birth</th> <th>Phone Number</th> <th>Email</th> <th>Sex</th> <th>Previous Visits</th> </tr> <tr> <td><?php echo $row['Patient_id']; ?></td> <td> <?php echo $row['Patient_name']; ?> </td> <td><?php echo $row['DOB']; ?></td> <td><?php echo $row['Phonenum']; ?></td> <td><?php echo $row['Email']; ?></td> <td><?php echo $row['Sex']; ?></td> <td><?php echo "<a href='/application/previousvisit-bootstrap.php?id=" . $row['Patient_id'] . "'>Previous Visits</a>"; ?></td> </tr> </table> <div class="line"></div> <?php } } else { echo "No patient exists with this information. Name+Email"; } } if ($option == 'Name' && $newoption == 'Age' && !empty($newAge)){ $sql = "SELECT * FROM patients WHERE timestampdiff(year,dob,curdate()) > '$entry' $and_or patients.Patient_name LIKE '%$entry%' AND Doctor_ID = $usersid ORDER BY Patient_id"; $result = $pdo->query($sql); if ($result->rowCount() > 0) { while ($row = $result->fetch()) { ?> <table id="standard"> <tr> <th>Patient Id</th> <th>Patient Name</th> <th>Date of Birth</th> <th>Phone Number</th> <th>Email</th> <th>Previous Visits</th> </tr> <tr> <td><?php echo $row['Patient_id']; ?></td> <td> <?php echo $row['Patient_name']; ?> </td> <td><?php echo $row['DOB'] ?></td> <td><?php echo $row['Phonenum']; ?></td> <td><?php echo $row['Email']; ?></td> <td><?php echo "<a href='/application/previousvisit-bootstrap.php?id=" . $row['Patient_id'] . "'>Previous Visits</a>"; ?></td> </tr> </table> <div class="line"></div> <?php } } else { echo "No patient exists with this information. Name+Age"; }
上面的 if 语句会持续大约 20 次...每次都会检查一个属性与另一个属性的关系..例如
Name newID
或 ID newName
或 Email newSex
显然我知道我还没有完成,并且这些属性有数百种可能的组合,但我想知道是否有某种方法可以避免这一切并使其变得更容易......
很抱歉问了这么长的问题!欢迎任何有关如何使这个问题变得更好的注释。
提前感谢您抽出时间。
P粉2390894432024-04-04 15:29:02
也许可以将相同的 IF 条件组合在一起?
这是我推荐的东西:
if ($option == 'Name') { if ($newoption == 'Email' && !empty($newEmail)) { //... } if ($newoption == 'Age' && !empty($newAge)) { //... } }
少一点重复:
if ($option == 'Name') { // use this 'if', if $newoption correlates to which child if is populated... if ($newoption == 'Email' || $newoption == 'Age') { if (!empty($newEmail)) { //... } if (!empty($newAge)) { //... } } }