basic writting to php

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
FranzS
Forum Newbie
Posts: 7
Joined: Sat Dec 26, 2009 12:07 am

basic writting to php

Post by FranzS »

I'm trying to write to a mysql database....
The html form that forwards the info seems to work but here it is anyways("formCheck" is my validation Javascript):

Code: Select all

<form action="form.processed.php" name="formcheck" onsubmit="return formCheck(this);">
<table border=0>
<tr>
    <td>First Name*: </td><td><input type=text name="FirstName" size="30"></td>
</tr>
<tr>
    <td>Last Name*: </td><td><input type=text name="LastName" size="30"></td>
</tr>
<tr>
    <td>E-mail*:</td><td><input type=text name="Email" size="30"></td>
</tr>
<tr>
    <td>Company:</td><td><input type=text name="Company" size="30"></td>
</tr>
<tr>
    <td>Phone:</td><td><input type=text name="Phone" size="30"></td>
</tr>
<tr>
    <td colspan=2>Address</td>
</tr>
<tr>
    <td>City:</td><td><input type=text name="City" size="15">State:<input type=text name="State" size="2"></td>
</tr>
</table>
 
 
 
<input type=submit value="Submit and Download Trial">
</form>
*Required Fields<br><br><br>
 
The php file form.processed.php is:

Code: Select all

 
<?php
$FirstName = $_POST[FirstName];
$LastName = $_POST[LastName];
$Email = $_POST[Email];
$Company = $_POST[Company];
$Phone = $_POST[Phone];
$City = $_POST[City];
$State = $_POST[State];
 
mysql_connect  ("localhost", "u1094355_regac@000.00.0.00", "password") or die ('Error: ' . mysql_error());
mysql_select_db ("db1094355_downloadsregistry");
 
$query="INSERT INTO Registry (ID, FirstName, LastName, Email, Company, Phone, City, State) VALUES ('NULL', $_POST[FirstName]','$_POST[LastName]','$_POST[Email]','$_POST[Company]','$_POST[Phone]','$_POST[City]','$_POST[State]')";
 
mysql_query($query) or die ('Error updating database');
 
echo "Database Updated.";
?>
 
But when the form goes through i get:
Error: Can't connect to MySQL server on 'localhost' (10061)
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: basic writting to php

Post by califdon »

FranzS wrote:Error: Can't connect to MySQL server on 'localhost' (10061)
This is the only pertinent part of your post. If you can't connect to the server, it doesn't matter what other code you have.

The address of the server doesn't look valid to me. It is normally either a web URL or a local network address or a local machine alias, such as "localhost". Where is the MySQL server located? If it's on your local network, you need to determine the correct address for it.
FranzS
Forum Newbie
Posts: 7
Joined: Sat Dec 26, 2009 12:07 am

Re: basic writting to php

Post by FranzS »

um its paid hosting at dotster, not my network. i think localhost should work, i'm a noob with php, i was able to make the SQL database and am fairly sure that i am using the correct database name username and password. The database controls give me the ability to create tables, though i think the php should do that. Could that be it?
I have no idea, but i have admin access, is there some setting i should look for to make sure the php can access the database? is there supposed to be some sort of mysql file in the directory? The host might use a different database server because i don't see any file with the database's name on ftp.
Any tips?
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: basic writting to php

Post by califdon »

OK, yes, I now understand. Also, I see that I must have been in a hurry before and misinterpreted your username as the hostname.

"Localhost" refers to when the server is on the same physical computer as is running the PHP script. When you have paid hosting, that's never the case, because their database servers may get a lot of traffic, so they always (as far as I know) locate them on a different physical machine. Try replacing "localhost" with the name of your domain, like "abcxyz.org" and see if the rest of it works.

If you have both a web server and a mysql server installed on your computer workstation, then you would use "localhost".
FranzS
Forum Newbie
Posts: 7
Joined: Sat Dec 26, 2009 12:07 am

Re: basic writting to php

Post by FranzS »

K I tried using my website address instead of localhost and got the same error. I went into the database manager and it gives me the following information:
Server: 172.20.18.53 via TCP/IP

User: u1094355_regac@172.16.2.14
Now i'm getting this new error that i'm calling error 2:

Error 1:
Error: Can't connect to MySQL server on 'localhost' (10061)
Error 2:
Error: Access denied for user 'u1094355_regac@1'@'172.20.16.77' (using password: YES)
So I input the server it gives me up there(172.20.18.53) instead of the "localhost" and when i ran the php it gives me error 2.

I did also try the ip that the account says @(172.16.2.14) and that gave me the old error message(Error 1).

I am puzzled as to why the user account it gives me is different than the one in the error message(I also tried the address from the error message and got Error 2)

Also wondering what's up with the user that comes out in error 2... 'u1094355_regac@1'@'172.20.16.77' with the weird 1' in between the incomplete account and the server.... hope its not too confusing but thanks for the help... i'm thinking it might have something to do with the "via tcp/ip" readout that my database manager gives me
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: basic writting to php

Post by califdon »

Your hosting tech support should be able to clear this up for you. What you need to know is that hosting services have many (hundreds or thousands) of servers that handle its customers requirements for web, mail, ftp, database and other servers, and every one of them has its own different IP address. That's why your web server IP address is not the same as your mysql server or your ftp server.

Your "error 2" indicates that you ARE connecting to the mysql server, but it's not recognizing your login, probably due to whatever is causing your user name to be truncated. I don't have any explanation for why it is truncated. But that confirms that THAT is the correct IP address for the mysql server. You have to learn to read closely what those error messages are telling you. "Error 1" said simply that it couldn't connect to the database server, so obviously nothing else can work. "Error 2" was coming FROM the database server, telling you that it was denying you access, so obviously you WERE connecting to it.

Your best bet is to contact tech support. They should be able to have you up and running within minutes.
FranzS
Forum Newbie
Posts: 7
Joined: Sat Dec 26, 2009 12:07 am

Re: basic writting to php

Post by FranzS »

Before doing that I tried some more tweaks and finally found one that works,
I just took the "@172.16.2.14" from the username u1094355_regac@172.16.2.14
But now its giving me "Error updating database" when the php runs. This message comes from the or die at bottom of this part of code, because the query failed.

Code: Select all

$query="INSERT INTO Registry (ID, FirstName, LastName, Email, Company, Phone, City, State, Apptone) VALUES ('NULL', $_POST[FirstName]','$_POST[LastName]','$_POST[Email]','$_POST[Company]','$_POST[Phone]','$_POST[City]','$_POST[State]','$_POST[Apptone]')";
 
mysql_query($query) or die ('Error updating database');
 
echo "Database Updated.";
?>
So I went to the database manager again and the table "Registry" wasn't even created, though it has tools to let me make the table(which i don't understand... it asks me for a name and amount of fields... and takes me to this weird setup screen that i attached).

So...
is the code supposed to create the table? if not, how do I set it up? if so, what's wrong with my code?

Or is it just a permissions issue? what permissions should i check to be on?
Thanks for the continuous help, I'm learning a lot
Attachments
tableoptions.png
tableoptions.png (66.24 KiB) Viewed 1443 times
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: basic writting to php

Post by califdon »

What you are seeing is the phpMyAdmin software that makes it easier for you to work with your database. There's no more permission problem, you're looking at your database there. Your code was a query to INSERT a new record into an existing table named "Registry", so of course if there is no such table, it can't do that, so that's what it was telling you. Your screenshot is the appropriate screen to create a new table. Your query specifies 9 fields in the table that it is supposed to update: ID, FirstName, LastName, Email, Company, Phone, City, State, Apptone. For that query to work, your table must have at least those fields (it could have additional ones). That's what you have to create in your phpMyAdmin screen (there are alternative ways to create a table, but I don't want to confuse you). Creating the table is a one-time operation, since you can then add, update, or delete records once it exists. Under the first column ("Field"), you enter the field names, as above. In the next column ("Type") you must specify one of the standard data types that you can have in MySQL. All of your fields should be VarChar type (Variable length Character data) except the ID field. That field may be a problem, the way your query is constructed. Conventionally, a field named "ID" is the Primary Key field of a table, and is often specified to be an auto-increment field, under "Attributes", meaning that every time you add a new record to the table, MySQL will assign the next higher number as that field, assuring that there are NO NULLS and NO DUPLICATE VALUES, which are never allowed in a Primary Key field. But since your query is assigning a string to that field, with the letters N U L L in it, it cannot be your Primary Key field, so there's a problem there. Ordinarily, there would be no mention of the ID field in that INSERT query, and the database would take care of that for you.

You need to study a little about how a MySQL database works and how to use phpMyAdmin. Only then will you be able to write any PHP code that interacts with the database.
Post Reply