this is the whole code where in I got a problem

<?php
//get the value of the selected radio value from webpage1.html
$n=$_POST['company'];
$dbhost="localhost";
$dbuser="root";
$dbpass="";
$dbname;
?>
<html><head><title>web page1</title></head><body>
<?php
// logical selection for $n
if($n==1){
echo"You selected Company <font color=red>bhagis portal</font> <br>";
$dbname="bhagis_portal";
}
elseif($n==2){
echo"You selected Company <font color=red>bli portal</font> <br>";
$dbname="bliportal";
}
elseif($n==3){
echo"You selected Company <font color=red>m portal</font> <br>";
$dbname="m_portal";
}
else{
echo"You Selected Company <font color=red>vg portal</font> <br>";
$dbname="vg_portal";
}
$conn=mysql_connect($dbhost,$dbuser,$dbpass)or die("Error connecting to MySQL");
mysql_select_db($dbname)or die(mysql_error());
$query="Select * from img_table";
$row=mysql_query($query)or die(mysql_error());
while($result=mysql_fetch_array($row)){
echo $result[0].") ";
echo "<img src=/".$dbname."/images/".$result[1]."><br>";
}
?>
<form action="index_web_page2.php" method="post" enctype="multipart/form-data">
<input name="hiddenField" type="hidden" id="hiddenField" value="999999">
<input type="file" id="timesheet" name="timesheet"><br>
<input type="submit" name="cb_read" id="cb_read" value="Upload Timekeeping File">
<input type=reset  name=reset value=clear></form>

<?php
$pic=$_file["timesheet"]["tmp_name"];
$destination='\xampp\htdocs\new'.'\\'.$dbname.'\images'.'\\'.$_files["timesheet"]["tmp_name"];
move_uploaded_file($pic,$destination);

/* key code */
$query="insert into img_table(img_name) values('".$pic."')";


?>
</body>
</html>

this code has the ability to select 5 database at first... I tried to have an output of images at first that will reveal it's output at the browser... by default it was not uploaded by the user but already in a folder ( only path by image tag inside the while loop statement ). My problem now is, I dunno about how to do this 2 things:

1.) if the user will upload something, it will be automatically transfered to the $destination

2.) the last upload of the user will be shown too like what is done in the while loop statement

I will truly appreciate your explanations and help to this forum

Dani AI

Generated

A short, practical plan to fix the upload flow and make the latest upload visible (builds on 's suggestion to move upload logic into the form handler and addresses the bugs visible in 's code).

The main problems in the posted snippet are: wrong superglobal names, using the temporary filename as both destination and database value, mixing Windows backslashes with web paths, missing upload/error checks, and reliance on deprecated mysql_ functions. The correct approach is to accept the form submission in a separate handler, validate the incoming file (size, MIME/extension, $_FILES error code), create a safe unique filename, move the uploaded file into the public images directory, and store that final filename in the database (not the temporary name). Use prepared statements (mysqli or PDO) instead of old mysql_ calls.

Practical workflow (no code here so it adds to the thread): 1) Keep the form on the main page and POST to a handler script named in the form action. 2) In the handler, check $_FILES['timesheet']['error'] and validate file type/size. 3) Generate a sanitized filename and ensure the target images directory exists and is writable. 4) Call move_uploaded_file to move from the tmp location to the images folder. 5) Insert the saved filename into the DB with a prepared statement. 6) Redirect back to the main page; the existing SELECT/while display loop will then show the new image (select ordering by id or timestamp to show the latest).

Troubleshooting notes and references: confirm enctype="multipart/form-data", check php.ini settings (file_uploads, upload_max_filesize, post_max_size), verify move_uploaded_file returns true and folder permissions allow writes. See the PHP manual on file uploads and move_uploaded_file for details: File upload basics and move_uploaded_file.

Recommended Answers

All 3 Replies

you must create another php file called index_web_page2.php

<?php
 -- here proper connection parameter are required
$pic=$_file["timesheet"]["tmp_name"];$destination='\xampp\htdocs\new'.'\\'.$dbname.'\images'.'\\'.$_files["timesheet"]["tmp_name"];move_uploaded_file($pic,$destination); /* key code */
$query="insert into img_table(img_name) values('".$pic."')";  


header("location:main_file_name.php");

?>

remove this code from your main file.

you must create another php file called index_web_page2.php

<?php
 -- here proper connection parameter are required
$pic=$_file["timesheet"]["tmp_name"];$destination='\xampp\htdocs\new'.'\\'.$dbname.'\images'.'\\'.$_files["timesheet"]["tmp_name"];move_uploaded_file($pic,$destination); /* key code */
$query="insert into img_table(img_name) values('".$pic."')";  


header("location:main_file_name.php");

?>

remove this code from your main file.

you mean I need to create a third page??? like webpage.php??
and remove this from webpage2.php??

you need two pages
1 one your main page which code you have posted above(only remove the upload logic that i mentioned from main page)
2 another page will contain upload logic i posted name that page as index_web_page2.php (because you mentioned that name in action attribute of form element)

No third page required

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.