PHP 脚本无法识别给定的表

发布于 2024-11-09 00:29:57 字数 7026 浏览 0 评论 0原文

最后我找到了一些时间来编写自己的网站。问题是,它无法识别我给他的桌子。我正在写一本基于 SQL 的留言簿。它正在登录 SQL 平台,但根本无法获取数据库...我给他的名字 100% 拼写正确!我尝试在脚本中不使用 $mysql_select_db ,而是在“Select”语句中命名数据库。 “插入”-语句。也不起作用>。<

这是我的脚本:

addguestbook.php

<?php

//******************************************************//
//********************Database stuff********************//
//******************************************************//
$host="localhost";                  // Host name
$username="";                       // Mysql username
$password="";                       // Mysql password
$db_name="mywebsite";               // Database name
//********************Tables***************************//
$tbl_name="guestbook";              // Guestbook

if ($_SERVER['REQUEST_METHOD'] == 'POST'){

    $name = $_POST['name'];
    $email = $_POST['email'];
    $website = $_POST['website'];
    $comment = $_POST['comment'];
    $datetime=date("y-m-d h:i:s"); //date time

    // Connect to server and select database.
    mysql_connect("$host", "$username", "$password")or die("cannot connect server ");
    //mysql_select_db("$db_name")or die("cannot select DB");


    $sql="INSERT INTO $db_name.$tbl_name(name, email, website, comment, datetime)VALUES('$name', '$email', '$website', '$comment', '$datetime')";
    $result=mysql_query($sql);

    mysql_close();
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>

    <table width="700" border="0" align="center" cellpadding="3"
        cellspacing="0">
        <tr>
            <td><strong>Test Sign Guestbook </strong>
            </td>
        </tr>
    </table>
    <table width="700" border="0" align="center" cellpadding="0"
        cellspacing="1" bgcolor="#CCCCCC">
        <tr>
            <form id="form1" name="form1" method="post"
                action="index.php?mod=guestbook">
                <td>
                    <table width="400" border="0" cellpadding="3" cellspacing="1"
                        bgcolor="#FFFFFF">
                        <tr>
                            <td width="117">Name</td>
                            <td width="14">:</td>
                            <td width="357"><input name="name" type="text" id="name"
                                size="65" />
                            </td>
                        </tr>
                        <tr>
                            <td>Email</td>
                            <td>:</td>
                            <td><input name="email" type="text" id="email" size="65" />
                            </td>
                        </tr>
                        <tr>
                            <td>Website</td>
                            <td>:</td>
                            <td><input name="website" type="text" id="website" size="65" />
                            </td>
                        </tr>
                        <tr>
                            <td valign="top">Comment</td>
                            <td valign="top">:</td>
                            <td><textarea name="comment" cols="65" rows="3" id="comment"></textarea>
                            </td>
                        </tr>
                        <tr>
                            <td>&nbsp;</td>
                            <td>&nbsp;</td>
                            <td><input type="submit" name="Submit" value="Submit" /> <input
                                type="reset" name="Submit2" value="Reset" />
                            </td>
                        </tr>
                    </table></td>
            </form>
        </tr>
    </table>
    <table width="700" border="0" align="center" cellpadding="3"
        cellspacing="0">
        <tr>
            <td><strong><a href="viewguestbook.php">View Guestbook</a> </strong>
            </td>
        </tr>
    </table>

</body>
</html>

viewguestbook.php

<table width="400" border="0" align="center" cellpadding="3" cellspacing="0">
<tr>
<td><strong>View Guestbook | <a href="guestbook.php">Sign Guestbook</a> </strong></td>
</tr>
</table>
<br>

<?php
//******************************************************//
//********************Database stuff********************//
//******************************************************//
$host="localhost";                  // Host name
$username="";                       // Mysql username
$password="";                       // Mysql password
$db_name="mywebsite";               // Database name
//********************Tables***************************//
$tbl_name="guestbook";              // Guestbook

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect server ");
//mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM $db_name.$tbl_name";
$result=mysql_query($sql);

while($rows=mysql_fetch_array($result)){
?>
<table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td><table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td>ID</td>
<td>:</td>
<td><? echo $rows['id']; ?></td>
</tr>
<tr>
<td width="117">Name</td>
<td width="14">:</td>
<td width="357"><? echo $rows['name']; ?></td>
</tr>
<tr>
<td>Email</td>
<td>:</td>
<td><? echo $rows['email']; ?></td>
</tr>
<tr>
<td valign="top">Comment</td>
<td valign="top">:</td>
<td><? echo $rows['comment']; ?></td>
</tr>
<tr>
<td valign="top">Date/Time </td>
<td valign="top">:</td>
<td><? echo $rows['datetime']; ?></td>
</tr>
</table></td>
</tr>
<tr>
<td>Website</td>
<td>:</td>
<td><? echo $rows['website']; ?></td>
</tr>
<tr>
</table>
<BR>
<?
}
mysql_close(); //close database
?>

编辑:错误消息:

警告:mysql_fetch_array() 需要 参数 1 为资源,布尔值 给出 C:\Anderes\xampp-win32-1.7.4-VC6\xampp\htdocs\MyWorkspace\MyWebsite\data\viewguestbook.php 第 26 行

我什至手动将一些数据行放入表中,也不起作用。

Finally I found some time to program my own website. The problem is, that it doesn't recognise the table that I'm giving him. I'm writing a guestbook based on SQL. It's logging into the SQL-platform, but simply doesn't get the database ... The name that I'm giving him is 100% spelled correctly! I've tried not to use the $mysql_select_db in my script, but naming the database in the "Select"-Statement & "Insert"-Statement. Doesn't work either >.<

Here're my scripts:

addguestbook.php

<?php

//******************************************************//
//********************Database stuff********************//
//******************************************************//
$host="localhost";                  // Host name
$username="";                       // Mysql username
$password="";                       // Mysql password
$db_name="mywebsite";               // Database name
//********************Tables***************************//
$tbl_name="guestbook";              // Guestbook

if ($_SERVER['REQUEST_METHOD'] == 'POST'){

    $name = $_POST['name'];
    $email = $_POST['email'];
    $website = $_POST['website'];
    $comment = $_POST['comment'];
    $datetime=date("y-m-d h:i:s"); //date time

    // Connect to server and select database.
    mysql_connect("$host", "$username", "$password")or die("cannot connect server ");
    //mysql_select_db("$db_name")or die("cannot select DB");


    $sql="INSERT INTO $db_name.$tbl_name(name, email, website, comment, datetime)VALUES('$name', '$email', '$website', '$comment', '$datetime')";
    $result=mysql_query($sql);

    mysql_close();
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>

    <table width="700" border="0" align="center" cellpadding="3"
        cellspacing="0">
        <tr>
            <td><strong>Test Sign Guestbook </strong>
            </td>
        </tr>
    </table>
    <table width="700" border="0" align="center" cellpadding="0"
        cellspacing="1" bgcolor="#CCCCCC">
        <tr>
            <form id="form1" name="form1" method="post"
                action="index.php?mod=guestbook">
                <td>
                    <table width="400" border="0" cellpadding="3" cellspacing="1"
                        bgcolor="#FFFFFF">
                        <tr>
                            <td width="117">Name</td>
                            <td width="14">:</td>
                            <td width="357"><input name="name" type="text" id="name"
                                size="65" />
                            </td>
                        </tr>
                        <tr>
                            <td>Email</td>
                            <td>:</td>
                            <td><input name="email" type="text" id="email" size="65" />
                            </td>
                        </tr>
                        <tr>
                            <td>Website</td>
                            <td>:</td>
                            <td><input name="website" type="text" id="website" size="65" />
                            </td>
                        </tr>
                        <tr>
                            <td valign="top">Comment</td>
                            <td valign="top">:</td>
                            <td><textarea name="comment" cols="65" rows="3" id="comment"></textarea>
                            </td>
                        </tr>
                        <tr>
                            <td> </td>
                            <td> </td>
                            <td><input type="submit" name="Submit" value="Submit" /> <input
                                type="reset" name="Submit2" value="Reset" />
                            </td>
                        </tr>
                    </table></td>
            </form>
        </tr>
    </table>
    <table width="700" border="0" align="center" cellpadding="3"
        cellspacing="0">
        <tr>
            <td><strong><a href="viewguestbook.php">View Guestbook</a> </strong>
            </td>
        </tr>
    </table>

</body>
</html>

viewguestbook.php

<table width="400" border="0" align="center" cellpadding="3" cellspacing="0">
<tr>
<td><strong>View Guestbook | <a href="guestbook.php">Sign Guestbook</a> </strong></td>
</tr>
</table>
<br>

<?php
//******************************************************//
//********************Database stuff********************//
//******************************************************//
$host="localhost";                  // Host name
$username="";                       // Mysql username
$password="";                       // Mysql password
$db_name="mywebsite";               // Database name
//********************Tables***************************//
$tbl_name="guestbook";              // Guestbook

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect server ");
//mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM $db_name.$tbl_name";
$result=mysql_query($sql);

while($rows=mysql_fetch_array($result)){
?>
<table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td><table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td>ID</td>
<td>:</td>
<td><? echo $rows['id']; ?></td>
</tr>
<tr>
<td width="117">Name</td>
<td width="14">:</td>
<td width="357"><? echo $rows['name']; ?></td>
</tr>
<tr>
<td>Email</td>
<td>:</td>
<td><? echo $rows['email']; ?></td>
</tr>
<tr>
<td valign="top">Comment</td>
<td valign="top">:</td>
<td><? echo $rows['comment']; ?></td>
</tr>
<tr>
<td valign="top">Date/Time </td>
<td valign="top">:</td>
<td><? echo $rows['datetime']; ?></td>
</tr>
</table></td>
</tr>
<tr>
<td>Website</td>
<td>:</td>
<td><? echo $rows['website']; ?></td>
</tr>
<tr>
</table>
<BR>
<?
}
mysql_close(); //close database
?>

edit: the errormessage:

Warning: mysql_fetch_array() expects
parameter 1 to be resource, boolean
given in
C:\Anderes\xampp-win32-1.7.4-VC6\xampp\htdocs\MyWorkspace\MyWebsite\data\viewguestbook.php
on line 26

I've even put some lines of data into the table manually, doesn't work either.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

千秋岁 2024-11-16 00:29:57
  $sql="SELECT * FROM $db_name.$tbl_name";

 $sql="INSERT INTO $db_name.$tbl_name(name, email, website, comment, datetime)VALUES('$name', '$email', '$website', '$comment', '$datetime')";

您应该使用以下方式 $db_name
和 $tbl_name 是变量

  $sql="SELECT * FROM `".$db_name."` .`".$tbl_name."`";

$sql="INSERT INTO `".$db_name."` .`".$tbl_name."`(name, email, website, comment, datetime)VALUES('$name', '$email', '$website', '$comment', '$datetime')";
  $sql="SELECT * FROM $db_name.$tbl_name";

 $sql="INSERT INTO $db_name.$tbl_name(name, email, website, comment, datetime)VALUES('$name', '$email', '$website', '$comment', '$datetime')";

You should use following way $db_name
and $tbl_name are variables

  $sql="SELECT * FROM `".$db_name."` .`".$tbl_name."`";

$sql="INSERT INTO `".$db_name."` .`".$tbl_name."`(name, email, website, comment, datetime)VALUES('$name', '$email', '$website', '$comment', '$datetime')";
把时间冻结 2024-11-16 00:29:57

在这两个文件中,您使用 mysql_connect("$host", "$username", "$password") 这是错误的,您应该使用 mysql_connect($host, $username, $password ) 。那么它应该可以工作。

in both files you are using mysql_connect("$host", "$username", "$password") it is wrong , you should use mysql_connect($host, $username, $password) . then it should work .

恰似旧人归 2024-11-16 00:29:57

请尝试这个更新的代码并给出结果。

// Connect to server and select database.
$conn = mysql_connect($host, $username, $password)or die(mysql_error());

$sql="INSERT INTO $db_name.$tbl_name(name, email, website, comment, datetime)VALUES('$name', '$email', '$website', '$comment', '$datetime')";
$result=mysql_query($sql,$conn);

mysql_close();

Try this updated code please and give result.

// Connect to server and select database.
$conn = mysql_connect($host, $username, $password)or die(mysql_error());

$sql="INSERT INTO $db_name.$tbl_name(name, email, website, comment, datetime)VALUES('$name', '$email', '$website', '$comment', '$datetime')";
$result=mysql_query($sql,$conn);

mysql_close();
吝吻 2024-11-16 00:29:57

为了安全起见,我假设您在这里删除了用户名和密码值。
正如你所拥有的,该代码对我来说看起来不错。
在我的服务器上,数据库名称以我的主登录名为前缀。
所以“mywebsite”将是“myname_mywwebsite”。
确保数据库和表名称拼写正确。

I'm assuming you blanked out the username and password values for security here.
The code looks okay to me as you have it.
On my server, the database name is prefixed by my primary login name.
So "mywebsite" would be "myname_mywwebsite".
Make sure you have the database and table names spelled correctly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文