BachirDarawish Posted May 3, 2014 Share Posted May 3, 2014 Hi I need some help with this code. I have been trying to execute this query by clicking on the search button. The query should select a the data on the web page but only the for the id entered. but I can't seem to make to button execute the query. Can somebody help me? <table border="1" align="center" cellpadding="2" cellspacing="0"> <tr> <td colspan="4"> <div align="center"> <input type="button" name="Student ID" value="Search ID" onclick="f()" /> <input type="text" name="StudentID"> <?php function f(){ $StudentID=$_POST['StudentID']; // Connect to the database $dbLink = new mysqli('', '', '', ''); if(mysqli_connect_errno()) { die("MySQL connection failed: ". mysqli_connect_error()); } // Query for a list of all existing files $sql = 'SELECT `MeetingRef`, `StudentID`, `Created`, `Progress`,`Problems` FROM `Meeting` WHERE StudentID StudentID'; $result = $dbLink->query($sql); // Check if it was successfull if($result) { // Make sure there are some files in there if($result->num_rows == 0) { echo '<p>There are no dat saved for this student in the database</p>'; } else { // Print the top of a table echo '<table width="100px"> <tr> <td align ="center"><b>Meeting Ref</td> <td align ="center"><b>Student ID</td> <td align ="center"><b>Created</b></td> <td align ="center"><b>Progress</b></td> <td align ="center"><b>Problems</b></td> </tr>'; // Print each file while($row = $result->fetch_assoc()) { echo " <tr> <td>{$row['MeetingRef']}</td> <td>{$row['StudentID']}</td> <td>{$row['Created']}</td> <td>{$row['Progress']}</td> <td>{$row['Problems']}</td> </tr>"; } // Close table echo '</table>'; } // Free the result $result->free(); } else { echo 'Error! SQL query failed:'; echo "<pre>{$dbLink->error}</pre>"; } // Close the mysql connection $dbLink->close(); } ?> </div></td> </tr> </table> Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted May 4, 2014 Share Posted May 4, 2014 php is a server-side scripting language. all the php code you have on a page runs when the page is requested. your button and the onclick() event exists in the client-side (browser) code. to do what you want will require that your client-side code make a http request to the server and pass it either a post or get value that it can test to decide to call your php f() function code. also, don't create database connections inside of functions to run just the code in that function. your application should create one database connection and pass it into any function/class that needs it, as a call time parameter. Quote Link to comment Share on other sites More sharing options...
BachirDarawish Posted May 4, 2014 Author Share Posted May 4, 2014 yeah but this is to select and retrieve the data of the specific student ID but what abt the displays one? what is the query because mine don't seem to work it keep display all the data for all student but I just want for 1 student id? Quote Link to comment Share on other sites More sharing options...
ginerjm Posted May 5, 2014 Share Posted May 5, 2014 As Mac_gyver aptly pointed out you are confusing yourself with your coding practices. Separate the php from your presentation code (html) and handle the inputs and functions to run the query upon receiving the proper submit from the form. What you have shown us is impossible to understand and I'm sure it is not making much sense to you as you try to figure out what is wrong. As for your execution problem, I think your confusion is that you have created a button to trigger the query but you don't have two things: 1 - a form to contain the inputs and to trigger the script to do your query 2 - a submit element to trigger the form. You are using a type='button' element expecting something to happen in php which won't occur until you submit the data via a form element. Unless you are planning on doing this thru JS and Ajax, which I highly doubt. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.