Chris721 Posted December 17, 2014 Share Posted December 17, 2014 I am new to php and html, I am trying to make a search function where you can search by a persons firstname or lastname but I am getting an error. the error is Notice: Undefined variable: searchq in C:\wamp\www\search2.php on line 17. Can anyone help me please <title>Applicant Data view</title> <p> <center><img src="cyberwarfareimage1.png" alt="Squadron logo" style="width:200px;height:200px" style="middle"></center> <table border="1"> <tr> <td><a href="index.php"> Home Page </a></td> <td><a href="administratorhomepage.html">Administrator Home Page </a></td> <td><a href="viewhomepage.html">View Home Page </a></td> </table> </p> <?php include "connection.php"; $sql = ("SELECT * FROM applicant WHERE `Firstname` LIKE $searchq OR `Lastname` LIKE $searchq") or die("Could Not Search"); $result = $dbhandle->query($sql); echo "<table border='1'> <tr> <th>Applicant Id</th> <th>Firstame</th> <th>Lastname</th> <th>Address</th> <th>Date Of Birth</th> <th>Contact Number</th> <th>Next Of Kin</th> <th>Next Of Kin Address</th> <th>Vetting Date</th> <th>Clearance Expiry</th> <th>Current Employer</th> <th>Date Applied</th> <th>Resubmitting</th> <th>Number Of Previous Attempts</th> <th>Why</th> <th>Qualificaton 1</th> <th>Date Completed</th> <th>Expiry Date</th> <th>Qualificaton 2</th> <th>Date Completed</th> <th>Expiry Date</th> <th>Qualificaton 3</th> <th>Date Completed</th> <th>Expiry Date</th> <th>Qualificaton 4</th> <th>Date Completed</th> <th>Expiry Date</th> <th>Qualificaton 5</th> <th>Date Completed</th> <th>Expiry Date</th> <th>Qualificaton 6</th> <th>Date Completed</th> <th>Expiry Date</th> <th>Qualificaton 7</th> <th>Date Completed</th> <th>Expiry Date</th> <th>Qualificaton 8</th> <th>Date Completed</th> <th>Expiry Date</th> <th>Qualificaton 9</th> <th>Date Completed</th> <th>Expiry Date</th> <th>Qualificaton 10</th> <th>Date Completed</th> <th>Expiry Date</th> </tr>"; if(isset ($_POST["submit"])) { $searchq = $_POST[`submit`]; $searchq = preg_replace("#[^a-z]#i","",$searchq); if ($result->num_rows > 0) { // output data of each row while($row = mysqli_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['Applicant_ID'] . "</td>"; echo "<td>" . $row['Firstname'] . "</td>"; echo "<td>" . $row['Lastname'] . "</td>"; echo "<td>" . $row['Address'] . "</td>"; echo "<td>" . $row['DOB'] . "</td>"; echo "<td>" . $row['Telephone_No'] . "</td>"; echo "<td>" . $row['NOK'] . "</td>"; echo "<td>" . $row['NOK_Address'] . "</td>"; echo "<td>" . $row['Vetting_Date'] . "</td>"; echo "<td>" . $row['Clearance_Expiry'] . "</td>"; echo "<td>" . $row['Current_Employer'] . "</td>"; echo "<td>" . $row['Date_applied'] . "</td>"; echo "<td>" . $row['Resubmission'] . "</td>"; echo "<td>" . $row['Number_of_Attempts'] . "</td>"; echo "<td>" . $row['Why'] . "</td>"; echo "<td>" . $row['Qual_1'] . "</td>"; echo "<td>" . $row['Date_Completed_1'] . "</td>"; echo "<td>" . $row['Run_out_date_1'] . "</td>"; echo "<td>" . $row['Qual_2'] . "</td>"; echo "<td>" . $row['Date_Completed_2'] . "</td>"; echo "<td>" . $row['Run_out_date_2'] . "</td>"; echo "<td>" . $row['Qual_3'] . "</td>"; echo "<td>" . $row['Date_Completed_3'] . "</td>"; echo "<td>" . $row['Run_out_date_3'] . "</td>"; echo "<td>" . $row['Qual_4'] . "</td>"; echo "<td>" . $row['Date_Completed_4'] . "</td>"; echo "<td>" . $row['Run_out_date_4'] . "</td>"; echo "<td>" . $row['Qual_5'] . "</td>"; echo "<td>" . $row['Date_Completed_5'] . "</td>"; echo "<td>" . $row['Run_out_date_5'] . "</td>"; echo "<td>" . $row['Qual_6'] . "</td>"; echo "<td>" . $row['Date_Completed_6'] . "</td>"; echo "<td>" . $row['Run_out_date_6'] . "</td>"; echo "<td>" . $row['Qual_7'] . "</td>"; echo "<td>" . $row['Date_Completed_7'] . "</td>"; echo "<td>" . $row['Run_out_date_7'] . "</td>"; echo "<td>" . $row['Qual_8'] . "</td>"; echo "<td>" . $row['Date_Completed_8'] . "</td>"; echo "<td>" . $row['Run_out_date_8'] . "</td>"; echo "<td>" . $row['Qual_9'] . "</td>"; echo "<td>" . $row['Date_Completed_9'] . "</td>"; echo "<td>" . $row['Run_out_date_9'] . "</td>"; echo "<td>" . $row['Qual_10'] . "</td>"; echo "<td>" . $row['Date_Completed_10'] . "</td>"; echo "<td>" . $row['Run_out_date_10'] . "</td>"; echo "</tr>"; } echo "</table>"; } else { echo "0 results"; } } $dbhandle->close(); ?> Link to comment https://forums.phpfreaks.com/topic/293146-notice-undefined-variable-searchq-in-cwampwwwsearch2php-on-line-17/ Share on other sites More sharing options...
hansford Posted December 17, 2014 Share Posted December 17, 2014 $sql = ("SELECT * FROM applicant WHERE `Firstname` LIKE $searchq OR `Lastname` LIKE $searchq") The variable '$searchq' has not yet been defined on this line of code. Define this variable and then run this line of code. Link to comment https://forums.phpfreaks.com/topic/293146-notice-undefined-variable-searchq-in-cwampwwwsearch2php-on-line-17/#findComment-1499825 Share on other sites More sharing options...
Chris721 Posted December 17, 2014 Author Share Posted December 17, 2014 $sql = ("SELECT * FROM applicant WHERE `Firstname` LIKE $searchq OR `Lastname` LIKE $searchq") The variable '$searchq' has not yet been defined on this line of code. Define this variable and then run this line of code. I am not sure how to define the $searchq Link to comment https://forums.phpfreaks.com/topic/293146-notice-undefined-variable-searchq-in-cwampwwwsearch2php-on-line-17/#findComment-1499827 Share on other sites More sharing options...
hansford Posted December 17, 2014 Share Posted December 17, 2014 You define a variable by assigning it a value. Did you write this code. What are you trying to achieve. $searchq = "Jackson"; $searchq = "Paul"; $searchq = "Sarah"; $searchq = $_POST['lastname']; $searchq = $_POST['firstname']; Link to comment https://forums.phpfreaks.com/topic/293146-notice-undefined-variable-searchq-in-cwampwwwsearch2php-on-line-17/#findComment-1499828 Share on other sites More sharing options...
Chris721 Posted December 17, 2014 Author Share Posted December 17, 2014 Yes but I am only just starting with php so learning as I go. I have tried the one above before and got another error Notice: Undefined index: Lastname in C:\wamp\www\search2.php on line 14 and Notice: Undefined index: Firstname in C:\wamp\www\search2.php on line 15 Link to comment https://forums.phpfreaks.com/topic/293146-notice-undefined-variable-searchq-in-cwampwwwsearch2php-on-line-17/#findComment-1499830 Share on other sites More sharing options...
cyberRobot Posted December 17, 2014 Share Posted December 17, 2014 I am not sure how to define the $searchq It looks like you have a form being filled out before the Applicant Data View page is shown. What does the form code look like? Basically, I'm wondering what the search field is named. You just take the corresponding $_POST variable and assign it to $searchq. Of course, you need to assign the value to the variable prior to the query that uses the variable. Link to comment https://forums.phpfreaks.com/topic/293146-notice-undefined-variable-searchq-in-cwampwwwsearch2php-on-line-17/#findComment-1499831 Share on other sites More sharing options...
Chris721 Posted December 17, 2014 Author Share Posted December 17, 2014 I think I may have done the form wrong <form action='search2.php' method='GET'> <center> <h1>Search By Name</h1> <input type='text' size='90' name='search'></br></br> <input type='submit' name='submit' value='Submit' ></br></br></br> </center> </form> Link to comment https://forums.phpfreaks.com/topic/293146-notice-undefined-variable-searchq-in-cwampwwwsearch2php-on-line-17/#findComment-1499832 Share on other sites More sharing options...
cyberRobot Posted December 17, 2014 Share Posted December 17, 2014 Your form uses the GET method. You need to change it to POST...or use $_GET variables in lines like the following: if(isset ($_POST["submit"])) { To use the search input in the query, you need to do something like this prior to the query: $searchq = trim($_GET['search']); ...or if you use the POST method: $searchq = trim($_POST['search']); Link to comment https://forums.phpfreaks.com/topic/293146-notice-undefined-variable-searchq-in-cwampwwwsearch2php-on-line-17/#findComment-1499833 Share on other sites More sharing options...
cyberRobot Posted December 17, 2014 Share Posted December 17, 2014 Also, note that you'll want to protect your queries from SQL injection attacks. Since you're using MySQLi, you can do that with prepared statement queries...or by using mysqli_real_escape_string(). Link to comment https://forums.phpfreaks.com/topic/293146-notice-undefined-variable-searchq-in-cwampwwwsearch2php-on-line-17/#findComment-1499836 Share on other sites More sharing options...
Chris721 Posted December 17, 2014 Author Share Posted December 17, 2014 I have done that and I am now getting no errors but no posts, the code now looks like this <title>Applicant Data view</title> <p> <center><img src="cyberwarfareimage1.png" alt="Squadron logo" style="width:200px;height:200px" style="middle"></center> <table border="1"> <tr> <td><a href="index.php"> Home Page </a></td> <td><a href="administratorhomepage.html">Administrator Home Page </a></td> <td><a href="viewhomepage.html">View Home Page </a></td> </table> </p> <?php $searchq =($_GET['search']); include "connection.php"; $sql = ("SELECT * FROM applicant WHERE `Firstname` LIKE $searchq OR `Lastname` LIKE $searchq") or die("Could Not Search"); $result = $dbhandle->query($sql); echo "<table border='1'> <tr> <th>Applicant Id</th> <th>Firstame</th> <th>Lastname</th> <th>Address</th> <th>Date Of Birth</th> <th>Contact Number</th> <th>Next Of Kin</th> <th>Next Of Kin Address</th> <th>Vetting Date</th> <th>Clearance Expiry</th> <th>Current Employer</th> <th>Date Applied</th> <th>Resubmitting</th> <th>Number Of Previous Attempts</th> <th>Why</th> <th>Qualificaton 1</th> <th>Date Completed</th> <th>Expiry Date</th> <th>Qualificaton 2</th> <th>Date Completed</th> <th>Expiry Date</th> <th>Qualificaton 3</th> <th>Date Completed</th> <th>Expiry Date</th> <th>Qualificaton 4</th> <th>Date Completed</th> <th>Expiry Date</th> <th>Qualificaton 5</th> <th>Date Completed</th> <th>Expiry Date</th> <th>Qualificaton 6</th> <th>Date Completed</th> <th>Expiry Date</th> <th>Qualificaton 7</th> <th>Date Completed</th> <th>Expiry Date</th> <th>Qualificaton 8</th> <th>Date Completed</th> <th>Expiry Date</th> <th>Qualificaton 9</th> <th>Date Completed</th> <th>Expiry Date</th> <th>Qualificaton 10</th> <th>Date Completed</th> <th>Expiry Date</th> </tr>"; if(isset ($_GET["submit"])) { $searchq = $_GET[`submit`]; $searchq = preg_replace("#[^a-z]#i","",$searchq); if ($result->num_rows > 0) { // output data of each row while($row = mysqli_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['Applicant_ID'] . "</td>"; echo "<td>" . $row['Firstname'] . "</td>"; echo "<td>" . $row['Lastname'] . "</td>"; echo "<td>" . $row['Address'] . "</td>"; echo "<td>" . $row['DOB'] . "</td>"; echo "<td>" . $row['Telephone_No'] . "</td>"; echo "<td>" . $row['NOK'] . "</td>"; echo "<td>" . $row['NOK_Address'] . "</td>"; echo "<td>" . $row['Vetting_Date'] . "</td>"; echo "<td>" . $row['Clearance_Expiry'] . "</td>"; echo "<td>" . $row['Current_Employer'] . "</td>"; echo "<td>" . $row['Date_applied'] . "</td>"; echo "<td>" . $row['Resubmission'] . "</td>"; echo "<td>" . $row['Number_of_Attempts'] . "</td>"; echo "<td>" . $row['Why'] . "</td>"; echo "<td>" . $row['Qual_1'] . "</td>"; echo "<td>" . $row['Date_Completed_1'] . "</td>"; echo "<td>" . $row['Run_out_date_1'] . "</td>"; echo "<td>" . $row['Qual_2'] . "</td>"; echo "<td>" . $row['Date_Completed_2'] . "</td>"; echo "<td>" . $row['Run_out_date_2'] . "</td>"; echo "<td>" . $row['Qual_3'] . "</td>"; echo "<td>" . $row['Date_Completed_3'] . "</td>"; echo "<td>" . $row['Run_out_date_3'] . "</td>"; echo "<td>" . $row['Qual_4'] . "</td>"; echo "<td>" . $row['Date_Completed_4'] . "</td>"; echo "<td>" . $row['Run_out_date_4'] . "</td>"; echo "<td>" . $row['Qual_5'] . "</td>"; echo "<td>" . $row['Date_Completed_5'] . "</td>"; echo "<td>" . $row['Run_out_date_5'] . "</td>"; echo "<td>" . $row['Qual_6'] . "</td>"; echo "<td>" . $row['Date_Completed_6'] . "</td>"; echo "<td>" . $row['Run_out_date_6'] . "</td>"; echo "<td>" . $row['Qual_7'] . "</td>"; echo "<td>" . $row['Date_Completed_7'] . "</td>"; echo "<td>" . $row['Run_out_date_7'] . "</td>"; echo "<td>" . $row['Qual_8'] . "</td>"; echo "<td>" . $row['Date_Completed_8'] . "</td>"; echo "<td>" . $row['Run_out_date_8'] . "</td>"; echo "<td>" . $row['Qual_9'] . "</td>"; echo "<td>" . $row['Date_Completed_9'] . "</td>"; echo "<td>" . $row['Run_out_date_9'] . "</td>"; echo "<td>" . $row['Qual_10'] . "</td>"; echo "<td>" . $row['Date_Completed_10'] . "</td>"; echo "<td>" . $row['Run_out_date_10'] . "</td>"; echo "</tr>"; } echo "</table>"; } else { echo "0 results"; } } $dbhandle->close(); ?> Link to comment https://forums.phpfreaks.com/topic/293146-notice-undefined-variable-searchq-in-cwampwwwsearch2php-on-line-17/#findComment-1499837 Share on other sites More sharing options...
cyberRobot Posted December 17, 2014 Share Posted December 17, 2014 Have you tried seeing if there is any MySQL errors? Note that you can find out more information about doing that here: http://php.net/manual/en/mysqli.error.php Basically, you would do something like this: $sql = ("SELECT * FROM applicant WHERE `Firstname` LIKE $searchq OR `Lastname` LIKE $searchq") or die("Could Not Search"); $result = $dbhandle->query($sql) or die($dbhandle->error); Once you get the query working, you'll want to get rid of the "or die..." debugging part. Link to comment https://forums.phpfreaks.com/topic/293146-notice-undefined-variable-searchq-in-cwampwwwsearch2php-on-line-17/#findComment-1499838 Share on other sites More sharing options...
Chris721 Posted December 17, 2014 Author Share Posted December 17, 2014 the query is running but it returns this Unknown column 'Booth' in 'where clause' Link to comment https://forums.phpfreaks.com/topic/293146-notice-undefined-variable-searchq-in-cwampwwwsearch2php-on-line-17/#findComment-1499839 Share on other sites More sharing options...
Chris721 Posted December 17, 2014 Author Share Posted December 17, 2014 If I search by full name I get 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 'leanard OR `Lastname` LIKE adele leanard' at line 1 Link to comment https://forums.phpfreaks.com/topic/293146-notice-undefined-variable-searchq-in-cwampwwwsearch2php-on-line-17/#findComment-1499840 Share on other sites More sharing options...
cyberRobot Posted December 17, 2014 Share Posted December 17, 2014 the query is running but it returns this Unknown column 'Booth' in 'where clause' I wonder if the error is caused by the search string not being enclosed in quotes. Try this: $sql = "SELECT * FROM applicant WHERE `Firstname` LIKE '$searchq' OR `Lastname` LIKE '$searchq'"; Link to comment https://forums.phpfreaks.com/topic/293146-notice-undefined-variable-searchq-in-cwampwwwsearch2php-on-line-17/#findComment-1499841 Share on other sites More sharing options...
Chris721 Posted December 17, 2014 Author Share Posted December 17, 2014 that gives no errors but no results either Link to comment https://forums.phpfreaks.com/topic/293146-notice-undefined-variable-searchq-in-cwampwwwsearch2php-on-line-17/#findComment-1499842 Share on other sites More sharing options...
cyberRobot Posted December 17, 2014 Share Posted December 17, 2014 that gives no errors but no results either Since you're using LIKE, you probably need the wildcard character around the search variable. Here are some examples: http://dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html#operator_like Link to comment https://forums.phpfreaks.com/topic/293146-notice-undefined-variable-searchq-in-cwampwwwsearch2php-on-line-17/#findComment-1499843 Share on other sites More sharing options...
Chris721 Posted December 17, 2014 Author Share Posted December 17, 2014 thank you for all the help, its getting there. Just need to find out why it won't display the data now. Link to comment https://forums.phpfreaks.com/topic/293146-notice-undefined-variable-searchq-in-cwampwwwsearch2php-on-line-17/#findComment-1499844 Share on other sites More sharing options...
Chris721 Posted December 17, 2014 Author Share Posted December 17, 2014 I am sure there is something wrong around the if ($result->num_rows > 0) { // output data of each row while($row = mysqli_fetch_array($result)) area of the code, if I try to echo anything after that it doesn't work, I have tried everything I know and can't seem to fix it. <title>Applicant Data view</title> <p> <center><img src="cyberwarfareimage1.png" alt="Squadron logo" style="width:200px;height:200px" style="middle"></center> <table border="1"> <tr> <td><a href="index.php"> Home Page </a></td> <td><a href="administratorhomepage.html">Administrator Home Page </a></td> <td><a href="viewhomepage.html">View Home Page </a></td> </table> </p> <?php $searchq =($_GET['search']); include "connection.php"; $sql = ("SELECT * FROM applicant WHERE `Firstname` LIKE '$searchq' OR `Lastname` LIKE '$searchq'"); $result = $dbhandle->query($sql) or die($dbhandle->error); echo "<table border='1'> <tr> <th>Applicant Id</th> <th>Firstame</th> <th>Lastname</th> <th>Address</th> <th>Date Of Birth</th> <th>Contact Number</th> <th>Next Of Kin</th> <th>Next Of Kin Address</th> <th>Vetting Date</th> <th>Clearance Expiry</th> <th>Current Employer</th> <th>Date Applied</th> <th>Resubmitting</th> <th>Number Of Previous Attempts</th> <th>Why</th> <th>Qualificaton 1</th> <th>Date Completed</th> <th>Expiry Date</th> <th>Qualificaton 2</th> <th>Date Completed</th> <th>Expiry Date</th> <th>Qualificaton 3</th> <th>Date Completed</th> <th>Expiry Date</th> <th>Qualificaton 4</th> <th>Date Completed</th> <th>Expiry Date</th> <th>Qualificaton 5</th> <th>Date Completed</th> <th>Expiry Date</th> <th>Qualificaton 6</th> <th>Date Completed</th> <th>Expiry Date</th> <th>Qualificaton 7</th> <th>Date Completed</th> <th>Expiry Date</th> <th>Qualificaton 8</th> <th>Date Completed</th> <th>Expiry Date</th> <th>Qualificaton 9</th> <th>Date Completed</th> <th>Expiry Date</th> <th>Qualificaton 10</th> <th>Date Completed</th> <th>Expiry Date</th> </tr>"; if (isset ($_GET["search"])) { $searchq = $_GET[`search`]; $searchq = ("#[^a-z, A-Z]#i"); if ($result->num_rows > 0) { // output data of each row while($row = mysqli_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['Applicant_ID'] . "</td>"; echo "<td>" . $row['Firstname'] . "</td>"; echo "<td>" . $row['Lastname'] . "</td>"; echo "<td>" . $row['Address'] . "</td>"; echo "<td>" . $row['DOB'] . "</td>"; echo "<td>" . $row['Telephone_No'] . "</td>"; echo "<td>" . $row['NOK'] . "</td>"; echo "<td>" . $row['NOK_Address'] . "</td>"; echo "<td>" . $row['Vetting_Date'] . "</td>"; echo "<td>" . $row['Clearance_Expiry'] . "</td>"; echo "<td>" . $row['Current_Employer'] . "</td>"; echo "<td>" . $row['Date_applied'] . "</td>"; echo "<td>" . $row['Resubmission'] . "</td>"; echo "<td>" . $row['Number_of_Attempts'] . "</td>"; echo "<td>" . $row['Why'] . "</td>"; echo "<td>" . $row['Qual_1'] . "</td>"; echo "<td>" . $row['Date_Completed_1'] . "</td>"; echo "<td>" . $row['Run_out_date_1'] . "</td>"; echo "<td>" . $row['Qual_2'] . "</td>"; echo "<td>" . $row['Date_Completed_2'] . "</td>"; echo "<td>" . $row['Run_out_date_2'] . "</td>"; echo "<td>" . $row['Qual_3'] . "</td>"; echo "<td>" . $row['Date_Completed_3'] . "</td>"; echo "<td>" . $row['Run_out_date_3'] . "</td>"; echo "<td>" . $row['Qual_4'] . "</td>"; echo "<td>" . $row['Date_Completed_4'] . "</td>"; echo "<td>" . $row['Run_out_date_4'] . "</td>"; echo "<td>" . $row['Qual_5'] . "</td>"; echo "<td>" . $row['Date_Completed_5'] . "</td>"; echo "<td>" . $row['Run_out_date_5'] . "</td>"; echo "<td>" . $row['Qual_6'] . "</td>"; echo "<td>" . $row['Date_Completed_6'] . "</td>"; echo "<td>" . $row['Run_out_date_6'] . "</td>"; echo "<td>" . $row['Qual_7'] . "</td>"; echo "<td>" . $row['Date_Completed_7'] . "</td>"; echo "<td>" . $row['Run_out_date_7'] . "</td>"; echo "<td>" . $row['Qual_8'] . "</td>"; echo "<td>" . $row['Date_Completed_8'] . "</td>"; echo "<td>" . $row['Run_out_date_8'] . "</td>"; echo "<td>" . $row['Qual_9'] . "</td>"; echo "<td>" . $row['Date_Completed_9'] . "</td>"; echo "<td>" . $row['Run_out_date_9'] . "</td>"; echo "<td>" . $row['Qual_10'] . "</td>"; echo "<td>" . $row['Date_Completed_10'] . "</td>"; echo "<td>" . $row['Run_out_date_10'] . "</td>"; echo "</tr>"; } echo "</table>"; } else { echo "0 results"; } } $dbhandle->close(); ?> Link to comment https://forums.phpfreaks.com/topic/293146-notice-undefined-variable-searchq-in-cwampwwwsearch2php-on-line-17/#findComment-1499847 Share on other sites More sharing options...
cyberRobot Posted December 17, 2014 Share Posted December 17, 2014 The following if test isn't needed: if ($result->num_rows > 0) { mysqli_fetch_array() returns NULL if there are no results. If it's NULL, the loop content will be skipped. Also, you can avoid the extra overhead of mysqli_fetch_array() since you only use the associative array version of the data. You can use mysqli_fetch_assoc() instead. Perhaps the following will work: while($row = $result->fetch_assoc()) { Note that I switch the code to use the object-oriented version. I'm not sure if that's causing the issue. Link to comment https://forums.phpfreaks.com/topic/293146-notice-undefined-variable-searchq-in-cwampwwwsearch2php-on-line-17/#findComment-1499848 Share on other sites More sharing options...
Chris721 Posted December 17, 2014 Author Share Posted December 17, 2014 I have switched it round and I have tried changing a few things. things are looking better the code is starting to run as should bar one error Notice: Undefined index: in C:\wamp\www\search2.php on line 72 Link to comment https://forums.phpfreaks.com/topic/293146-notice-undefined-variable-searchq-in-cwampwwwsearch2php-on-line-17/#findComment-1499850 Share on other sites More sharing options...
cyberRobot Posted December 17, 2014 Share Posted December 17, 2014 You're using the grave accent in the following line: $searchq = $_GET[`search`]; You need to use straight quotes: $searchq = $_GET['search']; With that said, the following lines aren't really doing anything: $searchq = $_GET[`search`]; $searchq = ("#[^a-z, A-Z]#i"); I would imagine these lines need to happen before the query where the $searchq variable is used. For now, I would just comment them out until you get the script working. Especially since the second line is going to just overwrite your variable. The second line looks like an attempt to do a regular expression. If you want to get it working, you'll need to look into how they are executed. Link to comment https://forums.phpfreaks.com/topic/293146-notice-undefined-variable-searchq-in-cwampwwwsearch2php-on-line-17/#findComment-1499854 Share on other sites More sharing options...
Chris721 Posted December 17, 2014 Author Share Posted December 17, 2014 I am now getting some data with a error message off Notice: Undefined index: SELECT * FROM applicant WHERE `Firstname` LIKE 'booth' OR `Lastname` LIKE 'booth' in C:\wamp\www\search2.php on line 72. the data is 100% but its getting some Link to comment https://forums.phpfreaks.com/topic/293146-notice-undefined-variable-searchq-in-cwampwwwsearch2php-on-line-17/#findComment-1499855 Share on other sites More sharing options...
Chris721 Posted December 17, 2014 Author Share Posted December 17, 2014 <title>Applicant Data view</title> <p> <center><img src="cyberwarfareimage1.png" alt="Squadron logo" style="width:200px;height:200px" style="middle"></center> <table border="1"> <tr> <td><a href="index.php"> Home Page </a></td> <td><a href="administratorhomepage.html">Administrator Home Page </a></td> <td><a href="viewhomepage.html">View Home Page </a></td> </table> </p> <?php $searchq =($_GET['search']); include "connection.php"; $sql = ("SELECT * FROM applicant WHERE `Firstname` LIKE '$searchq' OR `Lastname` LIKE '$searchq'"); $result = $dbhandle->query($sql) or die($dbhandle->error); echo "<table border='1'> <tr> <th>Applicant Id</th> <th>Firstame</th> <th>Lastname</th> <th>Address</th> <th>Date Of Birth</th> <th>Contact Number</th> <th>Next Of Kin</th> <th>Next Of Kin Address</th> <th>Vetting Date</th> <th>Clearance Expiry</th> <th>Current Employer</th> <th>Date Applied</th> <th>Resubmitting</th> <th>Number Of Previous Attempts</th> <th>Why</th> <th>Qualificaton 1</th> <th>Date Completed</th> <th>Expiry Date</th> <th>Qualificaton 2</th> <th>Date Completed</th> <th>Expiry Date</th> <th>Qualificaton 3</th> <th>Date Completed</th> <th>Expiry Date</th> <th>Qualificaton 4</th> <th>Date Completed</th> <th>Expiry Date</th> <th>Qualificaton 5</th> <th>Date Completed</th> <th>Expiry Date</th> <th>Qualificaton 6</th> <th>Date Completed</th> <th>Expiry Date</th> <th>Qualificaton 7</th> <th>Date Completed</th> <th>Expiry Date</th> <th>Qualificaton 8</th> <th>Date Completed</th> <th>Expiry Date</th> <th>Qualificaton 9</th> <th>Date Completed</th> <th>Expiry Date</th> <th>Qualificaton 10</th> <th>Date Completed</th> <th>Expiry Date</th> </tr>"; if (isset ($_GET["search"])) { $searchq = $_GET["$sql"]; $searchq = ("#[^a-z, A-Z]#i"); if ($result->num_rows > 0) { // output data of each row while($row = $result->fetch_assoc()) { echo "<tr>"; echo "<td>" . $row['Applicant_ID'] . "</td>"; echo "<td>" . $row['Firstname'] . "</td>"; echo "<td>" . $row['Lastname'] . "</td>"; echo "<td>" . $row['Address'] . "</td>"; echo "<td>" . $row['DOB'] . "</td>"; echo "<td>" . $row['Telephone_No'] . "</td>"; echo "<td>" . $row['NOK'] . "</td>"; echo "<td>" . $row['NOK_Address'] . "</td>"; echo "<td>" . $row['Vetting_Date'] . "</td>"; echo "<td>" . $row['Clearance_Expiry'] . "</td>"; echo "<td>" . $row['Current_Employer'] . "</td>"; echo "<td>" . $row['Date_applied'] . "</td>"; echo "<td>" . $row['Resubmission'] . "</td>"; echo "<td>" . $row['Number_of_Attempts'] . "</td>"; echo "<td>" . $row['Why'] . "</td>"; echo "<td>" . $row['Qual_1'] . "</td>"; echo "<td>" . $row['Date_Completed_1'] . "</td>"; echo "<td>" . $row['Run_out_date_1'] . "</td>"; echo "<td>" . $row['Qual_2'] . "</td>"; echo "<td>" . $row['Date_Completed_2'] . "</td>"; echo "<td>" . $row['Run_out_date_2'] . "</td>"; echo "<td>" . $row['Qual_3'] . "</td>"; echo "<td>" . $row['Date_Completed_3'] . "</td>"; echo "<td>" . $row['Run_out_date_3'] . "</td>"; echo "<td>" . $row['Qual_4'] . "</td>"; echo "<td>" . $row['Date_Completed_4'] . "</td>"; echo "<td>" . $row['Run_out_date_4'] . "</td>"; echo "<td>" . $row['Qual_5'] . "</td>"; echo "<td>" . $row['Date_Completed_5'] . "</td>"; echo "<td>" . $row['Run_out_date_5'] . "</td>"; echo "<td>" . $row['Qual_6'] . "</td>"; echo "<td>" . $row['Date_Completed_6'] . "</td>"; echo "<td>" . $row['Run_out_date_6'] . "</td>"; echo "<td>" . $row['Qual_7'] . "</td>"; echo "<td>" . $row['Date_Completed_7'] . "</td>"; echo "<td>" . $row['Run_out_date_7'] . "</td>"; echo "<td>" . $row['Qual_8'] . "</td>"; echo "<td>" . $row['Date_Completed_8'] . "</td>"; echo "<td>" . $row['Run_out_date_8'] . "</td>"; echo "<td>" . $row['Qual_9'] . "</td>"; echo "<td>" . $row['Date_Completed_9'] . "</td>"; echo "<td>" . $row['Run_out_date_9'] . "</td>"; echo "<td>" . $row['Qual_10'] . "</td>"; echo "<td>" . $row['Date_Completed_10'] . "</td>"; echo "<td>" . $row['Run_out_date_10'] . "</td>"; echo "</tr>"; } echo "</table>"; } else { echo "0 results"; } } $dbhandle->close(); ?> Link to comment https://forums.phpfreaks.com/topic/293146-notice-undefined-variable-searchq-in-cwampwwwsearch2php-on-line-17/#findComment-1499858 Share on other sites More sharing options...
cyberRobot Posted December 17, 2014 Share Posted December 17, 2014 For future reference, please post code between tags. It makes your code and post easier to follow. Link to comment https://forums.phpfreaks.com/topic/293146-notice-undefined-variable-searchq-in-cwampwwwsearch2php-on-line-17/#findComment-1499859 Share on other sites More sharing options...
Chris721 Posted December 17, 2014 Author Share Posted December 17, 2014 sorry my bad Link to comment https://forums.phpfreaks.com/topic/293146-notice-undefined-variable-searchq-in-cwampwwwsearch2php-on-line-17/#findComment-1499860 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.