BrackNC
February 14, 2022, 11:33pm
1
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!
BrackNC
February 14, 2022, 11:41pm
2
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!
BrackNC
February 14, 2022, 11:57pm
4
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
BrackNC
February 15, 2022, 12:25am
5
BrackNC:
ha
The problem is that INSERT INTO don’t work the table is always empty
BrackNC:
ISSET
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.
There are two different ways to enable PHP errors.
1. Via CPanel
Go to CPanel->Alter PHP Config->Alter PHP Directives now click on on button of display errors radio group.
[IMG_20201030_160659]
2. Via .htaccess
Just paste this line in your .htaccess file.
php_value display_errors On
2 Likes
Admin
February 15, 2022, 2:54pm
7
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
BrackNC
February 15, 2022, 5:29pm
9
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.
BrackNC
February 15, 2022, 5:59pm
11
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
BrackNC
February 15, 2022, 6:06pm
13
In your bottom picture, it looks like it worked, no?
BrackNC
February 15, 2022, 7:17pm
16
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
So you are saying that the problem now has nothing to do with the database, but the fact that the files are never saved?
BrackNC
February 15, 2022, 7:38pm
19
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
YT_Xaos
February 15, 2022, 7:59pm
20
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
BrackNC
February 16, 2022, 11:10am
21
This is the error message that show
Incorrect integer value: ‘’ for column epiz_31069728_img
.user
.id
at row 1