Problem with redirect and file don't save on database

Hi, i have a problem with this code i think

save.php

<?php
	require_once 'conn.php';
	if(ISSET($_POST['save'])){
		$image_name = $_FILES['photo']['name'];
		$image_temp = $_FILES['photo']['tmp_name'];
		$titolo = $_POST['titolo'];
		$info = $_POST['info'];
		$seller = $_POST['seller'];
		$exp = explode(".", $image_name);
		$end = end($exp);
		$name = time().".".$end;
		$path = "/htdocs/upload/".$name;
		$allowed_ext = array("gif", "jpg", "jpeg", "png");
		if(in_array($end, $allowed_ext)){
			if(move_uploaded_file($image_temp, $path)){
				mysqli_query($conn, "INSERT INTO `user` VALUES('', '$titolo', '$info', '$seller', '$path')") or die();
				echo "<script>alert('User account saved!')</script>";
				header("location: index.php");
			}	
		}else{
			echo "<script>alert('Image only')</script>";
		}
	}
?>

I think that when it comes to inserting something happens because nothing is loaded on the database and it crashes before redirecting you to the index

Can anyone help me?
Thanks in advance!

conn.php

<?php

	$server ="sql102.epizy.com";
	$username ="epiz_31069728";
	$password ="thepasswordisright";
	$dbname ="epiz_31069728_img";

	$conn = new mysqli($server, $username, $password, $dbname);
	
	if(!$conn){
		die("Error: Failed to connect to database!");
	}
?>

If you enable Display Errors (can be done through cPanel), is there any error? Also, can you share a URL?
Thanks!

i’m new you can explain how i can to enable Display Error?
I think it’s the problem is the database, now save.php works, i replace $path with image_name

The problem is that INSERT INTO don’t work the table is always empty

ISSET() is not a PHP function, however, isset() is (notice the capitalization). And because you didn’t specify an else block, you code ends without an error. Try fixing that and see if it works.

2 Likes

By putting the path like this, you’re trying to write files to a folder called htdocs at the root level of the server, not the root level of your website.

You’ll probably want to use this something like this instead instead:

$path = $_SERVER['DOCUMENT_ROOT']."/upload/".$name;
4 Likes

Ok Now?

I change it but the problem is that don’t save anything on the database, why?


can help? sorry it is in italian

New save.php file

<?php
	require_once 'conn.php';
	if(isset($_POST['save'])){
		$image_name = $_FILES['photo']['name'];
		$image_temp = $_FILES['photo']['tmp_name'];
		$titolo = $_POST['titolo'];
		$info = $_POST['info'];
		$seller = $_POST['seller'];
		$exp = explode(".", $image_name);
		$end = end($exp);
		$path = $_SERVER['DOCUMENT_ROOT']."/upload/". $image_name;
		$allowed_ext = array("gif", "jpg", "jpeg", "png", "PNG");
		if(in_array($end, $allowed_ext)){
			if(move_uploaded_file($image_temp, $path)){
				mysqli_query($conn, "INSERT INTO `user` VALUES ('', $titolo', '$info', '$seller', '$image_name')") or die("f**k");
				header("location: index.php");
			}	
		}else{
			echo "<script>alert('Image only')</script>";
		}
	}
?>

i think the problem is on INSERT because send me the message

I would agree. Look at your quotation marks, they are clearly incorrect.

it stell doesen’t work

mysqli_query($conn, "INSERT INTO `user` VALUES ('', '$titolo', '$info', '$seller', '$image_name')") or die("f**k");

“It does not work” is not helpful. What is the error massage? Where are things going wrong? Is your MySQL table setup correctly? I see a 404 in the console, so what file is missing?

1 Like

http://fkresell.rf.gd this is the site,


In your bottom picture, it looks like it worked, no?

Since it works in localhost I imported the table and loads it and I can also modify and delete it from the site but here the save.php does not work because it does not save things in the database and in the upload folder the images uploads I do not know how to solve

Cannot see images

So you are saying that the problem now has nothing to do with the database, but the fact that the files are never saved?

What I am trying to say is that my code in save.php (that string with INSERT) does not insert anything inside the database table

To try and get the error do:

if(mysqli_query($conn, "INSERT INTO `user` VALUES('', '$titolo', '$info', '$seller', '$path')")) {
  echo "<script>alert('User account saved!')</script>";
  header("location: index.php");
} else {
 echo mysqli_error($conn);
}
6 Likes

This is the error message that show
Incorrect integer value: ‘’ for column epiz_31069728_img.user.id at row 1