herghost Posted September 27, 2009 Share Posted September 27, 2009 Hi all, hope this is easy to answer! I have a form which includes a user file upload. This is the code that is executed once the form has been submitted: if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0) { $fileName = $_SESSION['username'] ; $tmpName = $_FILES['userfile']['tmp_name']; $fileSize = $_FILES['userfile']['size']; $fileType = $_FILES['userfile']['type']; $fp = fopen($tmpName, 'r'); $content = fread($fp, filesize($tmpName)); $content = addslashes($content); $query = "INSERT INTO upload (name, size, type, content, post_no) VALUES ('$fileName', '$fileSize', '$fileType', '$content', '$post_no')"; mysql_query($query) or die("oh dear " .mysql_error()); $query = "UPDATE users_credits SET credits = credits - 1 WHERE username = '$username'"; mysql_query($query) or die('Error, insert query failed'); header('Location: ../solutions.php'); } ?> Is there a simple way to make this field necessary? Thanks Link to comment https://forums.phpfreaks.com/topic/175702-form-help/ Share on other sites More sharing options...
mrMarcus Posted September 27, 2009 Share Posted September 27, 2009 aren't you doing that already with isset($_POST['upload'] ? Link to comment https://forums.phpfreaks.com/topic/175702-form-help/#findComment-925883 Share on other sites More sharing options...
herghost Posted September 27, 2009 Author Share Posted September 27, 2009 The form still submits as there is more to the form, it just stops when it gets to this part. Complete form: <?php session_start(); include('../include/dbconnect.php'); //part one $query = 'SELECT user_id, username FROM users_credits WHERE username = "' . mysql_real_escape_string($_SESSION['username'], $conn) . '"'; $result = mysql_query($query, $conn) or die(mysql_error($conn)); $row = mysql_fetch_array($result); extract($row); mysql_free_result($result); $cat=$_POST['cat']; $os=$_POST['os']; $ram=$_POST['ram']; $graphics=$_POST['graphics']; $harddrive=$_POST['harddrive']; $detail=$_POST['detail']; $title=$_POST['title']; $datetime=date("d/m/y h:i:s"); $keywords=$_POST['keywords']; $query="INSERT INTO forum_question (id, user_id, title, cat, os, ram, graphics, harddrive, detail, username, datetime, is_solved, keywords) VALUES('', '$user_id', '$title', '$cat', '$os', '$ram', '$graphics', '$harddrive', '$detail', '$username', '$datetime', '', '$keywords')"; mysql_query($query) or die('Error, insert query failed'); $post_no = mysql_insert_id(); //end part one //part two if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0) { $fileName = $_SESSION['username'] ; $tmpName = $_FILES['userfile']['tmp_name']; $fileSize = $_FILES['userfile']['size']; $fileType = $_FILES['userfile']['type']; $fp = fopen($tmpName, 'r'); $content = fread($fp, filesize($tmpName)); $content = addslashes($content); $query = "INSERT INTO upload (name, size, type, content, post_no) VALUES ('$fileName', '$fileSize', '$fileType', '$content', '$post_no')"; mysql_query($query) or die("oh dear " .mysql_error()); $query = "UPDATE users_credits SET credits = credits - 1 WHERE username = '$username'"; mysql_query($query) or die('Error, insert query failed'); header('Location: ../solutions.php'); } ?> Link to comment https://forums.phpfreaks.com/topic/175702-form-help/#findComment-925888 Share on other sites More sharing options...
mrMarcus Posted September 27, 2009 Share Posted September 27, 2009 so, you want to make the 'file upload' field required? and to not have the script execute unless there is a file to be uploaded? Link to comment https://forums.phpfreaks.com/topic/175702-form-help/#findComment-925891 Share on other sites More sharing options...
herghost Posted September 27, 2009 Author Share Posted September 27, 2009 Yeah, Is it possible? Link to comment https://forums.phpfreaks.com/topic/175702-form-help/#findComment-925893 Share on other sites More sharing options...
mrMarcus Posted September 27, 2009 Share Posted September 27, 2009 absolutely. can you post your form, please? Link to comment https://forums.phpfreaks.com/topic/175702-form-help/#findComment-925894 Share on other sites More sharing options...
herghost Posted September 27, 2009 Author Share Posted September 27, 2009 Sure, Thanks for this: <?php session_start(); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta name="Description" content="" /> <meta name="Keywords" content="" /> <meta name="Robots" content="index,follow" /> <link rel="stylesheet" href="images/CoolWater.css" type="text/css" /> <title>Project</title> <script type="text/javascript" src="js/prototype.js"></script> <script type="text/javascript" src="js/scriptaculous.js?¬ load=builder,effects"></script> <script type="text/javascript" src="js/modalbox.js"></script> </head> <body> <div id="wrap"> <?php // filter incoming values $username = (isset($_POST['username'])) ? trim($_POST['username']) : ''; $password = (isset($_POST['password'])) ? $_POST['password'] : ''; $first_name = (isset($_POST['first_name'])) ? trim($_POST['first_name']) : ''; $last_name = (isset($_POST['last_name'])) ? trim($_POST['last_name']) : ''; $email = (isset($_POST['email'])) ? trim($_POST['email']) : ''; $city = (isset($_POST['city'])) ? trim($_POST['city']) : ''; $state = (isset($_POST['state'])) ? trim($_POST['state']) : ''; $postcode = (isset($_POST['postcode'])) ? trim($_POST['postcode']) : ''; $userlevel = (isset($_POST['userlevel'])) ? trim($_POST['userlevel']) : ''; include('include/header.php'); include ('include/dbconnect.php');?> <!-- content-wrap starts here --> <div id="content-wrap"> <div id="main"> <!--If user is not logged in--> <?php if (!isset($_SESSION['logged'])) { ?> <center><img src="images/oops.jpg" alt="oops" /><br /> <p><h3>You need to login before you can start posting</h3></p> </div> <?php include('include/sidebar_0.php'); include('include/footer.php'); echo "</body></html>"; } else { $query = 'SELECT userlevel, credits FROM users_info u JOIN users_credits i ON u.user_id = i.user_id WHERE username = "' . mysql_real_escape_string($_SESSION['username'], $conn) . '"'; $result = mysql_query($query, $conn) or die(mysql_error($conn)); $row = mysql_fetch_array($result); extract($row); mysql_free_result($result); } if ($credits > 0) { ?> <h1>Welcome to the Question Forum.</h1> <p> <h3>Post New Topic</h3></p> <form enctype="multipart/form-data" action="forum/add_post.php" method="POST" name="postquestion"> <p> <label for = "Catergory">Category</label> <label for="Hardware"> <input type="radio" name="cat" value="Hardware" id="Hardware" /> Hardware</label> <label for="Software"> <input type="radio" name="cat" value="Software" id="Software" /> Software</label> <label for="Peripheral"> <input type="radio" name="cat" value="Peripheral" id="Peripheral" /> Peripheral</label> <br /> <label for="os">Operating System</label> <input name="os" type="text" id="os" /> <label for="ram">MB of RAM</label> <input name="ram" type="text" id="ram" /> <label for="harddrive">Type & Size of Harddrive</label> <input name="harddrive" type="text" id="harddrive" /> <label for="graphics">Graphics Card</label> <input name="graphics" type="text" id="graphics" /> <label for="title">Post Title</label> <input name="title" type="text" id="title" /> <label for="detail">Your Problem Details</label> <textarea rows="20" cols="5" name="detail" id="detail"></textarea> <label for="keywords">Keywords (Seperate by , )</label> <textarea rows="20" cols="3" name="keywords" id="keywords"></textarea> <input type="hidden" name="MAX_FILE_SIZE" value="2000000"> <label for="userfile">Please upload your DXDIAG as a txt file (.txt) <a href="include/dxdiag.php" title="DXDIAG Infomation" onclick=" Modalbox.show(this.href, {title: this.title, width: 600}); return false; "> Whats This?</a> </label> <input name="userfile" id="userfile" type="file" /><br /> <input name="upload" type="submit" class="button" id="upload" value=" Upload "> </p> </form> </div> <?php } ?> <?php if ($credits == 0) { ?> <center> <img src="images/oops.jpg" width="305" height="179" alt="oops" /> <?php echo "<p>"; echo "<h3>"; echo "To post a question you need to have at least one posting credit available, you can purchase posting credits "; echo "<a href='../users/buycredits.php'>By Clicking Here</a>"; echo "</h3>"; echo "</p>"; echo "</center>"; } ?> </div> <?php if (isset($_SESSION['logged']) && $_SESSION['logged'] == 1) { include('include/sidebar_1.php'); } else { include('include/sidebar_0.php'); } ?> <!-- content-wrap ends here --> <?php include('include/footer.php');?> <!-- wrap ends here --> </div> </body> </html> Link to comment https://forums.phpfreaks.com/topic/175702-form-help/#findComment-925932 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.