如何通过在wamp server.i中使用php来访问数据库中的表。我已经完成了以下代码,但由于某种原因它无法工作。有什么东西可以放入' action ="" ' .it没有给出任何错误但显示相同的页面。我想在下拉菜单中的任何不同条目上显示数据库中的表格并按下搜索按钮..
<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);
?>
答案 0 :(得分:0)
查看mysqli_connect =&gt;的Php
手册程序风格
mysqli mysqli_connect([string $ host = ini_get(&#34; mysqli.default_host&#34;)[,string $ username = ini_get(&#34; mysqli.default_user&#34;)[,string $ passwd = ini_get (&#34; mysqli.default_pw&#34;)[,string $ dbname =&#34;&#34; [,int $ port = ini_get(&#34; mysqli.default_port&#34;)[,string $ socket = ini_get(&#34; mysqli.default_socket&#34;)]]]]]])
例如。如下所示
$con = mysqli_connect("myhost","myusername","mypassword","mydatabase") or die("Error " . mysqli_error($link));
在您的代码中,您将打开与mysqli_connect
的连接并选择带有mysql_select_db
的数据库。这是错误的..
您可以将其重写如下:
<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);
?>
答案 1 :(得分:0)
应该提供用户名和密码。
$con = mysqli_connect("localhost","root","","mydatabase") or die("Error " . mysqli_error($link));