1

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).

5
  • your form has no method... Commented Mar 3, 2017 at 16:10
  • Also use $_POST instead of $_GET - test it by doing a var_dump on the post to make sure your value is being submitted Commented Mar 3, 2017 at 16:12
  • $repName variable is not defined. Commented Mar 3, 2017 at 16:13
  • 2
    @PhpDude form always have a method, in his case the method is GET Commented Mar 3, 2017 at 16:20
  • @Jawanaut try the below answer.Problem is not in METHOD.By default form method is GET .As already mentioned by @Masivuye Cokile. Commented Mar 3, 2017 at 16:25

1 Answer 1

2

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();

?>
Sign up to request clarification or add additional context in comments.

9 Comments

@Masivuye Cokile yes agree with you. Down-voting without explanation is not a good habit.
strange... this was discussed here : meta.stackoverflow.com/questions/344703/…
The reason for the DV's is because the answer is wrong. If you bind a subset of the results you will get a warning: Warning: mysqli_stmt::bind_result(): Number of bind variables doesn't match number of fields in prepared statement in selecting 4 cols and 2 binds, error cc \ @MasivuyeCokile
The question of comments + downvotes has been discussed ad nauseum on Meta. Many folks just choose to DV and move on. Many offer advice. Many try to light the path for newbies. Sometimes folks just DV an answer which is wrong.
Do or do not. There is no "try". A good answer will always have an explanation of what was done and why it was done in such a manner, not only for the OP but for future visitors to SO.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.