-
Posts
65 -
Joined
-
Last visited
Everything posted by CloudBreaker
-
Never mind the above code...this following is what I'm trying to work with. At least I'm getting something...I just have to narrow it down by project which is just grabbing the project id. However I having short-term issues concatenating the last name after the first (there are two rows---first_name and last_name.) <?php //Open Connection $conn = mysqli_connect("localhost","root","","hsa_project_hub"); //Gather Information $sql = "SELECT * FROM user_project"; $result = mysqli_query($conn,$sql) or die (mysql_error()); ?> <form action="selectionTest.php" method="post"> <p>Final Answer by: <select name="first_name" > <option value="" >--Select--</option> <!-- Loop For Fetch Result --> <?php while($row = mysqli_fetch_array($result) ) : ?> <option value="first_name:" ><?php echo($row['first_name']);?></option> <?php endwhile; ?> <!-- End Loop for Fetch Result --> </select> </p> <input type="submit" value="Complete"/> </form>
-
The code is giving me no errors yet I get no selection. Seems to be the right approach. Starts on line 134 ends on 149. Thanks, CB <?php $conn = mysqli_connect("localhost","root","","hsa_project_hub"); session_start(); if(!$_SESSION['user_loginName']){ header("location: index.php"); } else { $project_id=$_SESSION['project_id']; if(isset($_GET['id']) && is_numeric($_GET['id'])){ $edit_id = $_GET['id']; $sql = "select rfis.no\n" . "FROM rfis\n" . "WHERE no=$edit_id"; $s = "SELECT * FROM `rfi_files` WHERE no=$edit_id "; $result=mysqli_query($conn,$sql); $row=mysqli_fetch_assoc($result); $sql = "SELECT * FROM `rfis` WHERE no=$edit_id"; $run = mysqli_query($conn,$sql); $row = mysqli_fetch_array($run); $id =$row["no"]; $rfiNo =$row["name"]; $subject =$row["subject"]; $date_submit =$row["date_submit"]; $needed_by =$row["needed_by"]; $question =$row["question"]; $suggestion =$row["gc_rsp"]; $owner_rsp =$row["owner_rsp"]; $engr_rsp =$row["engr_rsp"]; $arch_rsp =$row["arch_rsp"]; $final_rsp =$row["final_by"]; $status =$row["status"]; $sql = "SELECT * FROM `rfi_files` WHERE no=$edit_id"; $run = mysqli_query($conn,$sql); $row = mysqli_fetch_array($run); $fileName =$row["rfi_fileName"]; echo $fileName; } echo $id; ?> <!DOCTYPE HTML> <style> form label { float: left; width: 150px; margin-bottom: 5px; margin-top: 5px; padding: 5px; } .clear { display: block; clear: both; width: 100%; } #rfi_container { background: #F0F0F0; font-family: 'Josefin Sans', sans-serif; font-weight: bold; color: #8F0000; } #files { background: yellow; display:block; } </style> <html> <head> </head> <body> <h2><?php echo $project_id;?></h2> <form id="form1" name="form1" method="post" action="currentTest.php?id=<?php echo $id;?>" enctype="multipart/form-data"><br> <br class="clear" /> <label for="name">RFI NO.</label><input type="text" name="name" id="name" value="<?php echo $rfiNo;?>" /> <br class="clear" /> <label for="subject">Subject:</label><input type="text" name="subject" id="subject" value="<?php echo $subject;?>" /> <br class="clear" /> <label for="date_submit">Date submitted:</label><input type="date" name="date_submit" id="date_submit" disabled value="<?php echo $date_submit;?>" /> <br class="clear" /> <label for="needed_by">Date Needed by:</label><input type="date" name="needed_by" id="needed_by" value="<?php echo $needed_by;?>" /> <br class="clear" /> <label for="question">Question:</label><textarea name="question" id="question" cols="100" rows="8"><?php echo $question;?></textarea> <br class="clear" /> <label for="GC Uploaded File">GC Uploaded File:</label><a href="rfi_files/<?php echo $fileName;?>"><?php echo $fileName;?></a> <br class="clear" /> <label for="gc_rsp">Contractor Suggestion:</label><textarea name="gc_rsp" id="gc_rsp" cols="100" rows="8"> <?php echo $suggestion;?></textarea> <br class="clear" /> <label for="owner_rsp">Owner Response:</label><textarea name="owner_rsp" id="owner_rsp" cols="100" rows="8"><?php echo $owner_rsp;?> </textarea> <br class="clear" /> <label for="engr_rsp">Engineer Response:</label><textarea name="engr_rsp" id="engr_rsp" cols="100" rows="8"><?php echo $engr_rsp;?></textarea> <br class="clear" /> <label for="arch_rsp">Architect Response:</label><textarea name="arch_rsp" id="arch_rsp" cols="100" rows="8"><?php echo $arch_rsp;?></textarea><br> <br class="clear" /> <?php $data = "SELECT * FROM `user_project` WHERE project_id=$project_id"; $result=mysqli_query($conn,$data); //Fetch all mysqli_fetch_array($result,MYSQLI_ASSOC); // Free result set mysqli_free_result($result); mysqli_close($conn); ?> <label for="final_by">Answered by:</label><select name="answered_by" id="answered_by" > <option value=""> --Select-- </option> <?php foreach($data as $row): ?> <option><?=$row['first_name']?></option> <?php endforeach ?> </select> <br class="clear" /> <label for="date_returned">Date Returned:</label><input type="date" name="date_returned" id="date_returned" /><br> <br class="clear" /> <label for="status">Status:</label><select name="status" id="status" > <option><?php echo $status;?></option> <option value="CLOSED">CLOSED</option> <option value="at Architect">at Architect</option> <option value="at MEP Engineer">at MEP Engineer</option> <option value="at Structural Engineer">at Structural Engineer</option> <option value="at Civil Engineer">at Civil Engineer</option> <option value="at Landscape Architect">at Landscape Architect</option> <option value="at Owner">at Owner</option> <option value="at General Contractor">at General Contractor</option> </select> <br class="clear" /><br> <input type="file" name="files[]" multiple="" /> <br class="clear" /><br> <center><input type="submit" name="submit_data" id="submit_data" value="Modify RFI" /></center> </form> <?php if(isset($_POST['submit_data'])){ //getting the text information and saving in local variables $rfiNo = mysqli_real_escape_string($conn,$_POST['name']); $needed_by = mysqli_real_escape_string($conn,$_POST['needed_by']); $subject = mysqli_real_escape_string($conn,$_POST['subject']); $question = mysqli_real_escape_string($conn,$_POST['question']); $suggestion = mysqli_real_escape_string($conn,$_POST['gc_rsp']); $owner_rsp = mysqli_real_escape_string($conn,$_POST['owner_rsp']); $engr_rsp = mysqli_real_escape_string($conn,$_POST['engr_rsp']); $arch_rsp = mysqli_real_escape_string($conn,$_POST['arch_rsp']); $answered_by = mysqli_real_escape_string($conn,$_POST['answered_by']); $status = mysqli_real_escape_string($conn,$_POST['status']); $update = "update rfis set name='$rfiNo',needed_by='$needed_by',subject='$subject',question='$question',gc_rsp='$suggestion',owner_rsp='$owner_rsp', engr_rsp='$engr_rsp',arch_rsp='$arch_rsp',status='$status' WHERE no=$id"; $run_update = mysqli_query($conn, $update); if($run_update){ echo "<script>alert('RFI has been successfully modified.')</script>"; echo "<script>window.open('rfi_list.php','_self')</script>"; } } ?> </body> </html> <?php } ?>
-
Variable dependent on user input issue
CloudBreaker replied to CloudBreaker's topic in PHP Coding Help
Thanks mac_gyver. Using the A.I number in the rfis table did trick (no warnings or errors). I assigned a variable to mysqli_insert_id($conn), then used it later to associate the uploaded file into rfis_files table. The only issue now is when uploading more than one file, only the last file selected is entered into the the table, however all the files that are selected are successfully copied to the appropriate folder. Correct me if I'm wrong, but shouldn't the "foreach loop" in the code cover this? thx CB <?php // initialization $conn = mysqli_connect("localhost","root","","hsa_project_hub"); session_start(); // user state/permission check if(!$_SESSION['user_loginName']){ header("location: index.php"); exit; //this elimiates the need for the else {} statement } // post method form processing if($_SERVER['REQUEST_METHOD'] == 'POST') // an overall check if a post method form was submitted { //Enter info in database if(isset($_POST['submit_data'])){ //Getting the form information and saving in local variables $name = mysqli_real_escape_string($conn,$_POST['name']); $subject = mysqli_real_escape_string($conn,$_POST['subject']); $date_submit = mysqli_real_escape_string($conn,$_POST['date_submit']); $needed_by = mysqli_real_escape_string($conn,$_POST['needed_by']); $question = mysqli_real_escape_string($conn,$_POST['question']); $gc_rsp = mysqli_real_escape_string($conn,$_POST['gc_rsp']); $owner_rsp = mysqli_real_escape_string($conn,$_POST['owner_rsp']); $engr_rsp = mysqli_real_escape_string($conn,$_POST['engr_rsp']); $arch_rsp = mysqli_real_escape_string($conn,$_POST['arch_rsp']); $final_by = mysqli_real_escape_string($conn,$_POST['final_by']); $date_returned = mysqli_real_escape_string($conn,$_POST['date_returned']); $status = mysqli_real_escape_string($conn,$_POST['status']); // Creating local variable for query from Session variable $first_name = $_SESSION['firstName']; $project_id = $_SESSION['project_id']; $sql = " INSERT INTO rfis ( id, name, subject, issued_by, date_submit, needed_by, question, gc_rsp, owner_rsp, engr_rsp, arch_rsp, final_by, date_returned, status ) VALUES ('$project_id', '$name', '$subject', '$first_name', '$date_submit', '$needed_by', '$question', '$gc_rsp', '$owner_rsp', '$engr_rsp', '$arch_rsp', '$final_by', '$date_returned', '$status' ) "; if (mysqli_query($conn, $sql)) { $rfi_match = mysqli_insert_id($conn); } //Choose files to be uploaded if(isset($_FILES['files'])){ $errors= array(); foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){ $file_name = $key.$_FILES['files']['name'][$key]; $file_size =$_FILES['files']['size'][$key]; $file_tmp =$_FILES['files']['tmp_name'][$key]; $file_type=$_FILES['files']['type'][$key]; if($file_size > 2097152){ $errors[]='File size must be less than 2 MB'; } $query="INSERT into rfi_files(`no`,`rfi_project_id`,`rfi_fileName`,`rfi_fileSize`,`rfi_fileType`) VALUES('$rfi_match','$project_id','$file_name','$file_size','$file_type'); "; $desired_dir="rfi_files"; if(empty($errors)==true){ if(is_dir($desired_dir)==false){ mkdir("$desired_dir", 0700); // Create directory if it does not exist } if(is_dir("$desired_dir/".$file_name)==false){ move_uploaded_file($file_tmp,"$desired_dir/".$file_name); }else{ // rename the file if another one exist $new_dir="$desired_dir/".$file_name.time(); rename($file_tmp,$new_dir) ; } mysqli_query($conn,$query); }else{ print_r($errors); } } if(empty($error)){ echo "<script>alert('RFI added successfully')</script>"; echo "<script>window.open('rfi_list.php','_self')</script>"; } } } } ?> -
Variable dependent on user input issue
CloudBreaker replied to CloudBreaker's topic in PHP Coding Help
Thanks for taking time to help me out mac_gyver. I haven't been doing this long, so I get confused when new problems arise. I'm combing over all your comments. Making the RFI number incremental presents a problem because there is more than one project, which means there will be more than one RFI No.001, ann RFI No.002...etc. -
Variable dependent on user input issue
CloudBreaker replied to CloudBreaker's topic in PHP Coding Help
Ex: user enters 003 in the form (line 90 and it's local variable is set on line 154.) , fills out the rest of the form and selects a file to be uploaded. He then hits submit. The query is run and and 003 should be in the "name" column at this point..right? Now we're at line 195 where that 003 needs to be placed into a different table where file name, size, etc are posted to in order to associate the uploaded file to this form that the user is filling out. A SELECT query is run and 003 should be available to be pulled from the database. The problem is that the 003 is apparently not yet in the database, so I get an undefined variable at line 204. If I type in 003 manually in the place of $name on line 208 everything works great....but I can't do that, because that's user input. I could just create create separate page before this form and ask the user to enter this "RFI" number, then carry it over to this form page, but that just doesn't seem like the right thing to do. I'm fried over this. thnks CB -
The entire form below is submitted all at once. This creates a problem because I need id="name" (line 92) inserted into the database before the code at line 198 is initiated, because it's dependent upon the user input. How would I go about having that "name" value entered into the database before the code at 198 is executed while at the same time having all the code on the same page? thanks for you help. CB <?php $conn = mysqli_connect("localhost","root","","hsa_project_hub"); session_start(); if(!$_SESSION['user_loginName']){ header("location: index.php"); } else { ?> <link href='http://fonts.googleapis.com/css?family=Josefin+Sans' rel='stylesheet' type='text/css'> <style> form label { float: left; width: 150px; margin-bottom: 5px; margin-top: 5px; padding: 5px; } .clear { display: block; clear: both; width: 100%; } #rfi_container { background: #F0F0F0; font-family: 'Josefin Sans', sans-serif; font-weight: bold; color: #8F0000; } #files { background: yellow; display:block; } </style> <?php //Grab Session Variable $project_id = $_SESSION['project_id']; ?> <!DOCTYPE HTML> <html> <head> <title>New RFI</title> <link href="hsastyle.css" rel="stylesheet"> </head> <body> <div id="main_container"> <p><em>version 1.0 beta</em></p> <div id="banner"> <div id="logo"> <img src="images/hsa-logo.jpg" alt=HSA logo> </div> <div id="logout"><H5><a href="logout.php">Log Out</a></H5></div> <div id="welcome"><h6>Welcome <?php echo $_SESSION['firstName'];?></h6></div> <div id="project_name"> <strong><em><?php echo $_SESSION['projName']?></em></strong> </div> </div> <!--End Banner--> <div id="user_list"> <FORM> <INPUT Type="BUTTON" Value="Back to RFI List" Onclick="window.location.href='rfi_list.php'"> </FORM> </div> <div id="rfi_container"> <!--Create RFI for user input--> <form id="form1" name="form1" method="post" action="new_rfi.php" enctype="multipart/form-data"><br> <br class="clear" /> <label for="name">RFI NO.</label><input type="text" name="name" id="name" placeholder="Ex: 003" required="required" /> <br class="clear" /> <label for="subject">Subject</label><input type="text" name="subject" id="subject" placeholder="Ex: Discontinued floor finish" /> <br class="clear" /> <label for="date_submit">Date submitted:</label><input type="date" name="date_submit" id="date_submit" /> <br class="clear" /> <label for="needed_by">Date Needed by:</label><input type="date" name="needed_by" id="needed_by" /> <br class="clear" /> <label for="question">Question:</label><textarea name="question" id="question" required="required" cols="100" rows="5"></textarea> <br class="clear" /> <label for="gc_rsp">Contractor Suggestion:</label><textarea name="gc_rsp" id="gc_rsp" cols="100" rows="5"></textarea> <br class="clear" /> <label for="owner_rsp">Owner Response:</label><textarea name="owner_rsp" id="owner_rsp" cols="100" rows="5"></textarea> <br class="clear" /> <label for="engr_rsp">Engineer Response:</label><textarea name="engr_rsp" id="engr_rsp" cols="100" rows="5"></textarea> <br class="clear" /> <label for="arch_rsp">Architect Response:</label><textarea name="arch_rsp" id="arch_rsp" cols="100" rows="5"></textarea><br> <br class="clear" /> <label for="final_by">Final Response by:</label><select name="final_by" id="final_by"> <option></option> <option value="Architect">Architect</option> <option value="Owner">Owner</option> <option value="Structural Engineer">Structural Engineer</option> <option value="MEP Engineer">MEP Engineer</option> <option value="Civil Engineer">Civil Engineer</option> <option value="Landscape Architect">Landscape Architect</option> </select> <br class="clear" /> <label for="date_returned">Date Returned:</label><input type="date" name="date_returned" id="date_returned" /><br> <br class="clear" /> <label for="status">Status:</label><select name="status" id="status"> <option></option> <option value="CLOSED">CLOSED</option> <option value="at Architect">at Architect</option> <option value="at MEP Engineer">at MEP Engineer</option> <option value="at Structural Engineer">at Structural Engineer</option> <option value="at Civil Engineer">at Civil Engineer</option> <option value="at Landscape Architect">at Landscape Architect</option> <option value="at Owner">at Owner</option> <option value="at General Contractor">at General Contractor</option> </select> <br class="clear" /><br> <input type="file" name="files[]" multiple="" /> <br class="clear" /><br> <center><input type="submit" name="submit_data" id="submit_data" value="Submit RFI" /></center> </form> <br> <?php //Enter info in database if(isset($_POST['submit_data'])){ //Getting the form information and saving in local variables $name = mysqli_real_escape_string($conn,$_POST['name']); $subject = mysqli_real_escape_string($conn,$_POST['subject']); $date_submit = mysqli_real_escape_string($conn,$_POST['date_submit']); $needed_by = mysqli_real_escape_string($conn,$_POST['needed_by']); $question = mysqli_real_escape_string($conn,$_POST['question']); $gc_rsp = mysqli_real_escape_string($conn,$_POST['gc_rsp']); $owner_rsp = mysqli_real_escape_string($conn,$_POST['owner_rsp']); $engr_rsp = mysqli_real_escape_string($conn,$_POST['engr_rsp']); $arch_rsp = mysqli_real_escape_string($conn,$_POST['arch_rsp']); $final_by = mysqli_real_escape_string($conn,$_POST['final_by']); $date_returned = mysqli_real_escape_string($conn,$_POST['date_returned']); $status = mysqli_real_escape_string($conn,$_POST['status']); // Creating local variable for query from Session variable $first_name = $_SESSION['firstName']; $sql = " INSERT INTO rfis ( id, name, subject, issued_by, date_submit, needed_by, question, gc_rsp, owner_rsp, engr_rsp, arch_rsp, final_by, date_returned, status ) VALUES ( '$project_id', '$name', '$subject', '$first_name', '$date_submit', '$needed_by', '$question', '$gc_rsp', '$owner_rsp', '$engr_rsp', '$arch_rsp', '$final_by', '$date_returned', '$status' ) "; if (mysqli_query($conn, $sql)) { echo "Success with first query"; //echo "<script>alert('RFI added successfully')</script>"; //echo "<script>window.open('rfi_list.php','_self')</script>"; //} else { //echo "Error: " . $sql . "<br>" . mysqli_error($conn); } } ?> <?php //Fetch new RFI No. from database $s = "SELECT rfis.id,rfis.name\n" . "FROM rfis\n" . "where rfis.id=$project_id\n" . "and rfis.name=$name"; $result=mysqli_query($conn,$s); $row=mysqli_fetch_assoc($result); $i=0; $run = mysqli_query($conn, $s); //assign values to local variables while($row=mysqli_fetch_array($run)){ $rfiNo =$row["name"]; $i++; } ?> <?php //Choose files to be uploaded if(isset($_FILES['files'])){ $errors= array(); foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){ $file_name = $key.$_FILES['files']['name'][$key]; $file_size =$_FILES['files']['size'][$key]; $file_tmp =$_FILES['files']['tmp_name'][$key]; $file_type=$_FILES['files']['type'][$key]; if($file_size > 2097152){ $errors[]='File size must be less than 2 MB'; } $query="INSERT into rfi_files(`rfi_id`,`rfi_project_id`,`rfi_fileName`,`rfi_fileSize`,`rfi_fileType`) VALUES('$rfiNo','$project_id','$file_name','$file_size','$file_type'); "; $desired_dir="rfi_files"; if(empty($errors)==true){ if(is_dir($desired_dir)==false){ mkdir("$desired_dir", 0700); // Create directory if it does not exist } if(is_dir("$desired_dir/".$file_name)==false){ move_uploaded_file($file_tmp,"$desired_dir/".$file_name); }else{ // rename the file if another one exist $new_dir="$desired_dir/".$file_name.time(); rename($file_tmp,$new_dir) ; } mysqli_query($conn,$query); }else{ print_r($errors); } } if(empty($error)){ echo "<script>alert('RFI $rfiNo added successfully')</script>"; echo "<script>window.open('rfi_list.php','_self')</script>"; } } ?> <!--<div id="files"><a href="rfi_files<?php// echo $rfiFilename;?>"><?php //echo $rfiFilename;?></a></div> USE LATER TO SHOW LINK FOR DOWNLOAD--> <?php } ?> <div><!--end of container--> </body> </html>
-
Just needed a good night sleep.
-
I realized when posting a new file to the database and it's it's folder that I needed to have a corresponding name ($name) which is entered into the form by the user. After the initial form is run, I set up a query that should grab the $name variable listed on line 154 now that it exists. That query starts on line 194. After that query is run, I assigned a local variable to the appropriate row so I can utilize it with the script that uploads the chosen files to a different table created just for uploaded files. I need this value inserted with the file so I can associate it with the RFI that the user will have created once he or she submits the form. When I submit form, the information that the user entered is successfully placed into the proper tables. The problem starts at line one 198 with two warnings. Any ideas? Thanks CB Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in B:\Programs\wamp\www\hsa\new_rfi.php on line 198 Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in B:\Programs\wamp\www\hsa\new_rfi.php on line 205 Notice: Undefined variable: rfiNo in B:\Programs\wamp\www\hsa\new_rfi.php on line 215 <?php $conn = mysqli_connect("localhost","root","","hsa_project_hub"); session_start(); if(!$_SESSION['user_loginName']){ header("location: index.php"); } else { ?> <link href='http://fonts.googleapis.com/css?family=Josefin+Sans' rel='stylesheet' type='text/css'> <style> form label { float: left; width: 150px; margin-bottom: 5px; margin-top: 5px; padding: 5px; } .clear { display: block; clear: both; width: 100%; } #rfi_container { background: #F0F0F0; font-family: 'Josefin Sans', sans-serif; font-weight: bold; color: #8F0000; } #files { background: yellow; display:block; } </style> <?php //Grab Session Variable $project_id = $_SESSION['project_id']; ?> <!DOCTYPE HTML> <html> <head> <title>New RFI</title> <link href="hsastyle.css" rel="stylesheet"> </head> <body> <div id="main_container"> <p><em>version 1.0 beta</em></p> <div id="banner"> <div id="logo"> <img src="images/hsa-logo.jpg" alt=HSA logo> </div> <div id="logout"><H5><a href="logout.php">Log Out</a></H5></div> <div id="welcome"><h6>Welcome <?php echo $_SESSION['firstName'];?></h6></div> <div id="project_name"> <strong><em><?php echo $_SESSION['projName']?></em></strong> </div> </div> <!--End Banner--> <div id="user_list"> <FORM> <INPUT Type="BUTTON" Value="Back to RFI List" Onclick="window.location.href='rfi_list.php'"> </FORM> </div> <div id="rfi_container"> <!--Create RFI for user input--> <form id="form1" name="form1" method="post" action="new_rfi.php" enctype="multipart/form-data"><br> <br class="clear" /> <label for="name">RFI NO.</label><input type="text" name="name" id="name" placeholder="Ex: 003" required="required" /> <br class="clear" /> <label for="subject">Subject</label><input type="text" name="subject" id="subject" placeholder="Ex: Discontinued floor finish" /> <br class="clear" /> <label for="date_submit">Date submitted:</label><input type="date" name="date_submit" id="date_submit" /> <br class="clear" /> <label for="needed_by">Date Needed by:</label><input type="date" name="needed_by" id="needed_by" /> <br class="clear" /> <label for="question">Question:</label><textarea name="question" id="question" required="required" cols="100" rows="5"></textarea> <br class="clear" /> <label for="gc_rsp">Contractor Suggestion:</label><textarea name="gc_rsp" id="gc_rsp" cols="100" rows="5"></textarea> <br class="clear" /> <label for="owner_rsp">Owner Response:</label><textarea name="owner_rsp" id="owner_rsp" cols="100" rows="5"></textarea> <br class="clear" /> <label for="engr_rsp">Engineer Response:</label><textarea name="engr_rsp" id="engr_rsp" cols="100" rows="5"></textarea> <br class="clear" /> <label for="arch_rsp">Architect Response:</label><textarea name="arch_rsp" id="arch_rsp" cols="100" rows="5"></textarea><br> <br class="clear" /> <label for="final_by">Final Response by:</label><select name="final_by" id="final_by"> <option></option> <option value="Architect">Architect</option> <option value="Owner">Owner</option> <option value="Structural Engineer">Structural Engineer</option> <option value="MEP Engineer">MEP Engineer</option> <option value="Civil Engineer">Civil Engineer</option> <option value="Landscape Architect">Landscape Architect</option> </select> <br class="clear" /> <label for="date_returned">Date Returned:</label><input type="date" name="date_returned" id="date_returned" /><br> <br class="clear" /> <label for="status">Status:</label><select name="status" id="status"> <option></option> <option value="CLOSED">CLOSED</option> <option value="at Architect">at Architect</option> <option value="at MEP Engineer">at MEP Engineer</option> <option value="at Structural Engineer">at Structural Engineer</option> <option value="at Civil Engineer">at Civil Engineer</option> <option value="at Landscape Architect">at Landscape Architect</option> <option value="at Owner">at Owner</option> <option value="at General Contractor">at General Contractor</option> </select> <br class="clear" /><br> <input type="file" name="files[]" multiple="" /> <br class="clear" /><br> <center><input type="submit" name="submit_data" id="submit_data" value="Submit RFI" /></center> </form> <br> <?php //Enter info in database if(isset($_POST['submit_data'])){ //Getting the form information and saving in local variables $name = mysqli_real_escape_string($conn,$_POST['name']); $subject = mysqli_real_escape_string($conn,$_POST['subject']); $date_submit = mysqli_real_escape_string($conn,$_POST['date_submit']); $needed_by = mysqli_real_escape_string($conn,$_POST['needed_by']); $question = mysqli_real_escape_string($conn,$_POST['question']); $gc_rsp = mysqli_real_escape_string($conn,$_POST['gc_rsp']); $owner_rsp = mysqli_real_escape_string($conn,$_POST['owner_rsp']); $engr_rsp = mysqli_real_escape_string($conn,$_POST['engr_rsp']); $arch_rsp = mysqli_real_escape_string($conn,$_POST['arch_rsp']); $final_by = mysqli_real_escape_string($conn,$_POST['final_by']); $date_returned = mysqli_real_escape_string($conn,$_POST['date_returned']); $status = mysqli_real_escape_string($conn,$_POST['status']); // Creating local variable for query from Session variable $first_name = $_SESSION['firstName']; $sql = " INSERT INTO rfis ( id, name, subject, issued_by, date_submit, needed_by, question, gc_rsp, owner_rsp, engr_rsp, arch_rsp, final_by, date_returned, status ) VALUES ( '$project_id', '$name', '$subject', '$first_name', '$date_submit', '$needed_by', '$question', '$gc_rsp', '$owner_rsp', '$engr_rsp', '$arch_rsp', '$final_by', '$date_returned', '$status' ) "; if (mysqli_query($conn, $sql)) { echo "Success with first query"; //echo "<script>alert('RFI added successfully')</script>"; //echo "<script>window.open('rfi_list.php','_self')</script>"; //} else { //echo "Error: " . $sql . "<br>" . mysqli_error($conn); } } ?> <?php $sql = "select rfis.id,rfis.name\n" . "from rfis\n" . "where rfis.$project_id"; $result=mysqli_query($conn,$sql); $row=mysqli_fetch_assoc($result); $i=0; $run = mysqli_query($conn, $sql); //assign values to local variables while($row=mysqli_fetch_array($run)){ $rfiNo =$row["name"]; $i++; } ?> <?php echo $project_id; //test echo echo $rfiNo; //test echo //Choose files to be uploaded if(isset($_FILES['submit_data'])){ $errors= array(); foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){ $rfi_fileName = $key.$_FILES['files']['name'][$key]; $rfi_fileSize =$_FILES['files']['size'][$key]; $file_tmp =$_FILES['files']['tmp_name'][$key]; $rfi_fileType =$_FILES['files']['type'][$key]; if($rfi_fileSize > 8097152){ $errors[]='File size must be less than 8 MB'; } $query="INSERT into rfi_files (`rfi_id`,`rfi_project_id`,`rfi_fileName`,`rfi_fileSize`,`rfi_fileType`) VALUES('$rfiNo','$project_id','$rfi_fileName','$rfi_fileSize','$rfi_fileType'); "; $desired_dir="rfi_files"; if(empty($errors)==true){ if(is_dir($desired_dir)==false){ mkdir("$desired_dir", 0700); // Create directory if it does not exist } if(is_dir("$desired_dir/".$rfi_fileName)==false){ move_uploaded_file($file_tmp,"rfi_files/".$rfi_fileName); }else{ //rename the file if another one exists $new_dir="rfi_files/".$rfi_fileName.time(); rename($file_tmp,$new_dir) ; } mysqli_query($conn,$query); }else{ print_r($errors); } } if(empty($error)){ echo "Success"; } } ?> <!--<div id="files"><a href="rfi_files<?php// echo $rfiFilename;?>"><?php //echo $rfiFilename;?></a></div> USE LATER TO SHOW LINK FOR DOWNLOAD--> <?php } ?> <div><!--end of container--> </body> </html>
-
Maybe it's just late, but I can't see the undefined variables. I'm trying to list the files that have just been uploaded. <?php //if(isset($_row['upload_file'])){ $s = "SELECT rfi_files.id,rfi_files.rfi_id,rfi_files.rfi_fileName\n" . "FROM rfi_files\n" . "where rfi_files.rfi_id='$project_id'" . "AND rfi_files.rfi_fileName='$rfiFilename'" . "\n" . ""; $result=mysqli_query($conn,$s); $row=mysqli_fetch_assoc($result); $i=0; $run = mysqli_query($conn, $s); while($row=mysqli_fetch_array($run)){ $rfiFileid =$row["rfi_id"]; $rfiFilename =$row["rfi_fileName"]; $i++; } ?> <div><strong><u>Uploaded Files:<u></strong></div><br> <div id="files"><a href="rfi_files/<?php echo $rfiFilename;?>"><?php echo $rfiFilename;?></a></div> <?php } ?> <div><!--end of container--> </body> ( ! ) Notice: Undefined variable: rfiFilename in B:\Programs\wamp\www\hsa\new_rfi.php on line 246 Call Stack # Time Memory Function Location 1 0.0018 278168 {main}( ) ..\new_rfi.php:0 Uploaded Files: ( ! ) Notice: Undefined variable: rfiFilename in B:\Programs\wamp\www\hsa\new_rfi.php on line 268 Call Stack #TimeMemoryFunctionLocation 10.0018278168{main}( )..\new_rfi.php:0 "> ( ! ) Notice: Undefined variable: rfiFilename in B:\Programs\wamp\www\hsa\new_rfi.php on line 268 Call Stack # Time Memory Function Location 1 0.0018 278168 {main}( ) ..\new_rfi.php:0
-
Just trying to echo an uploaded file
CloudBreaker replied to CloudBreaker's topic in PHP Coding Help
Thanks iarp...you took me in the right direction. this is what worked for me. My string variable is different because I changed it. It would not have made a difference. <a href="rfi_files/<?php echo $rfiFilename;?>"><?php echo $rfiFilename;?></a> -
I have an RFI form that a contractor will fill out and if he or she chooses to, can upload a pdf for reference. The form works fine as well as the code for uploading the file which successfully puts the file information (file name, size and type) into the database as well as copying the file into a specified directory. All I'm trying to accomplish now is to have the file or files that have been uploaded (relating to the specific RFI) listed or echoed at the bottom of the form. Also, how would you make them downloadable by clicking on them once they are listed? Towards the end of the code I always go blind or lose my sense of logic, so the confusion starts at line 155. thanks CB <!DOCTYPE html> <html> <head> <title>RFI Insert Test</title> <link href="form_styles.css" rel="stylesheet"> </head> <body> <form id="form1" name="form1" method="post" action="rfi_form.php"> <label for="name">RFI NO.</label><input type="text" name="name" id="name" placeholder="Ex: 003" required="required" /> <br class="clear" /> <label for="subject">Subject</label><input type="text" name="subject" id="subject" placeholder="Ex: Discontinued floor finish" /> <br class="clear" /> <label for="date_submit">Date submitted:</label><input type="date" name="date_submit" id="date_submit" /> <br class="clear" /> <label for="needed_by">Date Needed by:</label><input type="date" name="needed_by" id="needed_by" /> <br class="clear" /> <label for="question">Question:</label><textarea name="question" id="question" required="required" cols="100" rows="5"></textarea> <br class="clear" /> <label for="gc_rsp">Contractor Suggestion:</label><textarea name="gc_rsp" id="gc_rsp" cols="100" rows="5"></textarea> <br class="clear" /> <label for="owner_rsp">Owner Response:</label><textarea name="owner_rsp" id="owner_rsp" cols="100" rows="5"></textarea> <br class="clear" /> <label for="engr_rsp">Engineer Response:</label><textarea name="engr_rsp" id="engr_rsp" cols="100" rows="5"></textarea> <br class="clear" /> <label for="arch_rsp">Architect Response:</label><textarea name="arch_rsp" id="arch_rsp" cols="100" rows="5"></textarea> <br class="clear" /> <label for="final_by">Final Response by:</label><select name="final_by" id="final_by"> <option></option> <option value="Architect">Architect</option> <option value="Owner">Owner</option> <option value="Structural Engineer">Structural Engineer</option> <option value="MEP Engineer">MEP Engineer</option> <option value="Civil Engineer">Civil Engineer</option> <option value="Landscape Architect">Landscape Architect</option> </select> <br class="clear" /> <label for="date_returned">Date Returned:</label><input type="date" name="date_returned" id="date_returned" /> <br class="clear" /> <label for="status">Status:</label><select name="status" id="status"> <option></option> <option value="CLOSED">CLOSED</option> <option value="at Architect">at Architect</option> <option value="at MEP Engineer">at MEP Engineer</option> <option value="at Structural Engineer">at Structural Engineer</option> <option value="at Civil Engineer">at Civil Engineer</option> <option value="at Landscape Architect">at Landscape Architect</option> <option value="at Owner">at Owner</option> <option value="at General Contractor">at General Contractor</option> </select> <br class="clear" /><br> <input type="submit" name="submit_data" id="submit_data" value="Submit" /> </form> <?php $servername = "localhost"; $username = "root"; $password = ""; $dbname = "hsa_project_hub"; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } //Enter info in database if(isset($_POST['submit_data'])){ //Getting the form information and saving in local variables $name = mysqli_real_escape_string($conn,$_POST['name']); $subject = mysqli_real_escape_string($conn,$_POST['subject']); $date_submit = mysqli_real_escape_string($conn,$_POST['date_submit']); $needed_by = mysqli_real_escape_string($conn,$_POST['needed_by']); $question = mysqli_real_escape_string($conn,$_POST['question']); $gc_rsp = mysqli_real_escape_string($conn,$_POST['gc_rsp']); $owner_rsp = mysqli_real_escape_string($conn,$_POST['owner_rsp']); $engr_rsp = mysqli_real_escape_string($conn,$_POST['engr_rsp']); $arch_rsp = mysqli_real_escape_string($conn,$_POST['arch_rsp']); $final_by = mysqli_real_escape_string($conn,$_POST['final_by']); $date_returned = mysqli_real_escape_string($conn,$_POST['date_returned']); $status = mysqli_real_escape_string($conn,$_POST['status']); $sql = " INSERT INTO rfis ( name, subject, date_submit, needed_by, question, gc_rsp, owner_rsp, engr_rsp, arch_rsp, final_by, date_returned, status ) VALUES ( '$name', '$subject', '$date_submit', '$needed_by', '$question', '$gc_rsp', '$owner_rsp', '$engr_rsp', '$arch_rsp', '$final_by', '$date_returned', '$status' ) "; if (mysqli_query($conn, $sql)) { echo "New record created successfully"; } else { echo "Error: " . $sql . "<br>" . mysqli_error($conn); } mysqli_close($conn); } //Choose files to be uploaded if(isset($_FILES['files'])){ $errors= array(); foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){ $rfi_fileName = $key.$_FILES['files']['name'][$key]; $rfi_fileSize =$_FILES['files']['size'][$key]; $file_tmp =$_FILES['files']['tmp_name'][$key]; $rfi_fileType =$_FILES['files']['type'][$key]; if($rfi_fileSize > 8097152){ $errors[]='File size must be less than 8 MB'; } $query="INSERT into rfi_files (`rfi_fileName`,`rfi_fileSize`,`rfi_fileType`) VALUES('$rfi_fileName','$rfi_fileSize','$rfi_fileType'); "; $desired_dir="rfi_files"; if(empty($errors)==true){ if(is_dir($desired_dir)==false){ mkdir("$desired_dir", 0700); // Create directory if it does not exist } if(is_dir("$desired_dir/".$rfi_fileName)==false){ move_uploaded_file($file_tmp,"rfi_files/".$rfi_fileName); }else{ //rename the file if another one exists $new_dir="rfi_files/".$rfi_fileName.time(); rename($file_tmp,$new_dir) ; } mysqli_query($conn,$query); }else{ print_r($errors); } } if(empty($error)){ echo "Success"; } } ?> <br> <form action="" method="POST" enctype="multipart/form-data"> <input type="file" name="files[]" multiple="" /> <input type="submit"/> </form> <?php $s = "Select rfi_files.rfi_filename, rfi_files.rfi_fileType\n" . "FROM rfi_files\n" . "WHERE rfi_files.id=22"; $result=mysqli_query($conn,$s) or die('Error, query failed'); $row=mysqli_fetch_assoc($result); $i=0; //execute query $run = mysqli_query($conn, $s); //assign file to variable while($row=mysqli_fetch_array($run)){ $rfi_fileName =$row["rfi_fileName"]; $i++; echo $rfi_fileName; } ?> </body> </html>
-
Just one undefined index in file upload array
CloudBreaker replied to CloudBreaker's topic in PHP Coding Help
F-Me...why didn't I see that. I need to go to bed. Thanks requinix. CB -
All files are uploading fine...however, on line 8 for $error_array I get that "undefined index" notice. Any ideas on how to get rid of it? Thanks, CB <?php if(Isset($_FILES['file_array'])) { $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']['error']; for($i = 0; $i < count($tmp_name_array); $i++){ if(move_uploaded_file($tmp_name_array[$i], "test_uploads/".$name_array[$i])){ echo $name_array[$i]." upload is complete<br>"; } else { echo "move_uploaded_file function failed for ".$name_array[$i]."<br>"; } } } ?>