Jump to content

Site has stopped uploading data


Go to solution Solved by gizmola,

Recommended Posts

At the end of March godaddy moved our site.  I am now running php 5.6 on lynix.  Just noticed yesterday that no data is being uploaded to the site since the end of March which is around when it was moved.  Is there anything in the following code that isn't compatible with 5.6 and needs changed?  I wrote this code 5 or 6 years ago and am not well versed in php.   It has been runing fine until the end of March.  Thanks for any help.

//code

// Create connection
date_default_timezone_set("America/New_York");
$connect = new mysqli($servername, $username, $password, $dbname);
if ($_FILES['csv']['size'] > 0) {
    //get the csv file
    mysqli_query($connect,'TRUNCATE TABLE users');
    $file = $_FILES['csv']['tmp_name'];
    $handle = fopen($file, "r");
    $i = 0;
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
                     
        if ($i > -1) {
$import = "INSERT into users(id,lname,fname,email,password,indx,hdcp,red,phone,hide) values('$data[0]','$data[1]','$data[2]','$data[3]','".md5($data[4])."','$data[5]','$data[6]','$data[7]','$data[8]','$data[9]')";
            $connect->query($import);

        }
        $i++;
    }
mysqli_query($connect,'TRUNCATE TABLE control');
$date = date( "Y-m-d" );
$import="INSERT INTO control (tdate) VALUES('$date')";
$connect->query($import);
    fclose($handle);
   print "<p>File uploaded successfully!";
}
}
?>
</font>
</center>
</BODY>
</HTML>

//endcode

 

Link to comment
Share on other sites

Hi..  Thanks for your reply... 

The to part of the script is login info with passwords, etc.

I get this error......

[06-May-2024 15:31:52 America/New_York] PHP Warning:  mysqli::query(): Couldn't fetch mysqli in /home/rgxb6tc5wk5q/public_html/golf/login/upload.php on line 68
[06-May-2024 15:31:52 America/New_York] PHP Warning:  mysqli_query(): Couldn't fetch mysqli in /home/rgxb6tc5wk5q/public_html/golf/login/upload.php on line 73
[06-May-2024 15:31:52 America/New_York] PHP Warning:  mysqli::query(): Couldn't fetch mysqli in /home/rgxb6tc5wk5q/public_html/golf/login/upload.php on line 76

I believe line 68 is the while...

//code

 while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
                     
        if ($i > -1) {
$import = "INSERT into users(id,lname,fname,email,password,indx,hdcp,red,phone,hide) values('$data[0]','$data[1]','$data[2]','$data[3]','".md5($data[4])."','$data[5]','$data[6]','$data[7]','$data[8]','$data[9]')";
            $connect->query($import);

//code

Could this have something to do with the csv file?

Link to comment
Share on other sites

those errors are because the database connection is failing or you have some code that's closing the connection, and you are on a php version so old that's there's no default error handling for database statements that can fail. i also doubt that's the code where these error are coming from, because only the procedural mysqli_query() calls produce warnings. The OOP $connect->query() calls would result in fatal runtime errors (or perhaps the php version is so old that this hasn't been implemented yet.)

to get error handling for all the mysqli database statements that can fail - connection, query, exec, prepare, and execute, add the following line of code before the point where you make the database connection (wonder if this has the desired effect on such an old version of php) -

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

also, after you make the database connection, you need to set the character set to match your database tables. add this code after the point where you make the database connection -

$connect->set_charset('utf8mb4'); // repleace as needed if this is not the character set you are using

 

Link to comment
Share on other sites

There's any number of things that could be problematic but that "error" is specifically a "warning".  In general warnings are things you might need to be aware of, but don't rise to the level of an actual runtime error.  

The line you think is 68 doesn't run a mysqli query, so that is not the source of the problem.  Given the offset of the errors it looks like it's the INSERT statement inside the loop that is failing.  Other queries to follow that try to truncate the database and insert into the control table also fail.

This error tends to be associated with trying to use a mysql connection that isn't open or was open and then closed by erroneous code.  

Since you left out some code, it's hard to be certain, but an educated guess makes me think that the database connection is failing, since you never check in this line:

 

$connect = new mysqli($servername, $username, $password, $dbname);

You should check that with:  

if (!$connect = new mysqli($servername, $username, $password, $dbname)) {
    echo "<p>Could not connect to the database</p>";
}

 

Link to comment
Share on other sites

Thanks for the help and suggestions.  I am sort of at a loss on this it has been so long.  I am going to include all the code as it seems what I didn't include last time is the problem.  Is it possible that when godaddy moved it some of the login credentials were changed?  The move caused some other problems.   I had the login set up for local or server access. 

//code

<!DOCTYPE html>

<HTML>

<HEAD>

<TITLE>CBMGA</TITLE>

<link rel="stylesheet" href="css/style.css">

<style>

body, html {

   height: 65%;

   margin: 0;

}

.bg {

   background-image: url("ccb.jpg");

   height: 100%;

   background-text: testing;

   background-position: center;

   background-repeat: no-repeat;

   background-size: cover;

}

</style>

</HEAD>

<BODY>

<p>

<div class="bg"></div>

<!--<DIV id="header"></DIV>-->

<center>

<font face="arial">

<H2>COCOA BEACH MEN'S GOLF ASSOCIATION</H2><p>

Browse for c:\golfcb\info\users.csv then click on the 'submit' button below.

<?php if (!$_POST) { ?>

<html>

    <body>

        <form action="" method="post" enctype="multipart/form-data">

            Choose your file: <br /> <p>

            <input name="csv" type="file" id="csv" /> <br /> <br />

            <input type="submit" name="Submit" value="Submit" />

        </form>

    </body>

</html>

<?php

} else {

if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {

    //echo 'This is a server using Windows!';

   $servername = "";

   $username = "";

   $password = "";

   $dbname = "";

} else {

    //echo 'This is a server not using Windows!';

   $servername = "";

   $username = "";

   $password = "";

   $dbname = "";

}

// Create connection

date_default_timezone_set("America/New_York");

$connect = new mysqli($servername, $username, $password, $dbname);

if ($_FILES['csv']['size'] > 0) {

    //get the csv file

    mysqli_query($connect,'TRUNCATE TABLE users');

    $file = $_FILES['csv']['tmp_name'];

    $handle = fopen($file, "r");

    $i = 0;

    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {

                     

        if ($i > -1) {

$import = "INSERT into users(id,lname,fname,email,password,indx,hdcp,red,phone,hide) values('$data[0]','$data[1]','$data[2]','$data[3]','".md5($data[4])."','$data[5]','$data[6]','$data[7]','$data[8]','$data[9]')";

            $connect->query($import);

        }

        $i++;

    }

mysqli_query($connect,'TRUNCATE TABLE control');

$date = date( "Y-m-d" );

$import="INSERT INTO control (tdate) VALUES('$date')";

$connect->query($import);

    fclose($handle);

   print "<p>File uploaded successfully!";

}

}

?>

</font>

</center>

 

 

</BODY>

</HTML>

//code

Link to comment
Share on other sites

On 5/8/2024 at 4:48 PM, gizmola said:

You should check that with:  

an OOP new mysqli() call always returns an object, regardless of if the connection worked or failed.

 

@charlie321, you must always have error handling for database statements that can fail - connection, query, exec, prepare, and execute. adding the mysqli_report() statement i showed should (unknown if this works for such an old php version) enable exceptions for mysqli errors. if that doesn't work for your php version, you will need to add logic to test the value returned by mysqli_connect_error() (even the OOP mysqli::$connect_error property didn't initially work.)

you also need to have post method form detecting and error handling for the file upload. if the size of the form data is greater than the post_max_size setting, both the $_POST and $_FILES arrays will be empty. once your has detected that there is $_FILES data, you must test the ['error'] element of the uploaded file information to make sure that the file was successfully uploaded before using the uploaded file data. because you are directly reading the temporary file data, you need to use is_uploaded_file() first, before opening and reading the file.

Link to comment
Share on other sites

  • Solution
9 hours ago, charlie321 said:

Thanks for the help and suggestions.  I am sort of at a loss on this it has been so long.  I am going to include all the code as it seems what I didn't include last time is the problem.  Is it possible that when godaddy moved it some of the login credentials were changed?  The move caused some other problems.   I had the login set up for local or server access. 

Right -- well as I said, yes it looks like there is something different about the login credentials for the database.  Typically on Godaddy shared hosting the mysql database is a shared resource, and they should document what you need to use for the host string, as well as being able to set your login/password for a specific database.  I would suggest you seek that info from GoDaddy.

Link to comment
Share on other sites

Thanks for the help and suggestions.  Happy to say I didn't have to go to godaddy.  The password evidently wasn't strong enough so they changed it without informing me of the change.  It is fixed.  Didn't know how marking the worked so this is the solution.  I'll try to fix it if I can.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.