What's new

Bakit kaya di di nasasave yun picture sa database

loktoy

Eternal Poster
Joined
Oct 20, 2014
Posts
1,166
Solutions
2
Reaction
103
Points
496
Age
30
di nasasave yun picture sa database pero nasasave sya folder at yun ibang info kagaya ng email, password, fullname, nickname nasasave naman..problima ko lang is yun picture di sya nasasave sa database.

ito code ko..

<?php
include "./config/config.php";
$payee_name = $email_address = $username = $password = '';
$payee_nameErr = $email_addressErr =$usernameErr =$passwordErr = '';
if($_SERVER['REQUEST_METHOD'] == 'POST') {
if(empty($_POST['payee_name'])){
$payee_nameErr = "Payee Name is required!";
}else{
$payee_name = $_POST['payee_name'];
}
if(empty($_POST['email_address'])){
$email_addressErr = "Email Address is required!";
}else {
$email_address = $_POST['email_address'];
}
if(empty($_POST['username'])) {
$usernameErr = " Username os required!";
}else {
$username = $_POST['username'];
}
if(empty($_POST['password'])){
$passwordErr = "Password is required!";
}else {
$password = $_POST['password'];
}
if ($payee_name && $email_address && $username && $password ) {
$check_email = mysqli_query($con, "SELECT * FROM registration WHERE payee_name = '$payee_name'");
$check_email_row = mysqli_num_rows($check_email);
if($check_email_row > 0 ){
$payee_nameErr = "Name is already registered!";
}else {
$slq= mysqli_query($con, "INSERT INTO registration (payee_name, email_address, username, password, user_type) VALUES ('$payee_name', '$email_address', '$username', '$password', '2')");

echo "<div class='alert alert-success' role='alert'>
A simple success alert—check it out!
</div>";

}

}
}

?>
<!DOCTYPE html>
<HTMl:5>
<head>
<title>signup</title>
<link rel="stylesheet" href="../Innerjoin/css/bootstrap.css">
<link rel="stylesheet" href="../Innerjoin/css/style.css">
<script src="../Innerjoin/js/bootstrap.bundle.js"></script>
</head>
<body>
<?php




?>

<form method="POST" action="<?php htmlspecialchars($_SERVER['PHP_SELF']); ?>" enctype="multipart/form-data">
<!-- <div
<div class="modal-dialog" role="document">
<div class="modal-content rounded-4 shadow">
<div class="modal-header p-5 pb-4 border-bottom-0">
<h1 class="fw-bold mb-0 fs-2">Sign up for free</h1> -->
<div class="body-background">
<div class="container col-xl-6 col-xxl-8 px-4 py-5 ">
<div class="modal-content rounded-4 shadow align-items-center bg-body-tertiary">
<div class="modal-header p-4 pb-2 border-bottom-0">
<h1 class="fw-bold mb-0 fs-2 ">BSWM REGISTRATION</h1>
</div>

<form class="">
<div class="container col-xl-10 ">
<div class="form-group mt-1 ">
<label for="exampleInputEmail1">Payee Name:</label>
<input type="text" class="form-control form-control-sm rounded-3" name="payee_name" value="" id="floatingInput" placeholder="Payee Name" required>
<small id="emailHelp" class="form-text text-muted">Registered your name base on your account</small>
</div>
<div class="form-group mt-2">
<label for="exampleInputEmail1">Email Address:</label>
<input type="text" class="form-control form-control-sm rounded-3" name="email_address" value="" id="floatingPassword" placeholder="Email Address" required >
</div>
<div class=" form-group mt-2">
<label for="exampleInputEmail1">Nickname:</label>
<input type="text" class="form-control form-control-sm rounded-3" name="username" value="" id="floatingInput" placeholder="Nickname" required >
</div>
<div class=" form-group mt-2">
<label for="exampleInputEmail1">Password:</label>
<input type="password" class="form-control form-control-sm rounded-3" name="password" value="" id="floatingPassword" placeholder="Password" required>
</div>
<div class="mt-3">
<label for="formFile" class="form-label ">Upload Image</label>
<input class="form-control" type="file" name="profile-pic" onchange ="displayPreview(this.files)" id="formFile">
</div>
<center>
<span id="preview"></span>
</center>
<div class="checkbox mt-3 text-body-secondary">
<label>
<small><input class="" type="checkbox" value="remember-me"> By clicking Sign up, you agree to the terms of use.</small>
</label>
</div>
<hr class="my-4">
<button class="w-100 mb-2 btn btn-sm btn-lg rounded-3 btn-primary" name="update" type="submit">Sign up</button>
<div class=" mb-3 text-center fs-6 fw-bold">
<label>
<small class="text-body-secondary"> Already have an Account <a href ="login.php">Login</a>.
</label>
</div>
</div>
</form>
</div>
</div>
</div>
</form>
<?php
$target_dir = "./photo_folder/";
$uploadErr = "";
if (isset($_POST["update"])){
$target_file = $target_dir . "/" . basename($_FILES["profile-pic"]["name"]);
$uploadOk = 1;
if(file_exists($target_dir)) {
$target_file = $target_dir . rand(1,9) . rand(1,9) . rand(1,9) . rand(1,9) . "_" .basename($_FILES["profile-pic"]["name"]);
$uploadOk = 1;
}
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
if($_FILES["profile-pic"]["size"] > 5000000000000000000 ) {
$uploadErr = "Sorry, your file is too large";
$uploadOk = 0;
}
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif") {
$uploadErr = "Sorry, only JPG, PNG, JPEG and GIF files are allowed";
$uploadOk = 0;
}
if($uploadOk == 1) {
if (move_uploaded_file($_FILES["profile-pic"]["tmp_name"], $target_file)){
$file_path = mysqli_real_escape_string($con, $target_file);
$slq = "INSERT INTO registration (img) VALUES ('$file_path')";
if ($con ->query($slq) === TRUE) {
echo "The file has been uploaded and the file path has been stored in the database" ;
}else {
echo "was error storing the file path in database" .$conn->error;
}

}else {
echo "sorry , there was an error uploading file.";
}}
}


?>
<script type = "application/javascript">
var _URL = window.URL || window.webkitURL;
function displayPreview(files){
var file = files[0];
var img = new Image();
var sizeKB = file.size / 1024;
img.onload =function(){
$(img).addClass('resized-image');
$('#preview').append(img);
}
img.src = _URL.createObjectURL(file);
}
</script>
</body>
</HTMl:5>
 
Based on your code, it seems that you are trying to upload an image and save its file path in the database. However, you are encountering an issue where the picture is not being saved in the database.

Here are a few suggestions to help you troubleshoot and fix the issue:

1. Check the Database Structure: Make sure that you have a column in your "registration" table to store the image file path. You can verify this by checking the table structure in your database.

2. Verify Database Connection: Ensure that your database connection is working correctly. You can check the "config.php" file to confirm that the connection details are correct.

3. Check File Upload Permissions: Make sure that the folder where you want to save the images has the necessary write permissions. You can set the folder permissions to 755 or 777 to ensure that the PHP script can save the files.

4. Debugging the Image Upload Process: You can add some debugging statements to check if the file upload process is working correctly. For example, you can add the following code snippet after the line if($uploadOk == 1) { in the code block where you handle the image upload:

PHP:
echo "File Name: " . $_FILES["profile-pic"]["name"] . "<br>";
echo "File Size: " . $_FILES["profile-pic"]["size"] . "<br>";
echo "Temp File: " . $_FILES["profile-pic"]["tmp_name"] . "<br>";
echo "Target File: " . $target_file . "<br>";

This will help you verify if the file details are correct and if the target file path is being generated properly.

5. Check Error Messages: Add some error handling to the file upload process to display error messages if any occur. For example, you can modify the move_uploaded_file line to include an else block with an error message:

PHP:
if (move_uploaded_file($_FILES["profile-pic"]["tmp_name"], $target_file)){
    // ... your existing code ...
} else {
    echo "Sorry, there was an error uploading the file: " . $_FILES["profile-pic"]["error"];
}

This will display any upload errors encountered by PHP and help you identify the issue.

By following these suggestions, you should be able to identify and resolve the issue with the image not being saved in the database.
 
Based on your code, it seems that you are trying to upload an image and save its file path in the database. However, you are encountering an issue where the picture is not being saved in the database.

Here are a few suggestions to help you troubleshoot and fix the issue:

1. Check the Database Structure: Make sure that you have a column in your "registration" table to store the image file path. You can verify this by checking the table structure in your database.

2. Verify Database Connection: Ensure that your database connection is working correctly. You can check the "config.php" file to confirm that the connection details are correct.

3. Check File Upload Permissions: Make sure that the folder where you want to save the images has the necessary write permissions. You can set the folder permissions to 755 or 777 to ensure that the PHP script can save the files.

4. Debugging the Image Upload Process: You can add some debugging statements to check if the file upload process is working correctly. For example, you can add the following code snippet after the line if($uploadOk == 1) { in the code block where you handle the image upload:

PHP:
echo "File Name: " . $_FILES["profile-pic"]["name"] . "<br>";
echo "File Size: " . $_FILES["profile-pic"]["size"] . "<br>";
echo "Temp File: " . $_FILES["profile-pic"]["tmp_name"] . "<br>";
echo "Target File: " . $target_file . "<br>";

This will help you verify if the file details are correct and if the target file path is being generated properly.

5. Check Error Messages: Add some error handling to the file upload process to display error messages if any occur. For example, you can modify the move_uploaded_file line to include an else block with an error message:

PHP:
if (move_uploaded_file($_FILES["profile-pic"]["tmp_name"], $target_file)){
    // ... your existing code ...
} else {
    echo "Sorry, there was an error uploading the file: " . $_FILES["profile-pic"]["error"];
}

This will display any upload errors encountered by PHP and help you identify the issue.

By following these suggestions, you should be able to identify and resolve the issue with the image not being saved in the database.
ok na kaso problima ko is kapag kpag nasasave na sya sa database sa ibang id sya so bali kapag nasasave ako ng info halimbawa nag save ako ng info at picture ang info ay nagiging id nya is 1 walang picture tapos yun picture nagiging id nya is 2 pero walang info
 
ok na kaso problima ko is kapag kpag nasasave na sya sa database sa ibang id sya so bali kapag nasasave ako ng info halimbawa nag save ako ng info at picture ang info ay nagiging id nya is 1 walang picture tapos yun picture nagiging id nya is 2 pero walang info
Based on the code you provided, it seems that you are using two separate SQL queries to save the registration information and the image file path. This can cause the issue you described, where the information and the image are saved with different IDs.

To resolve this issue, you need to ensure that both the registration information and the image file path are saved together using the same query.

Here's an updated version of your code that combines the two queries into one:

PHP:
<?php
include "./config/config.php";

$payee_name = $email_address = $username = $password = '';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    // ... your existing code ...

    if ($payee_name && $email_address && $username && $password) {
        $check_email = mysqli_query($con, "SELECT * FROM registration WHERE payee_name = '$payee_name'");
        $check_email_row = mysqli_num_rows($check_email);

        if ($check_email_row > 0 ) {
            $payee_nameErr = "Name is already registered!";
        } else {
            $target_dir = "./photo_folder/";
            $target_file = $target_dir . basename($_FILES["profile-pic"]["name"]);
            $uploadOk = 1;

            // ... your existing code ...

            if ($uploadOk == 1) {
                if (move_uploaded_file($_FILES["profile-pic"]["tmp_name"], $target_file)) {
                    $file_path = mysqli_real_escape_string($con, $target_file);

                    $query = "INSERT INTO registration (payee_name, email_address, username, password, user_type, img) VALUES ('$payee_name', '$email_address', '$username', '$password', '2', '$file_path')";
                    $result = mysqli_query($con, $query);

                    if ($result) {
                        echo "<div class='alert alert-success' role='alert'>A simple success alert—check it out!</div>";
                    } else {
                        echo "<div class='alert alert-danger' role='alert'>Error saving registration information</div>";
                    }
                } else {
                    echo "<div class='alert alert-danger' role='alert'>Sorry, there was an error uploading the file</div>";
                }
            }
        }
    }
}
?>

<!-- Your HTML form code here -->

In this updated code, the registration information and the image file path are both saved within the same if ($uploadOk == 1) block. This ensures that the data is saved together and avoids the issue of different IDs.

Make sure to adjust the code according to your specific requirements and database structure.
 
Based on the code you provided, it seems that you are using two separate SQL queries to save the registration information and the image file path. This can cause the issue you described, where the information and the image are saved with different IDs.

To resolve this issue, you need to ensure that both the registration information and the image file path are saved together using the same query.

Here's an updated version of your code that combines the two queries into one:

PHP:
<?php
include "./config/config.php";

$payee_name = $email_address = $username = $password = '';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    // ... your existing code ...

    if ($payee_name && $email_address && $username && $password) {
        $check_email = mysqli_query($con, "SELECT * FROM registration WHERE payee_name = '$payee_name'");
        $check_email_row = mysqli_num_rows($check_email);

        if ($check_email_row > 0 ) {
            $payee_nameErr = "Name is already registered!";
        } else {
            $target_dir = "./photo_folder/";
            $target_file = $target_dir . basename($_FILES["profile-pic"]["name"]);
            $uploadOk = 1;

            // ... your existing code ...

            if ($uploadOk == 1) {
                if (move_uploaded_file($_FILES["profile-pic"]["tmp_name"], $target_file)) {
                    $file_path = mysqli_real_escape_string($con, $target_file);

                    $query = "INSERT INTO registration (payee_name, email_address, username, password, user_type, img) VALUES ('$payee_name', '$email_address', '$username', '$password', '2', '$file_path')";
                    $result = mysqli_query($con, $query);

                    if ($result) {
                        echo "<div class='alert alert-success' role='alert'>A simple success alert—check it out!</div>";
                    } else {
                        echo "<div class='alert alert-danger' role='alert'>Error saving registration information</div>";
                    }
                } else {
                    echo "<div class='alert alert-danger' role='alert'>Sorry, there was an error uploading the file</div>";
                }
            }
        }
    }
}
?>

<!-- Your HTML form code here -->

In this updated code, the registration information and the image file path are both saved within the same if ($uploadOk == 1) block. This ensures that the data is saved together and avoids the issue of different IDs.

Make sure to adjust the code according to your specific requirements and database structure.
pano ko sya eecho para lumitaw o mag display sya sa output ng admin o user

pano ko sya eecho para lumitaw o mag display sya sa output ng admin o user
iooutput yun picture pano eecho
 
Am expecting that you are working on database Update query. You need to add "Where " clause to specific id you want to update. Kulang SQL statement mo sa Update statement something like this:
ex.
"INSERT INTO registration (img) VALUES ('$file_path') WHERE id =9";
Another thing is you need to fix the appropriate values na ilalagay mo, dapat nasa tamang order ng values ung field na gusto mo lagyan. refer to that chatgpt code sample:

$query = "INSERT INTO registration (payee_name, email_address, username, password, user_type, img) VALUES ('$payee_name', '$email_address', '$username', '$password', '2', '$file_path')";
 
Am expecting that you are working on database Update query. You need to add "Where " clause to specific id you want to update. Kulang SQL statement mo sa Update statement something like this:
ex.
"INSERT INTO registration (img) VALUES ('$file_path') WHERE id =9";
Another thing is you need to fix the appropriate values na ilalagay mo, dapat nasa tamang order ng values ung field na gusto mo lagyan. refer to that chatgpt code sample:

$query = "INSERT INTO registration (payee_name, email_address, username, password, user_type, img) VALUES ('$payee_name', '$email_address', '$username', '$password', '2', '$file_path')";
ok na po sir gumana na po yun display na lang po problima ko po
 
ok na po sir gumana na po yun display na lang po problima ko po
Depende kung paano ang saving. See example below:

Saving image function
PHP:
<?php
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
} else {
    if (move_uploaded_file($_FILES["image"]["tmp_name"], $targetFile)) {
        echo "The file " . htmlspecialchars(basename($_FILES["image"]["name"])) . " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}
?>

Display Uploaded Image
PHP:
<h1>Display uploaded Image:</h1>
<?php if (isset($_FILES["image"]) && $uploadOk == 1) : ?>
    <img src="<?php echo $targetFile; ?>" alt="Uploaded Image">
<?php endif; ?>
 

Similar threads

Back
Top