1

Good day people,
some days ago, I started learning php and now I'm at the point where I intend to teach myself database queries with mysql.
My current code, however, won't process anything.

It's supposed to take text input from the index.html, pass it to a.php and have the a.php look for the (name /) input string in a database (phone book), then output the matching row(s) in a table.

It consists of two parts; the index.html which is just the following:

<form action="a.php">
<input type="text" name="q">
<input type="submit">
</form>

and the a.php which is supposed to process the inputted data:

<?php
    echo $_GET['q'];
    $such = $_GET['q'];
    $mysqliu = new mysqli("HOST", "User", "Password", "Database");
    $sql="Select * from LIST where name like '%$such%'";
    $result = mysqli_query($mysqliu,$sql);
    if($result = $mysqliu->query($sql)) {
    echo "<table><tr><th>Kennummer</th><th>Name</th><th>Strasse</th><th>PLZ</th><th>Telefon</th></tr>";
        while($row = $result->fetch_array() ) {
            echo "<tr>";
            echo "<td>" , "$row[0]" , "</td>";
            echo "<td>" , "$row[1]" , "</td>";
            echo "<td>" , "$row[2]" , "</td>";
            echo "<td>" , "$row[3]" , "</td>";
            echo "<td>" , "$row[4]" , "</td>";
            echo "</tr>";
        }
    }
    $result->close();
    echo "</table>";
    else {
        echo"Nope"
    }
    $mysqliu->close();
?>

I tried outcommenting as much as possible to see where it breaks and it seems that as soon as I want to do something to "q" (the query from index.html), it breaks.
The above code doesn't contain the SQL connection data but that's present in my code.
The issue is not related to the PHP server or anything server-side so I'm sure I'm doing something wrong.
I can echo the variable q in a.php so it's passed over but whatever I do after that, nothing happens and I get a blank page. Can you experts help me please?

Solved: It was the ; missing right at the end. Thanks to everyone for their input.

Regards~

Yuka
  • 31
  • 4
  • Figure out where your Apache/PHP error log is and read through it, then try to fix your problem based on the encountered error. "Nothing happens" is not productive in trying to figure it out. – deceze Jun 10 '15 at 08:12
  • A blank screen means you have an error. Try enabling error reporting: http://php.net/manual/en/function.error-reporting.php – redelschaap Jun 10 '15 at 08:13
  • There must have been some kind of error message emitted, either into the web page (if viewing in a browser), into the console (if running from commandline) or in the error logs. What was it? – GordonM Jun 10 '15 at 08:13
  • Thank you for your quick answer. I can't take a look at said log because it's on a college server, sadly. – Yuka Jun 10 '15 at 08:14
  • Also, your code as written is wide open to SQL injections. It's better to learn how to avoid them early on than to form bad habits now that you'll need to break out of later. – GordonM Jun 10 '15 at 08:14
  • There should be no code between the if {} and else {} statements. Also you should add a semicolon at the end of echo"Nope" – Octav Jun 10 '15 at 08:26
  • @Yuka Add these 2 lines of code at top of the PHP code to display errors: error_reporting(-1); ini_set('display_errors', 'On'); – JC Lee Jun 10 '15 at 08:27

5 Answers5

1

Try to add a method in the form tag like GET or POST. Set a default value for the q field. Also set a name for the type submit and dump the whole $_GET or $_POST array in the php file.

Jubayer Arefin
  • 485
  • 7
  • 17
1

I won't give you the exact answer, I'll let you figure it out...

  1. use error_reporting
  2. Check your IF-ELSE statement, does it look correct??
Boby
  • 826
  • 5
  • 9
  • Thanks you: You gave me the right hint. `else { echo"Nope" }` There was a semicolon missing after the `echo` – Yuka Jun 10 '15 at 10:58
1

Note:

  • You don't have a method attribute in your <form>
  • What if a user just typed-in their browser, a.php? You should be validating the page so user can't just access this page
  • Is your table really LIST? Be case sensitive about it.
  • Your query is still prone to SQL injections. You should be using mysqli_real_escape_string() extension of PHP, or better use mysqli_* prepared statement.

Your form should look like this:

<form action="a.php" method="GET">

And sanitize the values of your passed on data:

$such = mysqli_real_escape_string($mysqliu,$_GET["q"]);

If you are curious with prepared statement, you can try this:

$such = "%".$_GET["q"]."%";
$mysqliu = new mysqli("HOST", "User", "Password", "Database"); /* REPLACE NECESSARY DATA */

if($stmt = $mysqliu->prepare("SELECT kennummer,name,strasse,plz,telefon FROM LIST WHERE name LIKE ?")){ /* REPLACE NECESSARY COLUMN NAME */
  $stmt->bind_param("s",$such); /* BIND PARAMETER TO THE QUERY */
  $stmt->execute(); /* EXECUTE QUERY */
  $stmt->bind_result($kennummer,$name,$strasse,$plz,$telefon); /* BIND THE RESULT TO VARIABLE */
  ?>
    <table>
      <tr>
        <th>Kennummer</th>
        <th>Name</th>
        <th>Strasse</th>
        <th>PLZ</th>
        <th>Telefon</th>
      </tr>
  <?php
  while($stmt->fetch()){ /* LOOP THE RESULT */
    ?>
      <tr>
        <td><?php echo $kennummer; ?></td>
        <td><?php echo $name; ?></td>
        <td><?php echo $strasse; ?></td>
        <td><?php echo $plz; ?></td>
        <td><?php echo $telefon; ?></td>
      </tr>
    <?php
  }
  ?>
    </table>
  <?php
  $stmt->close();
}
Community
  • 1
  • 1
Logan Wayne
  • 6,001
  • 16
  • 31
  • 49
1

Solved: It was the ; missing right at the end. Thanks to everyone for their input.

Yuka
  • 31
  • 4
0

First, you need to enable error reporting in the page using error_reporting(-1);. As you are getting error but that is not getting display because error reporting is OFF.

Second, your code welcomes to SQL injections So It is better to learn first that how you can avoid SQL injections after that approach for queries in database.

Third, You need to check MySQLi extension is installed or not on your PHP. Use var_dump(function_exists('mysqli_connect')); and then check the output.

Fourth, $mysqli->fetch_array(); returns weird results sometimes that is because of old PHP version so that can be a reason of error, please check once with that.

Fifth, I believe there is an error with your if else statement. else statement should start after } of if statement .

I can edit my answer once you please show the exact error in your question from error log meanwhile you can check with these.

Deep Kakkar
  • 5,831
  • 4
  • 39
  • 75