What's new

Bakit kaya di sya nasasave

loktoy

Eternal Poster
Joined
Oct 20, 2014
Posts
1,166
Solutions
2
Reaction
103
Points
496
Age
30
bakit kaya di nasasave yun picture 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>";

}

}
}

?>
<?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.";
}}
}


?>
 
There are a few issues in your code that might be causing the picture not to be saved in the database. Let's go through them and suggest some possible solutions:

1. Incorrect SQL syntax: In your code, you are using square brackets around the table name and column names, which is not valid in MySQL. Instead of [ICODE]registration[/ICODE], you should use backticks () or nothing at all. For example, change registration` to registration.

2. Typo in SQL query: In the section where you are trying to insert the file path into the database, you have a typo in your SQL query. Instead of $slq, it should be $sql for consistency with the other queries. Make sure you change it to $sql.

3. Incorrect variable name: In the second INSERT query where you are trying to insert the file path into the database, you are using $conn instead of $con as the variable for the database connection. Update it to $con to match the variable you defined earlier.

4. Unnecessary single quotes in SQL query: In the second INSERT query, you have an extra single quote at the end of the query. Remove it so that the query becomes: "INSERT INTO registration (img) VALUES ('$file_path')"

5. Insufficient file permissions: Make sure the photo_folder directory has the necessary write permissions for the web server to save the uploaded file. You can check the permissions by using the ls -l command on the directory. If the permissions are not set correctly, you can use the chmod command to update them.

6. Incorrect file path: Double-check the $target_dir variable to ensure that it points to the correct directory where you want to save the uploaded files. If the directory doesn't exist or the path is incorrect, the file will not be saved.

By addressing these issues, you should be able to save the picture in the database successfully.
 

Similar threads

Back
Top