hosted by liquidweb


Go Back   Web Hosting Talk : Web Hosting Main Forums : Programming Discussion : MySQL Help
Reply

Forum Jump

MySQL Help

Reply Post New Thread In Programming Discussion Subscription
 
Send news tip View All Posts Thread Tools Search this Thread Display Modes
  #1  
Old
Web Hosting Guru
 
Join Date: Jul 2003
Location: laredo
Posts: 280

MySQL Help


I was reading a php book and I attepted to create a database. I took the first beginning example and I was able to create it. Then I made a form. Then I had to make a "HandleForm.php" but I couldn't get it to work. Could anybody helpme on what is wrong please. I get the (The query could not be executed) message when I pass information trough the form to be stored in the database. Below is the script. Ofcourse I was using the right username and password. Also where it says "mysql" instead of "localhost" I am required to use that by my ISP. Thanks.





<HTML>
<HEAD>
<TITLE>Inserting Data into a Database</TITLE>
</HEAD>
<BODY>
<?php
/* This page receives and handles the data generated by "form.html". */
// Trim the incoming data.
$Array["FirstName"] = trim ($Array["FirstName"]);
$Array["LastName"] = trim ($Array["LastName"]);
$Array["Email"] = trim ($Array["Email"]);
$Array["Comments"] = trim ($Array["Comments"]);

// Set the variables for the database access:
$Host = "mysql";
$User = "username";
$Password = "password";
$DBName = "NewDatabase";
$TableName = "Feedback";

$Link = mysql_connect ($Host, $User, $Password);
$Query = "INSERT into $TableName values ('0', '$Array[FirstName]', '$Array[LastName]', '$Array[Email]', '$Array[Comments]')";
print ("The query is:<BR>$Query<P>\n");
if (mysql_db_query ($DBName, $Query, $Link)) {
print ("The query was successfully executed!<BR>\n");
} else {
print ("The query could not be executed!<BR>\n");
}
mysql_close ($Link);
?>
</BODY>
</HTML>



Sponsored Links
  #2  
Old
Newbie
 
Join Date: Sep 2002
Location: Santiago de Queretaro, Queretaro de Arteaga, Mexico
Posts: 21
Maybe if you use the next syntax at your query statement it could work:

$Query = "INSERT into ".$TableName." values ('0', '".$Array["FirstName"]."', '".$Array["LastName"]."', '".$Array["Email"]."', '".$Array["Comments"]."')";

  #3  
Old
Web Hosting Master
 
Join Date: Jul 2003
Location: East TN
Posts: 568
Try using <?php and ?> tags....

Take care,
Lori

Sponsored Links
  #4  
Old
Community Guide
 
Join Date: Jul 2003
Location: Kuwait
Posts: 5,099
A few tips.

First ... always use error checking, never assume that things went okay, especially when its something external (like a database server). For mysql, there is mysql_error() that returns the error from MySQL.

Second ... use syntax highlighting ... it helps avoid common syntax errors (from typos) :

PHP Code:
<HTML>
<HEAD>
<TITLE>Inserting Data into a Database</TITLE>
</HEAD>
<BODY>
<?php
/* This page receives and handles the data generated by "form.html". */
// Trim the incoming data.
$Array["FirstName"] = trim ($Array["FirstName"]);
$Array["LastName"] = trim ($Array["LastName"]);
$Array["Email"] = trim ($Array["Email"]);
$Array["Comments"] = trim ($Array["Comments"]);

// Set the variables for the database access:
$Host "mysql";
$User "username";
$Password "password";
$DBName "NewDatabase";
$TableName "Feedback";

$Link mysql_connect ($Host$User$Password);
$Query "INSERT into $TableName values ('0', '$Array[FirstName]', '$Array[LastName]', '$Array[Email]', '$Array[Comments]')";
print (
"The query is:<BR>$Query<P>\n");
if (
mysql_db_query ($DBName$Query$Link)) {
print (
"The query was successfully executed!<BR>\n");
} else {
print (
"The query could not be executed!<BR>\n");
}
mysql_close ($Link);
?>
</BODY>
</HTML>
There could be many reasons your processing script isn't working. First of all, $Host = "mysql"; should be $Host = "localhost"; if your mysql server is the same computer as your web server.

Secondly, you never select the database ... although you assign it to a variable ($DBName).

Thirdly, $Array does not hold incoming data from a form. The data will either be in $_GET or $_POST.

Try and get these things sorted out and then it might work

<plug type="shameless">I have a tutorial that's slightly more detailed that shows you how to do the same thing. Check it out at www.meidomus.com </plug>

Would personally like to know what book this example is from

Good luck

__________________
In order to understand recursion, one must first understand recursion.
If you feel like it, you can read my blog
Signal > Noise

  #5  
Old
Web Hosting Guru
 
Join Date: Jul 2003
Location: laredo
Posts: 280
Hello

I tried pretty much everything above. Finally I used one of those mysql error() that fyrestrtr pointed me to and I got the following error result.

The query is:
INSERT into Feedback values ('0', 'raul', 'gonzalez', 'raulgonzalez@smartlaredo.com', 'Just my email')
The query could not be executed!
A fatal MySQL error occured.
Query:
Error: (2002) Can't connect to local MySQL server through socket '/tmp/mysql.sock' (46)

Could anybody tell me what this means.

thanks

  #6  
Old
Community Guide
 
Join Date: Jul 2003
Location: Kuwait
Posts: 5,099
It means one of two things :

1. Your mysql server isn't running

2. You didn't specify the correct mysql server in your mysql_connect() function.


I think its number 2 since $Host = "mysql"; doesn't look right. Try changing $Host to "localhost" or "127.0.0.1"

__________________
In order to understand recursion, one must first understand recursion.
If you feel like it, you can read my blog
Signal > Noise

  #7  
Old
Web Hosting Guru
 
Join Date: Jul 2003
Location: laredo
Posts: 280
localhost

the thing is that I am not using a server on my computer. I am using yahoo webhost. Will that work anyway?

The reason I am using "mysql" instead of "localhost" is because I visited one of yahoo's help pages and I read the following:

First, make sure that you are using the host name "mysql" and not "localhost" in any of your PHP configuration files that require a MySQL host name.

  #8  
Old
Newbie
 
Join Date: Oct 2003
Location: atlanta georgia usa
Posts: 6
which book?

fyrestrtr sez:
Would personally like to know what book this example is from

Ullman, Larry, _PHP for the World Wide Web_ chapter 11, Script 11.4, page 194.

I have been trying to use this book and gotten stuck in ax example in the regular expressions chapter that doesn't work and cant seem to find out why. The book is out of date. I wish I had another newer edition.

Bob

  #9  
Old
Web Hosting Master
 
Join Date: Jan 2003
Posts: 1,715
Try using mysql error on the mysql_connect. Perhaps the login and/or password is wrong, but I think you're feeding db_query an invalid link, so it's making a desperate attempt to connect... and failing.

__________________
Game Servers are the next hot market!
Slim margins, heavy support, fickle customers, and moronic suppliers!
Start your own today!

  #10  
Old
Web Hosting Guru
 
Join Date: Jul 2003
Location: laredo
Posts: 280
The book

Yes that would be the exact same book I am using.

  #11  
Old
Web Hosting Guru
 
Join Date: Jul 2003
Location: laredo
Posts: 280
Hello

Hi. I finally made it work.

That was pretty much what the book covered.

1. create database
2. create a table
3. create an HTML form
4. create a HandleForm
and
5. Display database

does anybody know where I could go and learn how to "errase" from the database hehe.

thanks

  #12  
Old
Web Hosting Evangelist
 
Join Date: Dec 2002
Location: Toronto, Canada
Posts: 522
Go to www.mysql.com and look through their documentation. It's not for the true beginners, but they explain everything really well.

Inserting, deleting and updating a database is done through queries. Instead of doing the INSERT query that you did before, you would pass a DELETE query. Sometime like:

PHP Code:
$Query "DELETE FROM `".$TableName."` WHERE <some condition>"
You can always not put the WHERE, but that will delete everything from that table. So you should replace <some condition> with a condition, like FirstName = "John"

Also, note the ` around the name of the table. Although this is optional, it's a good idea to always add these around table and field names to make sure MySQL doesn't confuse it with some reserved word (most common example being a table named ORDER being confused with ORDER BY).

Hope this helps. Feel free to PM me if you need help.

Max

__________________
Nokhrin - http://www.nokhrin.com/
~ e-commerce application development

Reply

Related posts from TheWhir.com
Title Type Date Posted
Tesora's OpenStack DBaaS Supports MongoDB, Cassandra, Redis, and MySQL Web Hosting News 2014-05-23 14:53:11
Google Releases Hosted Database Service Cloud SQL to General Availability Web Hosting News 2014-02-12 13:46:02
Google Cloud Provides Support For Native MySQL Connections Web Hosting News 2013-11-01 14:36:06
Web Hosting Sales and Promos Roundup - September 20, 2013 Web Hosting News 2014-05-23 15:42:47
PHP And MySQL Scaling: Preparing A Startup For Growth Blog 2014-04-24 13:27:35


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes
Postbit Selector

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump
Login:
Log in with your username and password
Username:
Password:



Forgot Password?
WHT Host Brief Email:

We respect your privacy. We will never sell, rent, or give away your address to any outside party, ever.

Advertisement:
Web Hosting News:
WHT Membership
WHT Membership



 

X

Welcome to WebHostingTalk.com

Create your username to jump into the discussion!

WebHostingTalk.com is the largest, most influentual web hosting community on the Internet. Join us by filling in the form below.


(4 digit year)

Already a member?