首先,我有index.php,其中的代码如下所示
<form action="connection.php" method="post">
<table>
<thead><tr><td>Enter Item Details</td></tr></thead>
<tr>
<td>Item name :- </td>
<td><input type="text" maxlength="30" name="item_name"></td>
</tr>
<tr>
<td>Item Desc :- </td>
<td><input type="text" maxlength="150" name="item_desc"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Add Item">
</td></tr>
</table>
</form>我的connection.php由如下代码组成
$host="localhost";
$username="root";
$password="";
$db_name="eatery";
$tbl_name="test_item";
mysql_connect("$host", "$username", "$password") or die("Not able to connect to MySql");
echo "connected";
mysql_select_db("$db_name") or die ("Not able to connect to eatery database");我已经安装了wamp包来运行apache服务器和我的php代码。现在,当单击index.php的submit按钮时,它显示的不是数据库连接成功/失败消息,而是connection.php的内部代码。那么我在这里错过了什么呢?
发布于 2014-04-27 23:05:03
将connection.php文件包装在php标记中:
<?php
// your php code
?>更新:
如果这还不起作用。也许你应该确保apache使用的是php。而且您是通过apache而不是直接从文件系统提供文件的。
发布于 2014-04-27 23:05:35
您必须将变量作为变量传递,而不是作为字符串。
改变这一点
mysql_connect("$host", "$username", "$password")or die ....
mysql_select_db("$db_name")or die...至
<?php
.....
.....
.....
mysql_connect($host, $username, $password) or die ....
mysql_select_db($db_name)or die ...
......
?>要调试错误并查看确切的错误,请使用此命令而不是错误消息。
or die(mysql_error());https://stackoverflow.com/questions/23324928
复制相似问题