Sebastian
Sebastian

Reputation: 1654

PHP Script not recognising given table

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>&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
?>

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.

Upvotes: 0

Views: 289

Answers (4)

Bill D
Bill D

Reputation: 31

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.

Upvotes: 0

Vishwanath Dalvi
Vishwanath Dalvi

Reputation: 36631

  $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')";

Upvotes: 1

daganh
daganh

Reputation: 421

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();

Upvotes: 0

Kanishka Panamaldeniya
Kanishka Panamaldeniya

Reputation: 17576

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 .

Upvotes: 0

Related Questions