Jump to content

using php to access mysql database.


ardyandkari

Recommended Posts

hi, i am having a problem inputting data into my mysql database.  i am using an html form and php.  the form is just a basic form: name type of company, city, etc.  i have tried a few different things, but being a noob to php, dont really know what is wrong.

i will post the php code now and the error after:
[code]
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>

<body>
<?php
$username="user";
$password="name";
$database="password";

$first=$_POST['name'];
$last=$_POST['type'];
$phone=$_POST['phone'];
$mobile=$_POST['city'];
$fax=$_POST['county'];
$email=$_POST['email'];
$web=$_POST['web'];
$description=$_POST['description'];


mysql_connect(mysql.someserver.net,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$query = "INSERT INTO contacts VALUES ('','$name','$type','$phone','$city','$county','$email','$web','description')";
mysql_query($query);

mysql_close();
?>
</body>
</html>
[/code]

i have used mysql_connect(mysql_someserver_net,$username,$password); also.

this is the error message:

Warning: mysql_connect(): Unknown MySQL Server Host 'mysq_someserver_net' (0) in (path to the php page) on line 24
Unable to select database

any help would be very much appreciated.

thanks in advance.
ardy
Link to comment
Share on other sites

[quote author=ted_chou12 link=topic=121933.msg502207#msg502207 date=1168521740]
$query = "INSERT INTO contacts VALUES (''[color=red],[/color]'$name','$type','$phone','$city','$county','$email','$web','description')";

this is a syntax.
Ted
[/quote]

thanks for the quick reply.
i fixed the syntax.

i am still unable to connect to the database.  tried changing the connect to "mysql_connect(localhost,$username,$password);" now i get this as an error.

Warning: mysql_connect(): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) in (path to php page) on line 24
Unable to select database

super confused...thanks alot though for pointing out another problem.
[quote author=thorpe link=topic=121933.msg502213#msg502213 date=1168522615]
Its saying you havent specified a correct database server. mysql.someserver.net
[/quote]

for the address i copied it right from my host's mysql page.  unless they put up faulty addresses....
Link to comment
Share on other sites

[quote author=thorpe link=topic=121933.msg502237#msg502237 date=1168523879]
localhost needs to be a string...

[code=php:0]
mysql_connect("localhost",$username,$password);
[/code]

but the problem appears to be within your mysql servers configuration. Google for the specific error.
[/quote]

i dont understand what you mean by "string", do you mean it has to be the web address?

also, i have hosting through godaddy, so i dont think that it is something with the server.

thanks
Link to comment
Share on other sites

GoDaddy hosting does not use localhost. You need the specific server. You can find it in your setup.
Go into Control Panel, MySQL, then click on the first icon in the list of three, the tooltip says View Connection Strings
Link to comment
Share on other sites

This line:
[code]mysql_connect(mysql.someserver.net,$username,$password);[/code]
Should be ok without the quotes, because PHP will look for a defined value of each of those names, throw an error and then assume they are all one string (including the dots). Ask godaddy for your MySQL server, not the path to the local socket.
Link to comment
Share on other sites

ok...it connects now........THANKS to all who have helped out!!! it is super appreciated. Grin

now...

i tried the form, it comes back with no errors, which i assume means that everything went well.

but when i check the database, it doesn't show that there are any records.  does it take some time for the entries to get put into the database?

i took all the code from a tutorial page, so i was assuming that everything worked until i was directed to a syntax error earlier...

thanks in advance...
ardy
Link to comment
Share on other sites

[quotes]Should be ok without the quotes, because PHP will look for a defined value of each of those names, throw an error and then assume they are all one string[/quote]

Brilliant advice to leave it then huh?
Link to comment
Share on other sites

ok...i checked the mysql account and it says that i have 0 records.  im lost.

ill post the code again...

[code]<?
$hostname="mysql.someserver.net";
$username="blah blah";
$password="not really";
$dbname="database";
$usertable="table";
$first=$_POST['name'];
$last=$_POST['type'];
$phone=$_POST['phone'];
$mobile=$_POST['city'];
$fax=$_POST['county'];
$email=$_POST['email'];
$web=$_POST['web'];
$description=$_POST['description'];

mysql_connect($hostname,$username, $password) OR DIE ("Unable to connect to database! Please try again later.");
mysql_select_db($dbname);

$query = "INSERT INTO customers VALUES ('$name','$type','$phone','$city','$county','$email','$web','description')";
mysql_query($query);

mysql_close();
?>[/code]

like i said...the php page doesnt return an error anymore, but it also doesnt input any data into my database...

just a long shot, tell me if i am right...
if i use the GET command it will not affect the db either, because GET doesnt place data, it only "get"s it.........
Link to comment
Share on other sites

you guys are awesome!!! :D

thanks alot redbullmarky for the fix.

i am super grateful to you for allowing me to pick your brains...

as i learn php more, i hope to be able to actually contribute to the site. 
thanks again to everyone for all the help, and i will probably be back!

later
ardy
Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.