0

I use this following codes to make new users in website. it's like sign up page. it's working perfectly in localhost and data will insert to the database and the table but now in the server it won't work.

  $code = rand(11111,99999);
    $link1 = mysql_connect("http://www.mysite.com","username","password");
     mysql_select_db("username");
     mysql_query("SET character_set_results= 'utf8' , character_set_client = 'utf8' , character_set_connection = 'utf8',
     character_set_database ='utf8', character_set_server = 'utf8' ");
     mysql_query("INSERT into user(name,family,email,gender,username,pass)
     values('$_POST[name]','$_POST[family]','$_POST[email]','$_POST[gender]','$_POST[username]','$code')");
     mysql_close($link1);
     //connection user pass
     $link2 = mysql_connect("http://www.mysite.com","username","password");
     mysql_select_db("username");
     mysql_query("INSERT into joinuser(username,pass,permission)values('$_POST[username]','$code','user')" );
     mysql_close($link2);

It inserts the values in localhost but it won't work in the server.

It's php with mysql database.

Any help will be appreciated.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • try using the ip address of ur database server in place of www.mysite.com – Ghostman Nov 04 '12 at 18:01
  • You are using [an obsolete database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). You are also exposing yourself to [SQL injection attacks](http://bobby-tables.com/) that a modern API would make it easier to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Nov 04 '12 at 18:02
  • 3
    [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained and the [deprecation process](http://j.mp/Rj2iVR) has begun on it. See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://j.mp/PoWehJ). Your code is *extremely* vulnerable to SQL injection. – Madara's Ghost Nov 04 '12 at 18:02
  • If the page is on the server, wouldn't be "localhost" instead of "www.mysite.com" ? – Vucko Nov 04 '12 at 18:03

2 Answers2

1

"http://www.mysite.com" is wrong. You cannot connect to MySQL using HTTP.

In most cases, you will only ever need to use localhost and allow the library to connect via a local socket rather than TCP/IP.

If you are connected to a database server running on a different machine to the HTTP server, then you would use the hostname (and optionally the port). You wouldn't specify a URL scheme of any kind.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

Most probably this will be the solution of the problem.

//mysql_connect("http://www.mysite.com","username","password");
mysql_connect("localhost","username","password");

Notice the localhost in stead of http://www.mysite.com.

MD. Sahib Bin Mahboob
  • 20,246
  • 2
  • 23
  • 45