LearningScholar Posted November 17, 2014 Share Posted November 17, 2014 Hey guys, As my name refers, I am a beginner at school. I had a project in which I had to make a database. The database contains movies which you can search for. However in the beginning, the results only appeared when the name of the movie was fully written. I solved it by using 'LIKE' in the query. But now I got the problem of results being shown before you searched for the results. The form is on the same file as the result. I could seperate them but in this case, it's no option (for some reasons which doesn't matter). Can this problem get solved in an other way? This is the part of the code of my php file: <form id="form1" name="form1" method="post" action="Website2.php"> <div id="Zoekend"> <p> <label for="textfield"></label> <label for="Zoek"></label> <input type="text" name="Zoek" id="Zoek" /> </p> <p> <label> <input type="radio" name="RadioGroup1" value="radio" id="RadioGroup1_0" /> Genre</label> <br /> <label> <input type="radio" name="RadioGroup1" value="radio" id="RadioGroup1_1" /> Naam</label> <br /> <label> <input type="radio" name="RadioGroup1" value="radio" id="RadioGroup1_2" /> Jaar</label> <br /> <label> <input type="radio" name="RadioGroup1" value="radio" id="RadioGroup1_3" /> Regisseur</label> <br /> <input type="submit" name="Zoeken" id="Zoeken" value="Zoeken" /> <br /> </p> </form> </div> <div id="Resultaat"> <h1> Zoekresultaten: </h1> <?php $zoekterm = $_POST['Zoek']; $username = '1509506_dyon'; $wachtwoord = 'u2pv6stvk'; $host = 'fdb6.awardspace.net'; $database = '1509506_dyon'; mysql_connect($host, $username, $wachtwoord); mysql_select_db ($database); $sql = "SELECT * FROM Movies WHERE Genre LIKE '%$zoekterm%'"; $resultaat = mysql_query ($sql); while ( $row = mysql_fetch_assoc($resultaat) ) { echo $row['Genre'] . " - " . $row['Naam'] . " - " . $row ['Jaar'] . " - " . $row ['Regisseur'] .'<br>' ; } $sql = "SELECT * FROM Movies WHERE Naam LIKE '%$zoekterm%'"; $resultaat = mysql_query ($sql); while ( $row = mysql_fetch_assoc($resultaat) ) { echo $row['Genre'] . " - " . $row['Naam'] . " - " . $row ['Jaar'] . " - " . $row ['Regisseur'].'<br>' ; } $sql = "SELECT * FROM Movies WHERE Jaar LIKE '%$zoekterm%'"; $resultaat = mysql_query ($sql); while ( $row = mysql_fetch_assoc($resultaat) ) { echo $row['Genre'] . " - " . $row['Naam'] . " - " . $row ['Jaar'] . " - " . $row ['Regisseur'].'<br>' ; } $sql = "SELECT * FROM Movies WHERE Regisseur LIKE '%$zoekterm%'"; $resultaat = mysql_query ($sql); while ( $row = mysql_fetch_assoc($resultaat) ) { echo $row['Genre'] . " - " . $row['Naam'] . " - " . $row ['Jaar'] . " - " . $row ['Regisseur'] .'<br>'; } ?> </div> Quote Link to comment Share on other sites More sharing options...
Barand Posted November 17, 2014 Share Posted November 17, 2014 If that is real password, change it. Quote Link to comment Share on other sites More sharing options...
Barand Posted November 17, 2014 Share Posted November 17, 2014 Check to see if a search value has been posted. Only process the second part of your script if there is a posted value. Quote Link to comment Share on other sites More sharing options...
LearningScholar Posted November 17, 2014 Author Share Posted November 17, 2014 The site contains a login system. you'll get to this file after you succesfully logged in. Might that be an issue? Quote Link to comment Share on other sites More sharing options...
Barand Posted November 17, 2014 Share Posted November 17, 2014 If you load this page without a search term it will execute a search WHERE ... LIKE '%%', which is all records Quote Link to comment Share on other sites More sharing options...
LearningScholar Posted November 18, 2014 Author Share Posted November 18, 2014 That's the problem. How can I fix this? Quote Link to comment Share on other sites More sharing options...
Barand Posted November 18, 2014 Share Posted November 18, 2014 Check to see if a search value has been posted. Only process the second part of your script if there is a posted value. Quote Link to comment Share on other sites More sharing options...
LearningScholar Posted November 18, 2014 Author Share Posted November 18, 2014 When you're logged in and redirected to this form it automatically executes the criteria as nothing and it shows everything fromm the database. I would like to have it search only after you clicked on the search button Quote Link to comment Share on other sites More sharing options...
LearningScholar Posted November 18, 2014 Author Share Posted November 18, 2014 Do you know a code which only gives results if a value has been posted? Quote Link to comment Share on other sites More sharing options...
boompa Posted November 18, 2014 Share Posted November 18, 2014 if (isset($_POST['Zoeken'])) { // code } Quote Link to comment Share on other sites More sharing options...
Solution Barand Posted November 18, 2014 Solution Share Posted November 18, 2014 There are some other things you need to fix. Radio buttons need different values. All yours have a value of "radio" You don't change the search depending on the button selected (not that you could with those values) You are using mysql_xxxx() functions which are about to be removed from PHP. Try something like this <html> <body> <form id="form1" name="form1" method="post" action=""> <div id="Zoekend"> <p> <label for="Zoek"></label> <input type="text" name="Zoek" id="Zoek" /> </p> <p> <label> <input type="radio" name="RadioGroup1" value="1" id="RadioGroup1_0" /> Genre</label> <br /> <label> <input type="radio" name="RadioGroup1" value="2" id="RadioGroup1_1" checked="checked"/> Naam</label> <br /> <label> <input type="radio" name="RadioGroup1" value="3" id="RadioGroup1_2" /> Jaar</label> <br /> <label> <input type="radio" name="RadioGroup1" value="4" id="RadioGroup1_3" /> Regisseur</label> <br /> <input type="submit" name="Zoeken" id="Zoeken" value="Zoeken" /> <br /> </p> </div> </form> <div id="Resultaat"> <h1> Zoekresultaten: </h1> <?php // // if no search attempt then we don't want to process the results // if (isset($_POST['Zoek']) && trim($_POST['Zoek'])!='' ) { $zoekterm = $_POST['Zoek']; $username = '1509506_dyon'; $wachtwoord = '****'; // deleted for your protection $host = 'fdb6.awardspace.net'; $database = '1509506_dyon'; $db = new mysqli($host, $username, $wachtwoord, $database); $fields = array ( 1 => 'Genre', 2 => 'Naam', 3 => 'Jaar', 4 => 'Regisseur' ); $field = $fields[$_POST['RadioGroup1']]; $sql = "SELECT * FROM Movies WHERE $field LIKE '%$zoekterm%'"; $resultaat = $db->query ($sql); if ($resultaat->num_rows > 0) { while ( $row = $resultaat->fetch_assoc() ) { echo $row['Genre'] . " - " . $row['Naam'] . " - " . $row ['Jaar'] . " - " . $row ['Regisseur'] .'<br>' ; } } else { echo "No matching results found"; } } else { echo "No search"; } ?> </div> </body> </html> Quote Link to comment Share on other sites More sharing options...
LearningScholar Posted November 19, 2014 Author Share Posted November 19, 2014 Thank you so much! I've staring for hours at my code trying to find a way to make it work. And also thanks for the checkboxesproblem you solved. I didn't even noticed it myself XD Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.