0

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.

10
  • Can you update the answer with any error messages you are getting? Commented May 21, 2011 at 9:23
  • maybe mysql is not started by " /etc/init.d/mysqld start " Commented May 21, 2011 at 9:25
  • MySQL is started for sure ... I'm connecting on it, just not on the database Commented May 21, 2011 at 9:28
  • What exactly happens? What error messages do you get? Commented May 21, 2011 at 9:29
  • 1
    try this in your or die : or die("cannot connect server :".mysql_error()); Commented May 21, 2011 at 9:34

4 Answers 4

1
  $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')";
Sign up to request clarification or add additional context in comments.

1 Comment

ya, thanks for the advice. I'm not that familiar with the syntax-style-stuff.
0

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 .

1 Comment

it does also work the way I wrote it, but thanks :) it's working now - I just didn't know a username was existing -.-'
0

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

Comments

0

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.

Comments

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.