Can't access filename with $_FILES method, PHP [duplicate] - php

This question already has answers here:
Why would $_FILES be empty when uploading files to PHP?
(22 answers)
Closed 8 years ago.
I am trying to build a file uploader, with a youtube tutorial that doesn't cover files, and right now I am stuck on the name. Using $_Files returns nothing and I am unsure as to why. :/
I tried to echo it out, but nothing comes back.
Everything else seems to work though.
<html>
<head>
</head>
<body>
<?php
error_reporting(-1);
ini_set('display_errors', 'On');
$con = mysql_connect("localhost","root","root");
if (!$con){
die("Can not connect: " . mysql_error());
}
mysql_select_db("example",$con);
if(isset($_POST['update'])){
$UpdateQuery = "UPDATE repo SET location='$_POST[location]', name='$_POST[name]', description='$_POST[description]' WHERE location='$_POST[hidden]'";
mysql_query($UpdateQuery, $con);
};
if(isset($_POST['delete'])){
$DeleteQuery = "DELETE FROM repo WHERE location='$_POST[hidden]'";
mysql_query($DeleteQuery, $con);
};
if(isset($_POST['add'])){
$AddQuery = "INSERT INTO repo (name, id, image, location, partners, description, date) VALUES ('$image_name', '','$_POST[uimage]', '$_POST[ulocation]', '$_POST[upartners]', '$_POST[udescription]', NOW())";
mysql_query($AddQuery, $con);
};
$sql = "SELECT * FROM repo";
$myData = mysql_query($sql,$con);
echo "<table border=1>
<tr>
<th>Image</th>
<th>Name</th>
<th>Location</th>
<th>Partners</th>
<th>Description</th>
<th>Date</th>
</tr>";
while($record = mysql_fetch_array($myData)){
?>
<form action="mydata5.php"
method="post" enctype="multipart/form-data">
<?php
echo "<tr>";
echo "<td>" . "<img src=Assets/Images/" . $record['name'] . " </td>";
echo "<td>" . "<input type=text name=topic value=" . $record['name'] . " </td>";
echo "<td>" . "<input type=text name=name value=" . $record['location'] . " </td>";
echo "<td>" . "<input type=text name=name value=" . $record['partners'] . " </td>";
echo "<td>" . "<input type=text name=description value=" . $record['description'] . " </td>";
echo "<td>" . "<input type=text name=description value=" . $record['date'] . " </td>";
echo "<td>" . "<input type=hidden name=hidden value=" . $record['location'] . " </td>";
echo "<td>" . "<input type=submit name=update value=update" . " </td>";
echo "<td>" . "<input type=submit name=delete value=delete" . " </td>";
echo "</tr>";
echo "</form>";
}
echo "<form action=mydata5.php method=post>";
echo "<tr>";
// echo "<td><input type=file name=uimage></td>";
?>
<td><input type="file" name="uimage" id="uimage"></td>
<?php
$file = $_FILES['uimage']['tmp_name'];
$image_name = mysql_real_escape_string($_FILES['uimage']['name']);
echo $_FILES['uimage']['error'];
echo "<td><input type=hidden name=uname></td>";
echo "<td><input type=text name=ulocation></td>";
echo "<td><input type=text name=upartners></td>";
echo "<td><input type=text name=udescription></td>";
echo "<td>" . "<input type=submit name=add value=add" . " </td>";
echo "</form>";
echo "</table>";
mysql_close($con);
?>
</body>
</html>

You need to add enctype="multipart/form-data" in the form tag.

For file type fields you need to add enctype attribute in your form so that uploaded files can be access using $_FILES
Update form starting tag with below
<form action="mydata5.php"
method="post" enctype="multipart/form-data">

Check your Insert Query.You are using $_POST for image name. That is wrong.

Related

My php code is not Updating or Adding to database

Basically I am not getting any errors when I press update or add. Can anyone help me out? I want it so when I press add it adds that data to the database, and when I press update it updates the database with that value.
<html>
<head>
<title>Subcontractors Data</title>
</head>
<body>
Logout
Homepage
<?php
//make connection
$con = mysqli_connect("localhost","root","");
if(!$con){
die("Can not connect " . mysqli_error());
}
//select db
mysqli_select_db($con , 'subcontractor');
$sql="SELECT * FROM subcontractors";
if(isset($_POST['update'])){
$UpdateQuery = "UPDATE subcontractors SET ID='$_POST[ID]', Name='$_POST[Name]', Surname='$_POST[Surname]', FPA='$_POST[FPA]', Performance='$_POST[Performance]' WHERE ID='$_POST[hidden]'";
mysqli_query($con, $UpdateQuery);
};
if(isset($_POST['add'])){
$AddQuery = "INSERT INTO subcontractors (ID, Name, Surname, FPA, Performance) VALUES ('$_POST[aID]','$_POST[aName]','$_POST[aSurname]','$_POST[aFPA]','$_POST[aPerformance]')";
mysqli_query($con, $AddQuery);
};
$my_Data=mysqli_query($con,$sql);
echo "<table border=1>";
echo"<tr>";
echo"<th>ID</th>";
echo"<th>Name</th>";
echo"<th>Surname</th>";
echo"<th>FPA</th>";
echo "<th>Performance</th>";
echo "</tr>";
while($record=mysqli_fetch_assoc($my_Data)){
echo "<form action=editsub.php method=post>";
echo "<tr>";
echo "<td>" . "<input type=text name='ID' value=".$record['ID'] ." </td>";
echo "<td>" . "<input type=text name='Name' value=".$record['Name'] . " </td>";
echo "<td>" . "<input type=text name='Surname' value=".$record['Surname'] . " </td>";
echo "<td>" . "<input type=text name='FPA' value=".$record['FPA'] . "% </td>";
echo "<td>" . "<input type=text name='Performance' value=".$record['Performance'] . "% </td>";
echo "<input type=hidden name='hidden' value=" . $record['ID'] . ">";
echo "<input type=submit name='update' value='update'>";
echo "</tr>";
echo "</form>";
}
echo "<form action=editsub.php mehtod=post>";
echo "<tr>";
echo "<td><input type=text name='aID'></td>";
echo "<td><input type=text name='aName'></td>";
echo "<td><input type=text name='aSurname'></td>";
echo "<td><input type=text name='aFPA'></td>";
echo "<td><input type=text name='aPerformance'></td>";
echo "<td>" . "<input type=submit name='add' value='add'" . " </td>";
echo "</form>";
echo "</table>";
mysqli_close($con);
?>
</body>
</html>
This way you can check error.
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

How to update a hyperlink?

I decided to make my database with an active hyperlink so when I find a record I click on an email address and and email is opened. BUT I have a problem with update, When I want to make a correction to the entry and when I press update the email disappears and I get:
Notice: Undefined index: Mail in /Applications/XAMPP/xamppfiles/htdocs/robocze/mydata_dodaj_test_1.php on line 36
I can add records but I can't update...
Can You please help me, thank You
the code:
<?php
session_start();
if(!isset($_SESSION["sess_user"])){
header("location:login.php");
} else {
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Baza Klientów</title>
</head>
<body>
<h3>Welcome, <?=$_SESSION['sess_user'];?>! Logout </h3>
<input type="button" onclick="location.href='mydata_dodaj_test_1.php';" value="Powrót do wyszukiwania" />
<input type="button" onclick="location.href='index.php';" value="MENU powrót" />
<h2><b><center>Wyszukiwanie Klientów ITalents</center></b></h2>
<?php
}
?>
<?php
$con = mysql_connect("","","","");
if (!$con){
die("Błąd połączenia: " . mysql_error());
}
mysql_select_db("baza",$con);
if(isset($_POST['update'])) {
$UpdateQuery = "UPDATE Klienci SET id='$_POST[id]', Firma='$_POST[Firma]', Mail='$_POST[Mail]', Data='$_POST[Data]', Konsultant='$_POST[Konsultant]' WHERE id='$_POST[hidden]'";
mysql_query($UpdateQuery,$con);
};
if(isset($_POST['add'])) {
$AddQuery = "INSERT INTO Klienci (id, Firma, Mail, Data, Konsultant) VALUES ('$_POST[uid]','$_POST[uFirma]','$_POST[uMail]','$_POST[uData]','$_POST[uKonsultant]')"; // this is the 36 line
mysql_query($AddQuery,$con);
};
if(isset($_POST['search']))
{
$valueToSearch = $_POST['valueToSearch'];
$query = "SELECT * FROM Klienci WHERE CONCAT(Firma, Mail, Konsultant) LIKE '%".$valueToSearch."%'";
$search_result = filterTable($query);
}
else {
$query = "SELECT * FROM Klienci ORDER BY id ASC";
$search_result = filterTable($query);
}
function filterTable($query)
{
$con = mysql_connect("","","","");
if (!$con){
die("Błąd połączenia: " . mysql_error());
}
mysql_select_db("baza",$con);
$filter_Result = mysql_query($query, $con);
return $filter_Result;
};
echo "<form action=mydata_dodaj_test_1.php method=post>";
echo "<input type=text name=valueToSearch placeholder=wpisz>";
echo "<input type=submit name=search value=Szukaj>";
echo "<table align=center style=text-align:center border=5>
<tr>
<th>ID</th>
<th>Firma</th>
<th>Mail</th>
<th>Data</th>
<th>Konsultant</th>
</tr>";
while($row = mysql_fetch_array($search_result)) {
echo "<form action=mydata_dodaj_test_1.php method=post>";
echo "<tr>";
echo "<td>" . "<input type=int name=id value=" . $row['id'] . " </td>";
echo "<td>" . "<input type=varchar name=Firma value=" . $row['Firma'] . " </td>";
echo "<td>" . "<a href='mailto:{$row['Mail']}'>" . $row['Mail'] . " </td>";
echo "<td>" . "<input type=date name=Data value=" . $row['Data'] . " </td>";
echo "<td>" . "<input type=varchar name=Konsultant value=" . $row['Konsultant'] . " </td>";
echo "<td>" . "<input type=hidden name=hidden value=" . $row['id'] . " </td>";
echo "<td>" . "<input type=submit name=update value=update" . " </td>";
echo "</form>";
}
echo "<form action=mydata_dodaj_test_1.php method=post>";
echo "<tr>";
echo "<td><input type=text name=uid></td>";
echo "<td><input type=varchar name=uFirma></td>";
echo "<td><input type=text name=uMail></td>";
echo "<td><input type=text name=uData></td>";
echo "<td><input type=text name=uKonsultant></td>";
echo "<td>" . "<input type=submit name=add value=dodaj" . " </td>";
echo "</form>";
echo "</table>";
?>
<input type="button" onclick="location.href='mydata_dodaj_test_1.php';" value="Powrót do wyszukiwania" />
<input type="button" onclick="location.href='index.php';" value="MENU powrót" />
</body>
</html>
As error stated undefined index in your update statement
$UpdateQuery = "UPDATE Klienci SET id='$_POST[id]', Firma='$_POST[Firma]', Mail='$_POST[Mail]', Data='$_POST[Data]', Konsultant='$_POST[Konsultant]' WHERE id='$_POST[hidden]'";
you are accessing $_POST[Mail] which is no where in your form.

Whole text won't show in a table, PHP

I'm trying to is have have table to display my database with an update, delete, and add functions.
My problem is when I view it. It doesn't show the full text in the "Description" column and also in "Developer". Also in the "SecondDirectory" column it shows </td instead of just blank if its blank.
<html>
<head>
</head>
<body>
<?php
$con = mysql_connect("$$$$","$$$$","$$$$");
if (!$con){
die("Can not connect: " . mysql_error());
}
mysql_select_db("mydb",$con);
if(isset($_POST['update'])){
$UpdateQuery = "UPDATE table SE ID='$_POST[topic]',Filename='$_POST[filename]', Description='$_POST[description]', TopDirectory='$_POST[topdirectory]', SecondDirectory='$_POST[seconddirectory]', Developer='$_POST[developer]', Date='$_POST[date]' WHERE ID='$_POST[hidden]'";
mysql_query($UpdateQuery, $con);
};
if(isset($_POST['delete'])){
$DeleteQuery = "DELETE FROM table WHERE ID='$_POST[hidden]'";
mysql_query($DeleteQuery, $con);
};
if(isset($_POST['add'])){
$AddQuery = "INSERT INTO table (ID,Filename,Description,TopDirectory,SecondDirectory,Developer,Date) VALUES ('$_POST[uid]','$_POST[ufilename]','$_POST[udescription]','$_POST[utopdirectory]','$_POST[useconddirectory]','$_POST[udeveloper]','$_POST[udate]')";
mysql_query($AddQuery, $con);
};
$sql = "SELECT * FROM table";
$myData = mysql_query($sql,$con);
echo "<table border=1>
<tr>
<th>ID</th>
<th>Filename</th>
<th>Description</th>
<th>TopDirectory</th>
<th>SecondDirectory</th>
<th>Developer</th>
<th>Date</th>
</tr>";
while($record = mysql_fetch_array($myData)){
echo "<form action=theworks.php method=post>";
echo "<tr>";
echo "<td>" . "<input type=text name=id value=" . $record['ID'] . " </td>";
echo "<td>" . "<input type=text name=filename value=" . $record['Filename'] . " </td>";
echo "<td>" . "<input type=text name=description value=" . $record['Description'] . " </td>";
echo "<td>" . "<input type=text name=topdirectory value=" . $record['TopDirectory'] . " </td>";
echo "<td>" . "<input type=text name=seconddirectory value=" . $record['SecondDirectory'] . " </td>";
echo "<td>" . "<input type=text name=developer value=" . $record['Developer'] . " </td>";
echo "<td>" . "<input type=text name=date value=" . $record['Date'] . " </td>";
echo "<td>" . "<input type=hidden name=hidden value=" . $record['ID'] . " </td>";
echo "<td>" . "<input type=submit name=update value=update" . " </td>";
echo "<td>" . "<input type=submit name=delete value=delete" . " </td>";
echo "</tr>";
echo "</form>";
}
echo "<form action=database.php method=post>";
echo "<tr>";
echo "<td><input type=text name=uid></td>";
echo "<td><input type=text name=ufilename></td>";
echo "<td><input type=text name=udescription><td>";
echo "<td><input type=text name=utopdirectory></td>";
echo "<td><input type=text name=useconddirectory></td>";
echo "<td><input type=text name=udeveloper><td>";
echo "<td><input type=text name=udate></td>";
echo "<td>" . "<input type=submit name=add value=add" . " </td>";
echo "</tr>";
echo "</form>";
echo "</table>";
mysql_close($con);
?>
</body>
</html>
So I figured it out.
echo "<td>" . "<input type=text name=description value=" . $record['Description'] . " </td>";
What I needed to put is a single quotation right after value= and two single quotations in " " so that it also counts the spaces. The code should have been...
echo "<td>" . "<input type=text name=description value='" . $record['Description'] . "'' </td>";

MySQL - UPDATE and DELETE QUERY not working

I'm just a beginner in terms of programming, so I'm just referring all my codes through tutorials. Luckily, I found this online tutorial in youtube where users are allowed to add, update, and delete data in mysql using php. I follow all his instructions, I got it working but then it stopped when I added css on it.
This is not a general issue, I just need some help. If anyone can help me, much appreciated. Thank you so much.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "rssfeed";
$connect = mysql_connect($servername, $username, $password, $dbname);
if (!$connect) {
die("Connection failed. Error" . mysql_error());
}
$database = mysql_select_db($dbname, $connect);
if (!$database) {
die("Can't select database");
}
$sql = "SELECT * FROM record";
$data = mysql_query($sql, $connect);
if (isset($_POST['update'])){
$updateQuery = "UPDATE record SET name = '$_POST[name]', url = '$_POST[url]', description = '$_POST[desc]' WHERE name = '$_POST[hidden]'";
mysql_query($updateQuery, $connect);
header("Location: maintenance.php");
};
if (isset($_POST['delete'])){
$deleteQuery = "DELETE FROM record WHERE name = '$_POST[hidden]'";
mysql_query($deleteQuery, $connect);
header("Location: maintenance.php");
};
if (isset($_POST['add'])){
$addQuery = "INSERT INTO record (name, url, description) VALUES ('$_POST[iName]', '$_POST[iUrl]', '$_POST[iDesc]')";
mysql_query($addQuery, $connect);
header("Location: maintenance.php");
};
echo "<div class=center>
<table id=myTable border=1>
<tr>
<th> Name </th>
<th> URL </th>
<th> Description </th>
</tr>";
while($record = mysql_fetch_array($data)) {
echo "<form method=post action=maintenance.php>";
echo "<tr>";
echo "<td>" . "<input type=text name=name id=name value=" . $record['name'] . " </td>";
echo "<td>" . "<input type=text name=url id=url value=" . $record['url'] . " </td>";
echo "<td>" . "<textarea rows=1 cols=50 wrap=physical name=desc id=desc>" . strip_tags($record['description']) . "</textarea></td>";
echo "<input type=hidden name=hidden value=" . $record['name'] . ">";
echo "<td>" . "<input type=submit name=update id=update value=update" . " </td>";
echo "<td>" . "<input type=submit name=delete id=delete value=delete" . " </td>";
echo "</tr>";
echo "</form>";
}
echo "</table>";
echo "<table border=1>";
echo "<form method=post action=maintenance.php>";
echo "<tr>";
echo "<td><input type=text name=iName></td>";
echo "<td><input type=text id=url name=iUrl></input></td>";
echo "<td><textarea rows=1 cols=50 name=iDesc></textarea></td>";
echo "<td>" . "<input type=submit name=add id=add value=add" . " </td>";
echo "</tr>";
echo "</form>";
echo "</table>";
echo "</div>";
mysql_close($connect);
?>
In your queries, $_POST[name] should be $_POST[\"name\"]. BUT, this is terrible, you are very open to SQL injections.
Please have a read of this and stop using mysql_query (it's deprecated)
you have several errors in your echo output..
echo "<td>" . "<input type=text name=name id=name value=" . $record['name'] . " </td>";
You have forgot the single quotes for each html-element-attribute and the bigger then at the end of multiple input elements...
try this:
echo "<td><input type='text' name='name' id='name' value='" . $record['name'] . "'> </td>";

Update is not working in database

Dont mind the security issues, this is just local testing, but when ever i click the update button none of the changes go through on the page or on the query and i get no erros.
<?php
$link = mysqli_connect("localhost", "root", "", "test") or die("could not connect");
if (isset($_POST['update'])) {
$updateQuery = (" UPDATE `test1` SET f_name = '$_POST[f_name]', l_name='$_POST[l_name]', email='$_POST[email]' WHERE id='$_POST[id]'");
mysqli_query($link, $updateQuery);
};
$query = ("SELECT * FROM `test1`");
$result = mysqli_query($link, $query);
echo "<table border=1
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<form method=post action=update.php>";
echo "<tr>";
echo "<td>" . "<input type=text name=f_name value=" . $row['f_name'] . " </td>";
echo "<td>" . "<input type=text name=l_name value=" . $row['l_name'] . " </td>";
echo "<td>" . "<input type=text name=email value=" . $row['email'] . " </td>";
echo "<td>" . "<input type=hidden name=id value=" . $row['id'] . " </td>";
echo "<td>" . "<input type=submit name=submit value=update" . " </td>";
echo "</tr>";
}
?>
change your form to
while($row = mysqli_fetch_array($result)) {
echo "<form method=post action=update.php>";
echo "<input type=hidden name=update>";
echo "<tr>";
echo "<td>" . "<input type=text name=f_name value=" . $row['f_name'] . " </td>";
echo "<td>" . "<input type=text name=l_name value=" . $row['l_name'] . " </td>";
echo "<td>" . "<input type=text name=email value=" . $row['email'] . " </td>";
echo "<td>" . "<input type=hidden name=id value=" . $row['id'] . " </td>";
echo "<td>" . "<input type=submit name=submit value=update" . " </td>";
echo "</tr>";
}
POST keys should be in quotes. Try this instead:
$updateQuery = "UPDATE test1 SET f_name = ".$_POST['f_name'].", l_name=."$_POST['l_name'].", email=".$_POST['email']." WHERE id=".$_POST['id'];
Try this:
$updateQuery = ("UPDATE `test1` SET f_name = '{$_POST['f_name']}', l_name='{$_POST['l_name']}', email='{$_POST['email']}' WHERE id='{$_POST['id']}'");
Also you can try echoing something inside your if (isset($_POST['update'])) { to make sure it is testing true.
Here is your problem:
if (isset($_POST['submit']) && $_POST['submit'] == 'update') {
The name of the submit button is submit not update, the value is update.

Categories