PHP code does not start when the page is loaded - php

I made the code below to show the files that were inserted by the user, however it only shows the files after having attached a file, however I wanted it to show when loading the page, for the user to see what files he has already uploaded. Here is the code I used to upload the file:
<center><p class="text-success" style="font-size: 20px; padding-bottom: 100px;">
<?php
session_start();
require_once('conecta.php');
$email = $_SESSION['email'];
if (isset($_POST['enviar'])) {
$arq = $_FILES['arquivo']['name'];
$dataup = date('Y-m-d H:i:s');
echo $dataup;
$arq = str_replace(" ", "_", $arq);
$arq = str_replace("ç", "c", $arq);
if (file_exists("uploads/".$arq)) {
$a = 1;
while (file_exists("uploads/[".$a."]".$arq)) {
$a++;
}
$arq = "[".$a."]".$arq;
}
if (move_uploaded_file($_FILES['arquivo']['tmp_name'], 'uploads/'.$arq)) {
$objDb = new db();
$link = $objDb->conecta_mysql();
$sql = "insert into arquivos (email_vol, nomearq) values ('". $email."', '".$arq."')";
if (mysqli_query($link, $sql)){
echo 'Plano de aula 1 enviado com sucesso!';
} else {
echo (mysqli_error($link));
echo 'Erro ao enviar o plano de aula!';
}
} else {
echo "Nenhum arquivo selecionado!";
}
}
?>
</p></center>
Here is the code to show the file on the page:
<?php
session_start();
require_once('conecta.php');
$pasta = "uploads/";
$consulta = mysqli_query($link, "SELECT * FROM arquivos WHERE email_vol = '$email'");
while ($resultado = mysqli_fetch_array($consulta)) {
echo "" . $resultado["nomearq"] . "<br />";
}
?>
Can someone give me a hand? Thank you very much in advance.

Related

wait a few seconds before going further with the function php

so i have this deletefile function and i want it to go back to tyhe home page after 3 seconds automaticly
public function deleteFile($id)
{
try {
$query = "SELECT naam, bestand_path FROM bestanden ";
$query .= "WHERE id='$id'";
$result = $this->datahandler->readsData($query);
$results = $result->fetchAll();
foreach ($results as $row) {
$filename = $row['naam'];
}
$file = getcwd() . "/uploads/" . $filename;
//echo "Absolute Path To Directory is: ";
//echo $delfile;
***if (unlink($file)) {
echo $filename . ' was deleted successfully!';
header('Location: ' . SERVER_URL . '/Home/');***
} else {
echo 'There was a error deleting ' . $filename;
}
$query = "DELETE FROM bestanden ";
$query .= "WHERE id=$id";
$result = $this->datahandler->deleteData($query);
} catch (PDOException $e) {
echo "Fout opgetreden";
}
}
its about the one with the stars around it.
thank you

updating my database values through php

Hi I am trying to do a Registration that the users will put their name password and their answers to some questions and then an admin will manually answer to it if it's accepted.I did the system that loads their name password and answers in the database,and I also ran the things that will show the answers to the admin,but I can't figure a way to change a value just for one user not for all of them,I will leave you my codes and everything over here.
Here is my admin.viewapplications.php code
(Here,it shows everything fine,but I can't figure a way that the button to act just for one id not for all)
<?php
//include(__DIR__ . "/signup.php");
include("../resources/config.php");
//$name = $_POST['Name'];
//$mg = $_POST['MG'];
//$pg = $_POST['PG'];
//$rk = $_POST['RK'];
$sql = "SELECT id, name, tutorial, MG, PG, RK FROM rp_users WHERE tutorial = 2";
//$tutorial = "SELECT tutorial FROM rp_users";
$result = mysql_query($sql);
//$result2 = mysql_query($tutorial);
//$value = mysql_fetch_object($result2)
/*if($result)
{
echo "Succes";
}
else
{
die(mysql_error());
}*/
//if($value > 1)
//
while($row = mysql_fetch_array($result))
{
//$tutorial = row["tutorial"];
//f($tutorial == 2)
//}
$id = $row["id"];
$name = $row["name"];
$mg = $row["MG"];
$pg = $row["PG"];
$rk = $row["RK"];
echo "ID: " . $id."<br> <br>";
echo "Nume: " . $name."<br> <br>";
echo "MG: " . $mg."<br> <br>";
echo "PG: " . $pg."<br> <br>";
echo "RK: " . $rk."<br> <br>";
echo '<form action="./?p=applicationaccept" method="POST">';
echo '<input type="submit" name="accept" value="Accepta">';
echo '</form><br>';
echo '<form action="./?p=applicationdeny" method="POST">';
echo '<input type="submit" name="deny" value="Respinge">';
echo '</form><br> <br> <br>';
}
//}
//
?>
And here is my applicationaccept.php
<?php
include("../admin/admin.viewapplications.php");
include("../resources/config.php");
$iduser = $id;
$sql = "UPDATE rp_users SET tutorial=0";
$result = mysql_query($sql);
if($result)
{
echo "Succes";
}
else
{
die(mysql_error());
}
/*while($row = mysql_fetch_array($result))
{
}*/
?>
I think what you want to do is a simple UPDATE to your MySQL database..
but make sure you format the PHP code you're using otherwise it'll give you an ERROR!
Also you have to use 'mysqli' now in PHP!
<?php
$someID = '1';
$sql = "UPDATE `rp_users` SET `tutorial`= '0' WHERE `id` = $someID";
$result = mysqli_query($link, $sql);
if($result)
{
echo "Success";
}
else
{
echo ("Error");
}
?>
BTW I forgot to mntion the '$link' is the connection to your database!
As of my understanding of your question if your form action is applicationaccept.php and you are trying to update for one user in applicationaccept.php file, try this:
<?php
include("../admin/admin.viewapplications.php");
include("../resources/config.php");
$iduser = $_POST["id"]; // pass id as parameter in form
$sql = "UPDATE rp_users SET tutorial=0";// change this line to following line
$sql = "UPDATE rp_users SET tutorial=0 where id=$iduser";
$result = mysql_query($sql);
if($result)
{
echo "Succes";
}
else
{
die(mysql_error());
}
?>
Be aware your code is vulnerable

PHP header redirect not working on form submit

I have a form which updates information inside of a DB, everything works perfectly as it should....... except for some reason the header does not work for me. I am using the header redirect in a lot of other scripts within the same site; it is just this one script that the header redirect does not work. I know that the if statement the header is inside of is called because the echo right below the header is displayed on the page, its just kind of ignoring the header line. If you have any suggestions that would be very helpful.
The code below has been minimized so its not to long for everyone to read but if it is still a problem ill clean it up even more
the form
<form class="editUser" action="uploadEmployee.php" method="post" enctype="multipart/form-data" />
<input name="editUserFirstName" id="editUserFirstName" type="text" placeholder="Enter The User's First Name" />
<label>Employee's Coverage</label>
<input type="file" name="file_array[]" placeholder="Add The Employees Coverage" />
<input name="addUserSubmit" type="submit" value="Submit" />
</form>
The uploading scripts
<?php
include("includes/connect.inc.php");
if(isset($_FILES['file_array'])){
$day = date('d');
$month = date('m');
$dateObj = DateTime::createFromFormat('!m', $month);
$monthName = $dateObj->format('F'); // March
$year = date('Y');
$date = $monthName . ", " . $day . ", " . $year;
$name_array = $_FILES['file_array']['name'];
$tmp_name_array = $_FILES['file_array']['tmp_name'];
$type_array = $_FILES['file_array']['type'];
$size_array = $_FILES['file_array']['size'];
$error_array = $_FILES['file_array']['error'];
$payCheque = $name_array[0];
$T4 = $name_array[1];
$coverage = $name_array[2];
$selectedId = $_POST['editId'];
$name = $_POST['editUserFirstName'];
if($_POST['editUserPermission']){
$permission = "1";
}else{
$permission = "0";
}
for($i = 0; $i < count($tmp_name_array); $i++){
if(move_uploaded_file($tmp_name_array[$i], "uploads/".$name_array[$i])){
$title = "This Is A Test Title";
$icon = "0";
if($i == 0){
$icon = "1";
$title = "You Have Received A New Pay Cheque";
$comment = "Hello " . $name . ", click view document to view and download your pay cheque for " . $date . ". Your pay cheque will be a PDF file. Thank you.";
}
$sql = "INSERT INTO securedFiles (title, date, PDF, comment, idOfUser, icon)
VALUES ('$title', '$date', '$name_array[$i]', '$comment', '$documentId', '$icon')";
if ($connect->query($sql) === TRUE) {
//header("Location: hub.php");
} else {
echo "Error: " . $sql . "<br>" . $connect->error;
}
echo $payCheque ." upload is complete<br>";
} else {
echo "move_uploaded_file function failed for ".$name_array[$i]."<br>";
}
}
$sql = "UPDATE dealerEmployees SET firstName = '$_POST[editUserFirstName]', lastName = '$_POST[editUserLastName]', password = '$_POST[editUserPW]', permission = '$permission', address = '$_POST[editUserAddress]', email = '$_POST[editUserEmail]', phone = '$_POST[editUserPhone]' WHERE id = $selectedId";
if ($connect->query($sql) === TRUE) {
header('Location: hub.php');
echo "Success";
} else {
echo "Error: " . $sql . "<br>" . $connect->error;
}
}
//$connect->close();
?>
header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.
And in your case, you are using echo before header()
So you can use a redirect method(that i use in all my projects, no problems ever)
<?php
if ($connect->query($sql) === TRUE) {
echo "<script> parent.self.location = \"hub.php\";</script>";
echo "Success";
} else {
echo "Error: " . $sql . "<br>" . $connect->error;
}
?>

How do I delete files from dir plus db

So this is how I tried an put them together
But deleting the file in dir needs a value in the name of checkbox and deleting the row in db doesn't...
if(isset($_POST['delete']) && isset($_POST['checkbox'])){
$checkbox = $_POST['checkbox'];
for($i=0;$i<count($checkbox);$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM media WHERE id='$del_id'";
$result = mysqli_query($connection, $sql);
}
foreach($_POST['checkbox'] as $key => $value){
$name = $key;
list($dir, $file_name_type) = explode("/",$name); // get actual file name
$actual_name = $file_name_type;
list($actual_name,$type) = explode(".",$file_name_type);
if(unlink($key) && $result){
$_SESSION["message"]="Datei <b style=\"color:#005577\"> ".$actual_name."</b> wurde erfolgreich gelöscht :)";
header ("Location: media.php");
} else {
$_SESSION["message"]="Datei <b style=\"color:#005577\"> ".$actual_name."</b> konnte nicht gelöscht werden.";}
header ("Location: media.php");
}
}
$result = mysqli_query($connection, "SELECT * FROM media ORDER BY date DESC");
confirm_query($result);
while ($row = mysqli_fetch_array($result)){
$post_date = new DateTime($row["date"]);
echo "
<tr>
<td><input name=\"checkbox[]\" type=\"checkbox\" value=" .$row['id']. ">
<td><a class=\"image\" href=" .$row['path']. " rel=\"lightbox\"><img src=" .$row["path"]. " height=\"50\"></a></td>
<td class=\"title\">" .$row['file_name']. "</td>
<td class=\"date\">".strftime("%e. %b, '%g", $post_date->getTimestamp())."</td>
</tr>";
}
?>
</table>
</form>
Cheers Chris
Managed to resolve my problem thanx
foreach($_POST['checkbox'] as $key => $value){
$sql = "DELETE FROM media WHERE path='$key'";
$result = mysqli_query($connection, $sql);
$name = $key;
list($dir, $file_name_type) = explode("/",$name); // get actual file name
$actual_name = $file_name_type;
list($actual_name,$type) = explode(".",$file_name_type);
if(unlink($key) && $result){
$_SESSION["message"]="Datei <b style=\"color:#005577\"> ".$actual_name."</b> wurde erfolgreich gelöscht :)";
header ("Location: media.php");
} else {
$_SESSION["message"]="Datei <b style=\"color:#005577\"> ".$actual_name."</b> konnte nicht gelöscht werden.";}
header ("Location: media.php");
}
}
Seems to work nicely like this thanx
Cheers Chris

Syntax error but the code looks fine... I guess?

I get a syntax error on line 30 where the else clause is.
<!DOCTYPE html>
<?php
session_start();
require "users.php";
require "connect.php";
if(!isset($_SESSION['logged_in']) || $_SESSION['logged_in'] == false) {
header('Location: index.php');
exit();
}
if (!empty($_GET)){
$link = $_GET;
while(list($sleutel, $waarde) = each($link)){
$key = $sleutel;
$md5 = $waarde;
}
$query = mysql_query('SELECT * FROM Virtualbox WHERE md5naam ="'.$md5.'"')or die(mysql_error());
$row = mysql_fetch_array($query);
if($md5 == $row['md5naam']){
$resultaat = '<tr>
<td>'.$row["virtualboxnaam"].'</td>
<td>'.$row["operating_system"].'</td>
<td>'.$row["omschrijving"].'</td>
</tr>'
} else { **//this is line 30**
$resultaat = "";
}
}
?>
In my HTML, which is on the same page, I have an anchor that is generated from a PHP query:
<div id="tab2" class="tab2">
<div class="accordion">
<?php
$result = mysql_query("SELECT * FROM Virtualbox WHERE user_id = ". $_SESSION["user_id"]);
$teller = 1;
while($select = mysql_fetch_array($result)){
echo('<div class="accordion-section">');
echo('<a class="accordion-section-title" href="#accordion-'.$teller.'">' . $teller . '. ' .$select["titel"] .'</a>');
echo('<div id="accordion-'.$teller.'" class="accordion-section-content">');
echo('<h2>Titel</h2>
<p>'.$select["titel"].'</p><br>
<h2>Operating System</h2>
<p>'.$select["operating_system"].'</p><br>
<h2>Virtualbox</h2>
<p>'.$select["virtualboxnaam"].'</p><br>
'.$select["md5naam"].'');
echo('</div>');
echo('</div>');
$teller++;
}
?>
</div><!--end .accordion-->
Please don't judge me too hard on this, I'm just learning PHP a month ago.
You're missing a ; before the else.
if($md5 == $row['md5naam']){
$resultaat = '<tr>
<td>'.$row["virtualboxnaam"].'</td>
<td>'.$row["operating_system"].'</td>
<td>'.$row["omschrijving"].'</td>
</tr>'; // added ;
} else { **//this is line 30**
$resultaat = "";
}

Categories