Jawanaut
Jawanaut

Reputation: 11

Run SQL Query with button click

Edit 3: I figured I should try to word, not only my issue but also my end goal better... so here it goes. I need data to be returned to the user by their input. What they put into the form, will return specific data from the database or nothing at all. The database I'm using is information on field reps. When the user enters into the form, they will be looking for specific information. The number they will be asked for will be the repID number. Now... the problem I am having is taking the number that is put into the form, and calling that specific data from the database. The user will not be able to see other data, not associated with other repID's.

Okay, I'm pretty sure I'm not supposed to just delete my entire initial post, but I'm still searching for an answer. Now, I would assume it should be relatively simple, however it has turned out to more taxing than I had originally thought. Perhaps I am not explaining my needs clear enough, I do tend to have that issue a lot.

Here it goes... How to Run Query with button click 2.0:

I have a database written in and stored on a server. The table I will be accessing is called dataRep and houses Rep Data. This data is user input via form submission. Upon coming to the website there is an option to "View Rep Information" by submitting the Rep's ID that was given. That Rep ID will be the auto increment repID from the table. Upon clicking the button, it opens a new window that should display the rep's data. It does not, however.

Here is the the user will see:

<div class="pop_box">
<a class="rms-button" href="#popup1">Enter Rep Number Here</a> </div>
<div id="popup1" class="overlay">
<div class="popup">
    <a class="close" href="#">&times;</a>
<div align="center"><br>
       <br>
       <p>Enter rep number in box below. Submission will open new window.</p>
    <form method="get" action="/data/repPrepare.php" target="_blank" >
      <input type="text" id="repSelect" name="repSelect" placeholder="Enter Rep Number Given Here" />&nbsp;&nbsp;
        <button type="submit" class="newbutton">VIEW</button>
    </form>
</div>
</div>

...changed the a little, but now I get this error:

"Warning: mysqli_query() expects at least 2 parameters, 1 given on line 21 Unable to prepare statement: Query was empty"

<?php 
 $host = 'mhhost';
 $user = 'myuser';
 $pass = 'mypass';
 $db = 'mydatabase';

 $con = mysqli_connect($host,$user,$pass, $db);

 //-------------------------------------------------------------------------
 // 2) Query database for data
 //--------------------------------------------------------------------------

 $query = mysqli_query("SELECT * FROM dataRep WHERE repID = ?");   //query
 $stmt = mysqli_prepare($con, $query)
or die("Unable to prepare statement: " . $con->error);
 $stmt->bind_param("i", $_GET["repSelect"]);
 $stmt->execute(); 
 $array = mysqli_fetch_row($result);                        //fetch result    

 //-------------------------------------------------------------------------
 // 3) echo result as json 
 //-------------------------------------------------------------------------

 echo json_encode($array);

 ?>

I would like to apologize in advance if I'm totally messing up the procedure for the forum, its just I am truly stuck and have been dealing with this issue for two weeks. Once again, I would appreciate any assistance that can be provided. I just need the to pull the data tied the repID that the user puts in the box (repSelect).

Upvotes: 1

Views: 3829

Answers (1)

Hikmat Sijapati
Hikmat Sijapati

Reputation: 6994

try like this. You have to print variables bind in bind_result().So

<?php 
$stmt = $mysqli->prepare("SELECT repID, RepName, RepBio, RepCerts FROM dataRep WHERE repID = ?");
$stmt->bind_param('i', $_GET['repSelect']);
$stmt->execute(); 
$stmt->bind_result($repID, $repName,$repBio,$repCerts); 
while($stmt->fetch()){
 echo $repName;//now it prints RepName
   };
$stmt->close();

?>

Upvotes: 2

Related Questions