Jump to content

execute Mysql Query by Clicking a button and display the data


Recommended Posts

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>                

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.