Trying to uploading data using PHP and HTML form to SQL DATABASE

Website URL

(please specify the URL of the site on which you are experiencing the problem)

Error Message

I have made an upload form for the SQL database but when uploading data receive this error message: https://errors.infinityfree.net/errors/404/

Other Information

(other information and details relevant to your question)

404 means the file does not exist. Make sure the file is uploaded to the same location you are trying to view in the browser, and note that our servers are cAse sEnsiTivE.

3 Likes

Both files are in the root directory as you can see in the screenshot, I have not seen any issues like cAse sEnsiTivE, and the SQL database connection settings are also correct.

Can you share the contents of your .htaccess file here?

1 Like
RewriteEngine On
RewriteRule ^$ /wallpaper/fantasy/index.php [L]

Remove "index.html" from the end of the URL
RewriteCond %{REQUEST_URI} ^(.*)/index\.php$ [NC]
RewriteRule . %1/ [L,R=301]

Remove specific filenames from the end of each URL
RewriteCond %{REQUEST_URI} ^/(.+)\.php$ [NC]
RewriteRule . /%1/ [L,R=301]

Try this code instead

RewriteEngine on
RewriteRule ^$ /wallpaper/fantasy/index.php [L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.html [NC]
RewriteRule ^ %1 [R,L,NC]

From https://tinkertechlab.com/webhosting/htaccess/remove-file-extensions

4 Likes

except this path: /wallpaper/fantasy/index.php
both the pages crashed: /wallpaper/anime/index.php , /wallpaper/vector/index.php

I think problem is not lies in .htaccess
I need to write new code to connect html and php to upload data in sql, Interesting thing is I can go to infinityfree server and can use insert command to upload the data in SQL but I want to give visiter an option to insert there own data to SQL Table.

I’m sorry, but I don’t fully understand what the desired situation is exactly, or what you see instead.

The Anime, Vector and Anime pages all seem to be working fine from here, but I don’t see any upload functionality on any of the pages. Can you tell us what URL, button or link triggers the 404 error?

4 Likes

I’m sorry for the misunderstanding. All the files are and were showing. I previously had an issue with upload.php and userform.html, which were supposed to insert data into the SQL database. However, upload.php was not able to connect to the SQL database, causing a crash that triggered a 404 error. Currently, I have removed both the files “upload.php” and “userform.html” and am working on new ones.

PHP is unable to establish a connection with the SQL database. I’m trying to insert data through a simple HTML form and PHP, but causing 404 error. After clicking the “Submit Data” button in the ‘index.html’ link that is supposed to trigger ‘insert_data.php,’ it triggers the file, but ‘insert_data.php’ is unable to establish a connection with the SQL database causing 404 error.
Screenshot 2023-10-09 032951
Screenshot 2023-10-09 033116

Can you share the code of your insert_data.php file, censoring anything sensitive like passwords?

1 Like
<?php
// Database connection settings
$host = "sql313.infinityfree.com";
$username = "epiz_33922881";
$password = "XXXXXXXXXX";
$database = "epiz_33922881_images";

// Create a connection to the database
$conn = new mysqli($host, $username, $password, $database);

// Check if the connection was successful
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Retrieve data from the form
$img_src = $_POST['img_src'];
$button_link = $_POST['button_link'];
$alt_text = $_POST['alt_text'];
$category = $_POST['category'];

// Loop through the submitted data and insert it into the card_items table
for ($i = 0; $i < count($img_src); $i++) {
    $sql = "INSERT INTO card_items (img_src, button_link, alt_text, category) VALUES (?, ?, ?, ?)";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("ssss", $img_src[$i], $button_link[$i], $alt_text[$i], $category[$i]);

    if ($stmt->execute()) {
        echo "Data inserted successfully for record $i<br>";
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }

    $stmt->close();
}

// Close the database connection
$conn->close();
?>

1 Like

Thank you! I can see nothing wrong with the code, it should work fine.

The redirect to 404 is caused by your .htaccess rules; this is because while the .php (and .html if you’re using the code shared above by @Greenreader9) extension is stripped from the URLs, the new URLs are not internally rewritten to load the PHP/HTML files, so the server tries to load files that don’t exist and redirects you to the 404 page.

Adding this below the PHP redirect code should help:

RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]

You can do the same for .html:

RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [L]
2 Likes

You redirect all your traffic to index.php and if anyone try to access any .php file you remove it’s php suffix. What did you expect?

Try replacing your .htaccess code with the code I gave you.

I think what is happening is this:

  • The forum is submitted to https://www.DOMAIN.space/insert_data.php 1
  • .htaccess takes over and redirects to https://www.DOMAIN.space/insert_data/ 2
    • Notice the backslash on that last link, which tells Apache to look for a directory, not a file. Since no directory exists with the name “insert_data” (And, consequently, the file at /insert_data/index(.php/.html) does not exist), a 404 is thrown.

Using .htaccess code that is confirmed to work here should fix this, or at least eliminate .htaccess as a cause.

1
image

2
image

(No clue why you get a 302 here, that folder clearly does not exist ref)

Also, you need to remove the brackets from the name attributes in the HTML, or updated your PHP

image

4 Likes

The code you gave above seems to be broken; I tested it on my own account and it threw Error 404s until I added the lines mentioned in my message above.

Perhaps I’m doing something wrong? I updated it to your code again, try visiting http://do.epizy.com/info.php.

3 Likes

Thanks for verifying my php to sql file,

I did apply changes to .htaccess and now it is inserting data to SQL. Thank you so much for that.

But now I am facing new issue, my website url is “DOMAIN.space” and after adding it to browser it use to open “DKMAIN.space/wallpaper/fantasy/index.php but after applying your .htaccess code it showing me “www.DOMAIN.space” hiding “wallpaper/fantasy/index.php” from url but I just want to hide “index.php” from the end of url, and when going to other directory like “wallpaper/vector/index.php” or “wallpaper/anime/index.php” it crashed both the page,

Just to be clear “index.html” in root is not my home page file it’s form that trigger “insert_data.php”
my current .htaccess:


RewriteEngine On

RewriteRule ^$ /wallpaper/fantasy/index.php [L]

RewriteCond %{REQUEST_FILENAME}^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,NC,L]

RewriteCond %{REQUEST_FILENAME}^[A-Z]{3,}\s([^.]+)\.html [NC]
RewriteRule ^ %1 [R,NC,L]

Solution:

RewriteEngine On

RewriteRule ^$ /wallpaper/fantasy/index.php [L]

RewriteCond %{REQUEST_URI} ^(.*)/index\.php$ [NC]
RewriteRule . %1/ [L,R=301]
3 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.