1

When I click the go to the form I want to see all other information about that id number. enter image description here Here is the code behind the go to the form option:

        <td width="174"  class="centertext"><a href="form.php?formid=<?php echo $row["formid"]; ?>"> Go to the form</a></td> 

This is the code that I used to retrieve a specific row from my database. the code does not show any errors but it also does not show the result. All I see is empty page. What is the problem here? I have been trying to solve for 1 week and can't find anything. I am beginner at PHP. Thanks in advance.

<?php
$conn = mysqli_connect("localhost","root","","son_fbe");

if (mysqli_connect_error()) {

     echo "Failed to connect to MySQL: " . mysqli_connect_error();
exit();

    }


$formid = isset($_GET['formid ']) ? $_GET['formid '] : ''; 

    if($sql= "SELECT formid,gonderilen,gonderen FROM derssaydirma WHERE formid = ?")
                  {  
                     $stmt = $conn->prepare($sql); 
                     $stmt->bind_param("i", $formid);
                     $stmt->execute();
                     $result = $stmt->get_result();
                    while ($row = $result->fetch_assoc()) {
                         echo $row[ 'formid'];   
                         echo  $row['gonderilen'];
                         echo $row['gonderen'];   }

                     $stmt->close();
                 }
                 // show an error if the query has an error
                 else
                 {
                     echo "Error: could not prepare SQL statement";
                 }


?>
  • Your `if` statement will never be false. All it's checking is the assignment of a string to `$sql`. – BenM Apr 30 '20 at 20:51
  • what do you mean? I do not get it –  Apr 30 '20 at 20:53
  • 1
    What do you think this line does? `if($sql= "SELECT formid,gonderilen,gonderen FROM derssaydirma WHERE formid = ?")` – BenM Apr 30 '20 at 20:54
  • it will take three columns that mentioned there where the formid is met and İf it takes these 3 columns do the following statements. is it right? –  Apr 30 '20 at 20:55
  • But what is the purpose of wrapping it inside an `if`? – BenM Apr 30 '20 at 20:55
  • 1
    Actually I do not know. when you say it, it looks there is no need for if statement. –  Apr 30 '20 at 20:57
  • Are you sure there are any rows returned at all? Maybe your query is wrong and there are no results at all. Can you do a echo inside the while and check if something gets print? – davidev Apr 30 '20 at 21:11
  • 2
    @davidev Yeah there is no result. I already do the echo here. while ($row = $result->fetch_assoc()) { echo $row[ 'formid']; echo $row['gonderilen']; echo $row['gonderen']; } –  Apr 30 '20 at 21:13
  • 1
    Tip: A lot of problems can be detected and resolved by [enabling exceptions in `mysqli`](https://stackoverflow.com/questions/14578243/turning-query-errors-to-exceptions-in-mysqli) so errors resulting from simple mistakes made aren’t easily ignored. Without exceptions you must pay close attention to return values, many of these indicate problems you must resolve or report to the user. Exceptions allow for more sophisticated flow control as they can “bubble up” to other parts of your code where it’s more convenient to handle them. – tadman Apr 30 '20 at 21:16

1 Answers1

1

I think I found the mistake.. you having a blank inside your name when accessing the get array. $formid will be "" and that's why your query is not working.

Change this line..

$formid = isset($_GET['formid ']) ? $_GET['formid '] : ''; 

to

$formid = isset($_GET['formid']) ? $_GET['formid'] : ''; 
davidev
  • 7,694
  • 5
  • 21
  • 56
  • 1
    @dontclosemyquestions No problem .. I tried to reproduce your code and then noticed that $formid is "" eventhough I set the get parameter – davidev Apr 30 '20 at 21:36