0

I wanted to create a search bar, Final.php is a display page for every event the user has inputted into the database but I want to add a search bar to query the list to find the result they want faster (the event names and four scores for each team). I don't understand where I have gone wrong, I hope someone can help.


<?php
$con = mysqli_connect("localhost", "id5052875_signuplogin", "Meganruby2") or die("cannot connect");
mysqli_select_db($con, "id5052875_signuplogin") or die ("couldnt connect");
$output = '';

    //collect
if (isset($_POST['search'])) {
    $searchq = $_POST['search'];
    $searchq = preg_replace("#[^0-9a-z]#i","",$searchq);
    print("$searchq");
    $query = mysqli_query($con, "SELECT * FROM event WHERE event name LIKE '%$searchq%'") or die("could not search");
    $count = mysqli_num_rows($query);
    echo($searchq);
    if($count == 0 ) {
        $output = 'There was no search';

    }else {
        while($row = mysqli_fetch_array($query)) {
            $event = $row['event name'];
            $num1 = $row['Score1'];
            $num2 = $row['Score2'];
            $num3 = $row['Score3'];
            $num4 = $row['Score4'];


            $output .= '<div> '.$event.' '.$num1.' '.$num2.' '.$num3.' '.$num4.'</div>';

            }

        }



    }
?>    
<form action="Final.php" method="post">
    <input type = "text" name = "search" placeholder = "search for event.."/>
    <input type = "submit" value = "search"/>
</form>

1 Answers1

0

Your first condition, change

if ( isset( $_POST['event'] ) ) { to if ( isset( $_POST['search'] ) ) {

and

$searchq = $_POST['search']; to $searchq = $_POST['search'];

The name of the search text input is 'search', but for some reason, you are looking for event.

Also, please not that you are taking direct user input and inserting it into a DB query. Please be careful of SQL injection. This is a common question to check and learn more about preventing it.

Ice76
  • 1,143
  • 8
  • 16
  • ive done that but now my $query isn't working, Warning: mysqli_query() expects at least 2 parameters, 1 given in /storage/ssd2/875/5052875/public_html/Final.php on line 23 could not search – Ruby Watts Jul 31 '18 at 22:16
  • `$output` was never displayed because the if condition was never true, you were looking for the wrong field. Now that you are in the if block, your query needs fixing. `mysqli_query` expects two arguments, the SQL connection, and the query. You are only supplying the query at the moment, please read http://php.net/manual/en/mysqli.query.php – Ice76 Jul 31 '18 at 22:18
  • okay thank youuuu! – Ruby Watts Jul 31 '18 at 22:29
  • The biggest thanks is with rep or answer! @RubyWatts – Ice76 Jul 31 '18 at 22:32
  • @RubyWatts for almost every question there is some solution available on SO, so before posting your question try to google it :) – Mayank Pandeyz Aug 03 '18 at 13:49
  • I did google it but im inexperience and don't understand – Ruby Watts Aug 15 '18 at 13:11