0

how to access tables from database by using php in wamp server.i have done the following code but its not working for some reason.is there anything to put in 'action=""'.it is not giving any error but displaying the same page.i want to display table from database on any different entry in dropdown menu and pressing search button..

<p class="h2">Quick Search</p>
    <div class="sb2_opts">
     <p>
   </p>
<form method="post" action="" >
 <p>Enter your source and destination.</p>
<p>
    From:</p>
<select name="from">
<option value="Islamabad">Islamabad</option>
<option value="Lahore">Lahore</option>
<option value="murree">Murree</option>
<option value="Muzaffarabad">Muzaffarabad</option>
</select>
<p>
    To:</p>
   <select name="To">
<option value="Islamabad">Islamabad</option>
<option value="Lahore">Lahore</option>
<option value="murree">Murree</option>
<option value="Muzaffarabad">Muzaffarabad</option>
</select>
<input type="submit" value="search" /> 
</form>
</form> </table>

<?php


if(isset($_POST['from']) and isset($_POST['To'])) {
$from = $_POST['from'] ;
$to = $_POST['To'] ;
$table = array($from, $to);
$con=mysqli_connect("localhost");
$mydb=mysql_select_db("homedb"); 
if (mysqli_connect_errno()) 
{ 
echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
}


switch ($table) {
  case array ("Islamabad", "Lahore") :
$result = mysqli_query($con,"SELECT * FROM flights");
echo "</flights>";                                    //table name is flights


 break;
  case array ("Islamabad", "Murree") :

$result = mysqli_query($con,"SELECT * FROM `isb to murree`");
echo "</`isb to murree`>";                                     //table name isb to murree
  ;
 break;
  case array ("Islamabad", "Muzaffarabad") :

$result = mysqli_query($con,"SELECT * FROM `isb to muzz`");
echo "</`isb to muzz`>";
 break;
//.....
//......
default:
echo "Your choice is nor valid !!";
}

}
mysqli_close($con);
?>
2
  • How do you test this? Everything you echo are invalid HTML tags which will not be displayed by a browser. Commented Aug 23, 2014 at 10:03
  • how to echo database table ?? i am running it in browser using wamp server but yes it is not displaying table..can you please explain. @GeraldSchneider Commented Aug 23, 2014 at 10:43

2 Answers 2

0

Check the Php manual for mysqli_connect => Procedural style

mysqli mysqli_connect ([ string $host = ini_get("mysqli.default_host") [, string $username = ini_get("mysqli.default_user") [, string $passwd = ini_get("mysqli.default_pw") [, string $dbname = "" [, int $port = ini_get("mysqli.default_port") [, string $socket = ini_get("mysqli.default_socket") ]]]]]] )

An eg. like below

$con = mysqli_connect("myhost","myusername","mypassword","mydatabase") or die("Error " . mysqli_error($link)); 

In your code you are opening connection with mysqli_connect and selecting database with mysql_select_db. That is wrong..

You can rewrite it something like below:

<p class="h2">Quick Search</p>
    <div class="sb2_opts">
     <p>
   </p>
<form method="post" action="" >
 <p>Enter your source and destination.</p>
<p>
    From:</p>
<select name="from">
<option value="Islamabad">Islamabad</option>
<option value="Lahore">Lahore</option>
<option value="murree">Murree</option>
<option value="Muzaffarabad">Muzaffarabad</option>
</select>
<p>
    To:</p>
   <select name="To">
<option value="Islamabad">Islamabad</option>
<option value="Lahore">Lahore</option>
<option value="murree">Murree</option>
<option value="Muzaffarabad">Muzaffarabad</option>
</select>
<input type="submit" value="search" /> 
</form>
</form> </table>

<?php


if(isset($_POST['from']) and isset($_POST['To'])) {
$from = $_POST['from'] ;
$to = $_POST['To'] ;
$table = array($from, $to);
$con=mysqli_connect("localhost", "yourusername", "yourpassword", "yourdatabasename");

if (mysqli_connect_errno()) 
{ 
echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
}


switch ($table) {
  case array ("Islamabad", "Lahore") :
$result = mysqli_query($con,"SELECT * FROM flights");
echo "flights";                                    //table name is flights


 break;
  case array ("Islamabad", "Murree") :

$result = mysqli_query($con,"SELECT * FROM `isb to murree`");
echo "</`isb to murree`>";                                     //table name isb to murree
  ;
 break;
  case array ("Islamabad", "Muzaffarabad") :

$result = mysqli_query($con,"SELECT * FROM `isb to muzz`");
echo "isb to muzz";
 break;
//.....
//......
default:
echo "Your choice is nor valid !!";
}

}
mysqli_close($con);
?>
3
  • If this would be the problem the OP would get the Failed to connect to MySQL error message. Commented Aug 23, 2014 at 10:07
  • @GeraldSchneider in his code he is selecting the database using mysql_select_db(). But his connection is mysqli_connect.
    – fortune
    Commented Aug 23, 2014 at 10:11
  • its still not working just showing some warnings but not output msql table..guide me plz as i am stuck in this from three days,,thnanks @fortune Commented Aug 23, 2014 at 10:37
0

should give username and password.

$con = mysqli_connect("localhost","root","","mydatabase") or die("Error " . mysqli_error($link));
1
  • If this would be the problem the OP would get the Failed to connect to MySQL error message. Commented Aug 23, 2014 at 10:06

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.