0

I developed web form and connected to mysql db. When I tested on local hosting (on my computer) it worked ok, all INSERT queries were done successfully, but when I uploaded on hosting (tried on 2 hosting companies) all Inserts in db are empty.

What problem can be?

My code

$db = mysqli_connect('localhost', 'cy57447_forms', '123456789', 'cy57447_forms');

if(isset($_POST['register'])) {
    $email = mysql_real_escape_string($_POST['email']);
    $tg_username = mysql_real_escape_string($_POST['tg_username']);
    $bitcointalk_username = mysql_real_escape_string($_POST['bitcointalk_username']);
    $bitcointalk_profile_link = mysql_real_escape_string($_POST['bitcointalk_profile_link']);
    $twitter_account_url = mysql_real_escape_string($_POST['twitter_account_url']);
    $number_of_followers = mysql_real_escape_string($_POST['number_of_followers']);
    $twitter_audit_link = mysql_real_escape_string($_POST['twitter_audit_link']);
    $ethereum_wallet = mysql_real_escape_string($_POST['ethereum_wallet']);
    $tokens = '';
}

    $sql = "INSERT INTO twitter_form (`tg_username`, `bitcointalk_username`, `bitcointalk_profile_link`, `twitter_account_url`, `number_of_followers`, `twitter_audit_link`, `ethereum_wallet`, `email`, `tokens`) VALUES ('$tg_username', '$bitcointalk_username', '$bitcointalk_profile_link', '$twitter_account_url', '$number_of_followers', '$twitter_audit_link', '$ethereum_wallet','$email', '');";
    $result = mysqli_query($db, $sql);
  • Encountered any SQL connection error? Some hosting company may not host DB together with the server, hence "localhost" as the host address may not work. – Mark Seah Jul 04 '18 at 09:16
  • It would probably help if you would also include a snippet of the HTML page that contains the form which submits data to the PHP page. This could also be the source of the problem. – Reboot Jul 04 '18 at 09:17
  • Preferably change to using prepared statements in mysqli_, but changing to `mysqli_real_escape_string` may work. – Nigel Ren Jul 04 '18 at 09:19
  • are you sure you've configured mysql with your hosting running on default port 80 – popeye Jul 04 '18 at 09:19
  • 1
    Isn't connection to database, 3306? port 80 is for http connection. – Mark Seah Jul 04 '18 at 09:20

2 Answers2

0

Check your host, username, password, and database names are all correct?

To help your debugging process I would suggest using a try catch around your connection attempt

    try{
    $db = mysqli_connect('localhost', 'cy57447_forms', '123456789', 'cy57447_forms');
         //if condition
         $sql = "INSERT INTO twitter_form
                    ( ` tg_username ` ,
                     ` bitcointalk_username ` ,
                     ` bitcointalk_profile_link ` ,
                     ` twitter_account_url ` ,
                     ` number_of_followers ` ,
                     ` twitter_audit_link ` ,
                     ` ethereum_wallet ` ,
                     ` email ` ,
                     ` tokens ` )
        VALUES      ('$tg_username',
                     '$bitcointalk_username',
                     '$bitcointalk_profile_link',
                     '$twitter_account_url',
                     '$number_of_followers',
                     '$twitter_audit_link',
                     '$ethereum_wallet',
                     '$email',
                     '');";
    } catch (Exception $e) {
        $error = [
            'error' => $e->getCode(),
            'message' => $e->getMessage(),
            'traceString' => $e->getTraceAsString()
        ];
        var_dump($error);
    }
        $result = mysqli_query($db, $sql);
munsifali
  • 1,732
  • 2
  • 24
  • 43
  • This is more a debugging aid rather than a solution to the problem. Also not sure if adding extra spaces around the column names in the backticks will work. – Nigel Ren Jul 04 '18 at 09:25
  • Not adding extra spaces on Query I have just used sql formatter to format the query. – munsifali Jul 04 '18 at 09:27
0

I suggest for you to check this line.

$db = mysqli_connect('localhost', 'cy57447_forms', '123456789', 'cy57447_forms');

Codes look fine to me.

  1. If the database on the hosting company local or there is another endpoint/url to connect to for database. It's not all the time localhost on the hosting server.

  2. The username and password to the database. If you go through some wizard on Cpanel etc, use those password and username.

  3. The name of the database, make sure there is no prefix etc.

Thanks to @Nigel Ran in the comments. You may also want to try to change

mysql_real_escape_string

to

mysqli_real_escape_string
Mark Seah
  • 532
  • 5
  • 18