Please help me PHP geniuses! U are my last hope!

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Yiaggi
Forum Newbie
Posts: 13
Joined: Tue Feb 17, 2009 5:12 am

Please help me PHP geniuses! U are my last hope!

Post by Yiaggi »

pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


Hi guys,

I have to start by apologizing for posting what will probably prove to be an easy question! I have been agonizing over this for days now and have tried all the forums and tutorials etc but have hit a real stumbling block.

Basically - I am a web designer and have been learning mysql for a while now and have a good understanding of it (I thought!). I am trying to add a simple guestbook to a website using php, which I have been learning for a while also by following tutorials & scouring the net for tips.

So far I have:

* been on to my hosing servers site & opened a mysql database.
* built my simple database in mysql and added 3 .php pages to my site (details below).
* uploaded my site to my server.

The problem I am having is that when I try to add my first post to my guestbook - I get an error message that reads:

" cannot select DB Access denied for user 'myusername'@'%' to database 'mrcagb' "
(I have changed my actual username for 'myusername' for the purposes of this post but have left the curious '@'%' coz thats nothing to do with me!).

I am really confused! The username & password I have been using are the one's given to me by my hosing provider 'Switch Media'. I went onto their site, accessed the control panel for my website & opened a mysql database. It then gave me my username & asked me to set the password. Am I not using the right details? Should I have set these details in mysql to allow access from my website?

I have had the code checked over by people on Yahoo Answers but I suspect no one on their really gives a stuff! Could any of you give it the once over? I would be forever grateful!

********

I have a simple mysql database called 'mrcagb'

Within that I have a simple table to collect the information called 'guestbook'

I have 3 .php pages on my site called 'guestbook.php', 'addguestbook.php' & 'viewguestbook.php'.

GUESTBOOK.PHP
----------------------------

Code: Select all

<table width="400" border="0" align="center" cellpadding="3" cellspacing="0">
<tr>
<td><strong>Test Sign Guestbook </strong></td>
</tr>
</table>
<table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form id="form1" name="form1" method="post" action="addguestbook.php">
<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="40" /></td>
</tr>
<tr>
<td>Email</td>
<td>:</td>
<td><input name="email" type="text" id="email" size="40" /></td>
</tr>
<tr>
<td valign="top">Comment</td>
<td valign="top">:</td>
<td><textarea name="comment" cols="40" 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="400" border="0" align="center" cellpadding="3" cellspacing="0">
<tr>
<td><strong><a href="viewguestbook.php">View Guestbook</a> </strong></td>
</tr>
</table>
[THAT SEEMS TO WORK FINE & OFFER A PAGE TO ENTER THE MESSAGE ETC]

ADD GUESTBOOK.PHP
------------------------------------

Code: Select all

 
<?php
$host="localhost"; 
$username="myusername";
$password="mypassword";
$db_name="mrcadb"; 
$tbl_name="guestbook";
 
mysql_connect("$host", "$username", "$password")or die("cannot connect server "); 
mysql_select_db("$db_name")or die("cannot select DB ". mysql_error());
 
$datetime=date("y-m-d h:i:s"); //date time
 
$sql="INSERT INTO $tbl_name(name, email, comment, datetime)VALUES('$name', '$email', '$comment', '$datetime')";
$result=mysql_query($sql);
 
//check if query successful 
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='viewguestbook.php'>View guestbook</a>"; // link to view guestbook page 
}
 
else {
echo "ERROR";
}
 
mysql_close();
?>
and then finally:

VIEWGUESTBOOK.PHP
-------------------------------------

Code: Select all

<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
 
$host="localhost";
$username="myusername";
$password="mypassword";
$db_name="mrcadb";
$tbl_name="guestbook"; 
 
mysql_connect("$host", "$username", "$password")or die("cannot connect server "); 
mysql_select_db("$db_name")or die("cannot select DB ". mysql_error());
 
$sql="SELECT * FROM $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>

Once again - I have changed my username & password for the purposes of this post. I am aware it wont work like that and thats not what I have been doing! :)

I just want to get this to work so I can get on learning all the juicy things php can do. I am eager to learn but hit a real stumbling block. If any of you have the time to help I will be forever in your debt!

Kind regards :banghead:


pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

Re: Please help me PHP geniuses! U are my last hope!

Post by susrisha »

Code: Select all

 
[color=#FF0000]mysql_connect("$host", "$username", "$password")or die("cannot connect server "); 
mysql_select_db("$db_name")or die("cannot select DB ". mysql_error());[/color]
 
You need to assign the connection to a resource and then give that resource as a handle to select db

Code: Select all

 
$db_connection = mysql_connect("$host", "$username", "$password");
 
mysql_select_db("$db_name",$db_connection )or die("cannot select DB ". mysql_error());
 
Yiaggi
Forum Newbie
Posts: 13
Joined: Tue Feb 17, 2009 5:12 am

Re: Please help me PHP geniuses! U are my last hope!

Post by Yiaggi »

Thanks for your reply.

Have tried what you suggested but I still get the same error message!

Now I have an added part that says "Resource id #2" ....... ???! :banghead:
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

Re: Please help me PHP geniuses! U are my last hope!

Post by susrisha »

You need to check two things:
1. is the username and password correct for mysql? check the same using mysql commandline approach.
2. Please post the exact error that is being displayed.

Code: Select all

 
mysql_connect($host, $username,$password)or die("cannot connect server mysql_error() ");
 
Yiaggi
Forum Newbie
Posts: 13
Joined: Tue Feb 17, 2009 5:12 am

Re: Please help me PHP geniuses! U are my last hope!

Post by Yiaggi »

Thanks for your reply, I think I may have solved that part of the problem by changing my database name to one given to me by my hosting provider. I am now recieving an error that corresponds to a part further on in my script so I am assuming I am now connected!?

Basically I have this code on the last part off ADDGUESTBOOK.PHP :

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

//check if query successful
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='viewguestbook.php'>View guestbook</a>"; // link to view guestbook page
}

else {
echo "ERROR - query not successfull";

}

mysql_close();
?>

This is I assume the part that is trying to enter the details into my database to make available for viewing on VIEWGUESTBOOK.PHP.

I get this error message now:

ERROR - query not successfull Any idea's guys?!

:banghead: :cry:
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Please help me PHP geniuses! U are my last hope!

Post by califdon »

When posting code in this forum, please enclose the code in BBcode tags to make it easier to read. Put [ php] at the beginning and [ /php] at the end, but omit the spaces, which I added here so they wouldn't parse.

While you're getting a database script running, take advantage of the MySQL error messages to explain your errors. You can do that by adding an or die() conditional to your mysql_ commands, like this:

Code: Select all

$result=mysql_query($sql) or die(mysql_error());
This will do nothing if the query is successful, but if it throws an error, it will print the MySQL error message on the screen and then halt the script. You should remove the or die() once the script is working properly.
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

Re: Please help me PHP geniuses! U are my last hope!

Post by susrisha »

Code: Select all

 
 
//try to put this..
else {
echo "ERROR - query not successfull  mysql_error()";
}
[color=#FF0000]$result=mysql_query($sql);[/color]
 
You need to give the connection handler in mysql_query if you are doing an insert or an update query. so change the line accordingly

@califdon
Please correct me if i am wrong.. ;)
Yiaggi
Forum Newbie
Posts: 13
Joined: Tue Feb 17, 2009 5:12 am

Re: Please help me PHP geniuses! U are my last hope!

Post by Yiaggi »

Hi mate,

Thanks for your reply.

It seems that every time I write on here - my problem has evolved somehow!

I have tried what you sugested but still get an error message. Right now I am getting:

"Table 'mrcagb.guestbook' doesn't exist"

That is obviously very wierd! The database does exist because I can access it through the mySQL prompt! Secondly - my table is not called "mrcagb.guestbook" - it is just called 'guestbook'.

I will put the code for my pages etc below for you to have a gander at as it has changed slightly since my first post. I have changed my actual username & password to "myusername" & "mypassword" for the purposes of this post.

DATABASE

Database name: mrcagb
Table name: guestbook
Data entered to table: create table guestbook (id not null auto_increment, name char(255), email char(255), comment longtext, datetime(255), primary key (id));

ADDGUESTBOOK.PHP

<?php
$host="localhost"; // Host name
$username="myusername"; // Mysql username
$password="mypassword"; // Mysql password
$db_name="mrcagb"; // Database name
$tbl_name="guestbook"; // Table name

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

$datetime=date("y-m-d h:i:s"); //date time

$sql="INSERT INTO guestbook(name, email, comment, datetime)VALUES('$name', '$email', '$comment', '$datetime')";
$result=mysql_query($sql) or die(mysql_error());

//check if query successful
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='viewguestbook.php'>View guestbook</a>"; // link to view guestbook page
}

else {
echo "ERROR - query not successfull mysql_error ()";
}
$result=mysql_query($sql);

mysql_close();
?>


VIEWGUESTBOOK.PHP

<?php

$host="localhost"; // Host name
$username="myusername"; // Mysql username
$password="mypassword"; // Mysql password
$db_name="mrcagb"; // Database name
$tbl_name="guestbook"; // Table name

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

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

while($rows=mysql_fetch_array($result)){
?>


GUESTBOOK.PHP

<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="40" /></td>
</tr>
<tr>
<td>Email</td>
<td>:</td>
<td><input name="email" type="text" id="email" size="40" /></td>
</tr>
<tr>
<td valign="top">Comment</td>
<td valign="top">:</td>
<td><textarea name="comment" cols="40" 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>


Any help you can give will be truly appreciated! I really want to continue learning php but have hit a horrible stumbling block! :banghead:

Ta in advance :)
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

Re: Please help me PHP geniuses! U are my last hope!

Post by susrisha »

Code: Select all

 
$sql="INSERT INTO guestbook (name, email, comment, datetime) VALUES ('$name', '$email', '$comment', '$datetime')";
[color=#FF0000]$result=mysql_query($sql) or die(mysql_error());[/color]
 
You forgot to give the connection handler here.

Code: Select all

 
$result=mysql_query($sql,$db_connection);
 
also check if there are any typing errors with the database tables. mind you it might also be a case sensitive issue. Also see the spaces that i have included in the sql query here posted. Even that used to cause me problems earlier.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Please help me PHP geniuses! U are my last hope!

Post by VladSun »

susrisha wrote:

Code: Select all

 
 
//try to put this..
else {
echo "ERROR - query not successfull  mysql_error()";
}
[color=#FF0000]$result=mysql_query($sql);[/color]
 
You need to give the connection handler in mysql_query if you are doing an insert or an update query. so change the line accordingly

@califdon
Please correct me if i am wrong.. ;)
No, you are not right :
http://bg2.php.net/manual/en/function.mysql-query.php
link_identifier

The MySQL connection. If the link identifier is not specified, the last link opened by mysql_connect() is assumed.
Yiaggi opens only one connection to a database, so omitting the link identifier is fine.
There are 10 types of people in this world, those who understand binary and those who don't
Yiaggi
Forum Newbie
Posts: 13
Joined: Tue Feb 17, 2009 5:12 am

Re: Please help me PHP geniuses! U are my last hope!

Post by Yiaggi »

No dice im afraid guys!

Have tried your suggestions from above and am still getting the same error message!

Table 'mrcagb.guestbook' doesn't exist

Any more idea's would be very welcome.

Also - If you have a link to a simple guestbook tutorial that works then feel free to pass them on - there are clearly issues with the one I have been using!

Thanks again guys :)
vmmene
Forum Newbie
Posts: 8
Joined: Mon Feb 09, 2009 3:26 am

Re: Please help me PHP geniuses! U are my last hope!

Post by vmmene »

exceute this sql statement show tables in your command line after selection of your database.This command will give the the list of tables in your database.This will help you to chek whether your table is exist in your database or not.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Please help me PHP geniuses! U are my last hope!

Post by califdon »

Yiaggi wrote:No dice im afraid guys!

Have tried your suggestions from above and am still getting the same error message!

Table 'mrcagb.guestbook' doesn't exist

Any more idea's would be very welcome.
When you get an error message, a good idea would be to read it. If it says something doesn't exist, either it's just not there, it's somewhere else, or you've misspelled it. There aren't any other possibilities that I can think of.

You might also read the replies to your message and follow instructions. Please do not post lengthy PHP code again without enclosing it in tags, as I explained to you earlier.
Post Reply