DasHaas Posted August 2, 2007 Share Posted August 2, 2007 I have a form that needs to perform different actions, insert validated data into my table, and upload a file to my server. I also need the file location to be inserted into one of the fields in my table. I got everything working except for the upload & insert file path part. Here is my code, any help would be greatly appreciated! <?php } else { // includes include('includes/PXS_Conf.php'); // set up error list array $errorList = array(); $name = ucfirst($_POST['txtname']); $email = ($_POST['txtemail']); $phone = ($_POST['txtphone']); $password = trim($_POST['txtpassword']); $password1 = trim($_POST['txtpassword1']); $impressions = ($_POST['sslimpressions']); $adtype = ($_POST['ssladtype']); $file = ($_POST['file']); $url = ($_POST['txturl']); $alt = ucwords($_POST['txtalt']); // validate text input fields if (trim($_POST['txtname']) == '') {$errorList[] = 'Please enter your name';} if (trim($_POST['txtemail']) == '') {$errorList[] = "Please enter your email address";} if (trim($_POST['txtphone']) == '') {$errorList[] = "Please enter your phone number";} if (trim($_POST['txtpassword']) == '') {$errorList[] = "Please enter a password";} if (trim($_POST['txtpassword1']) == '') {$errorList[] = "Please re-enter your password";} if (trim($_POST['sslimpressions']) == '') {$errorList[] = "Please select a number of impressions";} if (trim($_POST['ssladtype']) == '') {$errorList[] = "Please select ad type";} if (trim($_POST['file']) == '') {$errorList[] = "Please select an image to upload";} if (trim($_POST['txturl']) == '') {$errorList[] = "Please enter your website url";} if (trim($_POST['txtalt']) == '') {$errorList[] = "Please enter your business name";} if ($_POST['txtpassword'] != $_POST['txtpassword1']) {$errorList[] = "Passwords did not match";} // check for errors // if none found... if (sizeof($errorList) == 0) { // open database connection $connection = mysql_connect($host, $user, $pass) or die ('Unable to connect to MySql server!'); // select database mysql_select_db($db) or die ('Unable to select database'); // generate and execute query $query = "INSERT INTO PXS_Ads(href, src, alt, impressions_bought, impressions_left, ad_date, contact_name, contact_email, contact_phone, status, type, password) VALUES('$url','$file','$alt','$impressions','$impressions',NOW(),'$name','$email','$phone','0','$adtype','$password')"; $result = mysql_query($query) or die ("Error in query: $query. " . mysql_error()); // close database connection mysql_close($connection); // print result echo '<center> Request subitted!<br /> We will be contacting you shortly<br /> <a href=index.php>Return to website</a> </center>'; } else { // errors found // print as list echo 'The following errors were encountered:'; echo '<br>'; echo '<ul>'; for ($x=0; $x<sizeof($errorList); $x++) { echo "<li>$errorList[$x]"; } echo '</ul>'; } } ?> ???Thanks in advance! ??? Quote Link to comment https://forums.phpfreaks.com/topic/63081-php-insert-data-and-upload-file-at-the-same-time/ Share on other sites More sharing options...
premiso Posted August 2, 2007 Share Posted August 2, 2007 You need to use $_FILES instead of $_POST['file']. You may want to take a look at this: http://us3.php.net/features.file-upload as it seems you are just guessing. Quote Link to comment https://forums.phpfreaks.com/topic/63081-php-insert-data-and-upload-file-at-the-same-time/#findComment-314149 Share on other sites More sharing options...
DasHaas Posted August 2, 2007 Author Share Posted August 2, 2007 Well I tried the following and it did not work, I looked at the site you gave me but I had problems deciphering the code. It seems like they had some validating functions as well, I just need it to work first then I will try to add bells and whistles. I snagged this from another post but it does not work for me ???. $target_path = "images/"; $target_path = $target_path . basename( $_FILES['file']['name']); if(move_uploaded_file($_FILES['file']['tmp_name'], $target_path)) { echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";} Im confused with the following parts: file - should this be the name of the upload field in my form? name - is this from the form? tmp_name - is this somthing i assign or is it the original name from the users pc Quote Link to comment https://forums.phpfreaks.com/topic/63081-php-insert-data-and-upload-file-at-the-same-time/#findComment-314255 Share on other sites More sharing options...
premiso Posted August 2, 2007 Share Posted August 2, 2007 Post the form code, chances are you do not have the enctype defined. Quote Link to comment https://forums.phpfreaks.com/topic/63081-php-insert-data-and-upload-file-at-the-same-time/#findComment-314266 Share on other sites More sharing options...
DasHaas Posted August 2, 2007 Author Share Posted August 2, 2007 <?php // form not yet submitted // display initial form if (!$_POST['submit']) { ?> <table width="484" cellpadding="3" cellspacing="3" class="table1"> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <tr> <td width="199" valign="top" class="text5"><div align="right" class="form_text">name</div></td> <td width="262"><input name="txtname" type="text" id="txtname" /></td> </tr> <tr> <td valign="top" class="text5"><div align="right"><span class="form_text">email address</span> <div class="form_text_desc">We fight spam.</div> </div></td> <td><input name="txtemail" type="text" id="txtemail" /></td> </tr> <tr> <td valign="top" class="text5"><div align="right" class="form_text">phone number </div></td> <td><input name="txtphone" type="text" id="txtphone" maxlength="10" /></td> </tr> <tr> <td><div align="right"><span class="form_text">password </span> <div class="form_text_desc">Make it strong!</div> </div></td> <td><input name="txtpassword" type="password" id="txtpassword" maxlength="7" /></td> </tr> <tr> <td><div align="right"><span class="form_text">confirm password </span> <div class="form_text_desc">Really strong...</div> </div></td> <td><input name="txtpassword1" type="password" id="txtpassword1" maxlength="7" /></td> </tr> <tr> <td><div align="right" class="form_text">number of impressions </div></td> <td><select name="sslimpressions" id="sslimpressions"> <option>number of impressions</option> <option value="5000">5,000</option> <option value="10000">10,000</option> <option value="15000">15,000</option> <option value="20000">20,000</option> </select></td> </tr> <tr> <td><div align="right"><span class="form_text">ad type </span> <div class="form_text_desc">Body = 200x249 | Header = 134x23</div> </div></td> <td><select name="ssladtype" id="ssladtype"> <option>select ad size</option> <option value="1">header ad</option> <option value="2">body ad</option> </select></td> </tr> <tr> <td><div align="right" class="form_text">upload image<span class="form_text_desc"><br /> .GIF, .JPG only, 300k max </span><br /> </div></td> <td><input type="file" name="file" /></td> </tr> <tr> <td><div align="right"><span class="form_text">your website url </span> <div class="formexpl"><span class="form_text_desc">i.e. http://www.yoursite.com </span>.</div> </div></td> <td><input name="txturl" type="text" id="txturl" /></td> </tr> <tr> <td><div align="right"><span class="form_text">image description</span> <div class="form_text_desc">i.e YOUR NAME ...</div> </div></td> <td><input name="txtalt" type="text" id="txtalt" /></td> </tr> <tr> <td colspan="2"><div align="center"> <input type="submit" name="submit" value="Add" /> <input type="reset" name="Reset" value="Reset" /> </div></td> </tr> </form> </table> <?php } else { // includes include('includes/PXS_Conf.php'); // set up error list array $errorList = array(); $name = ucfirst($_POST['txtname']); $email = ($_POST['txtemail']); $phone = ($_POST['txtphone']); $password = trim($_POST['txtpassword']); $password1 = trim($_POST['txtpassword1']); $impressions = ($_POST['sslimpressions']); $adtype = ($_POST['ssladtype']); $file = ($_FILES['file']); $url = ($_POST['txturl']); $alt = ucwords($_POST['txtalt']); // validate text input fields an check password if (trim($_POST['txtname']) == '') {$errorList[] = 'Please enter your name';} if (trim($_POST['txtemail']) == '') {$errorList[] = "Please enter your email address";} if (trim($_POST['txtphone']) == '') {$errorList[] = "Please enter your phone number";} if (trim($_POST['txtpassword']) == '') {$errorList[] = "Please enter a password";} if (trim($_POST['txtpassword1']) == '') {$errorList[] = "Please re-enter your password";} if (trim($_POST['sslimpressions']) == '') {$errorList[] = "Please select a number of impressions";} if (trim($_POST['ssladtype']) == '') {$errorList[] = "Please select ad type";} if (trim($_POST['file']) == '') {$errorList[] = "Please select an image to upload";} if (trim($_POST['txturl']) == '') {$errorList[] = "Please enter your website url";} if (trim($_POST['txtalt']) == '') {$errorList[] = "Please enter your business name";} if ($_POST['txtpassword'] != $_POST['txtpassword1']) {$errorList[] = "Passwords did not match";} // check for errors // if none found... if (sizeof($errorList) == 0) { //upload file to server $target_path = "images/"; $target_path = $target_path . basename( $_FILES['file']['txtname']); if(move_uploaded_file($_FILES['file']['name'], $target_path)) { echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";} // open database connection $connection = mysql_connect($host, $user, $pass) or die ('Unable to connect to MySql server!'); // select database mysql_select_db($db) or die ('Unable to select database'); // generate and execute query $query = "INSERT INTO PXS_Ads(href, src, alt, impressions_bought, impressions_left, ad_date, contact_name, contact_email, contact_phone, status, type, password) VALUES('$url','$uploadfile','$alt','$impressions','$impressions',NOW(),'$name','$email','$phone','0','$adtype','$password')"; $result = mysql_query($query) or die ("Error in query: $query. " . mysql_error()); // close database connection mysql_close($connection); Quote Link to comment https://forums.phpfreaks.com/topic/63081-php-insert-data-and-upload-file-at-the-same-time/#findComment-314275 Share on other sites More sharing options...
premiso Posted August 2, 2007 Share Posted August 2, 2007 <form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> You need the enctype to transfer files via a form. Quote Link to comment https://forums.phpfreaks.com/topic/63081-php-insert-data-and-upload-file-at-the-same-time/#findComment-314279 Share on other sites More sharing options...
DasHaas Posted August 2, 2007 Author Share Posted August 2, 2007 ah ok, ill give that a try. Thanks a million Quote Link to comment https://forums.phpfreaks.com/topic/63081-php-insert-data-and-upload-file-at-the-same-time/#findComment-314282 Share on other sites More sharing options...
soycharliente Posted August 2, 2007 Share Posted August 2, 2007 echo "<li>$errorList[$x]"; You can't do that can you? Wouldn't he need to use this??? echo "<li>{$errorList[$x]}"; Quote Link to comment https://forums.phpfreaks.com/topic/63081-php-insert-data-and-upload-file-at-the-same-time/#findComment-314285 Share on other sites More sharing options...
premiso Posted August 2, 2007 Share Posted August 2, 2007 echo "<li>$errorList[$x]"; You can't do that can you? Wouldn't he need to use this??? echo "<li>{$errorList[$x]}"; No he can do that, it would be a problem if single quotes were in use, than you would need the { } curly braces around it. Quote Link to comment https://forums.phpfreaks.com/topic/63081-php-insert-data-and-upload-file-at-the-same-time/#findComment-314289 Share on other sites More sharing options...
DasHaas Posted August 2, 2007 Author Share Posted August 2, 2007 meh, file still wont upload :'( Quote Link to comment https://forums.phpfreaks.com/topic/63081-php-insert-data-and-upload-file-at-the-same-time/#findComment-314292 Share on other sites More sharing options...
soycharliente Posted August 2, 2007 Share Posted August 2, 2007 What's your code? Quote Link to comment https://forums.phpfreaks.com/topic/63081-php-insert-data-and-upload-file-at-the-same-time/#findComment-314293 Share on other sites More sharing options...
DasHaas Posted August 2, 2007 Author Share Posted August 2, 2007 $target_path = "images/"; $target_path = $target_path . basename( $_FILES['file']['txtname']); move_uploaded_file($_FILES['file']['name'], $target_path); This is the form <form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <tr> <td width="234" valign="top" class="text5"><div align="right" class="form_text">Your name</div></td> <td width="227"><input name="txtname" type="text" id="txtname" /></td> </tr> <tr> <td valign="top" class="text5"><div align="right"><span class="form_text">email address</span> <div class="form_text_desc">We fight spam</div> </div></td> <td><input name="txtemail" type="text" id="txtemail" /></td> </tr> <tr> <td valign="top" class="text5"><div align="right" class="form_text">phone number </div></td> <td><input name="txtphone" type="text" id="txtphone" maxlength="10" /></td> </tr> <tr> <td><div align="right"><span class="form_text">password </span> <div class="form_text_desc">Make it strong!</div> </div></td> <td><input name="txtpassword" type="password" id="txtpassword" maxlength="7" /></td> </tr> <tr> <td><div align="right"><span class="form_text">confirm password </span> <div class="form_text_desc">Really strong...</div> </div></td> <td><input name="txtpassword1" type="password" id="txtpassword1" maxlength="7" /></td> </tr> <tr> <td><div align="right" class="form_text"> impressions </div></td> <td><select name="sslimpressions" id="sslimpressions"> <option>number of impressions</option> <option value="5000">5,000</option> <option value="10000">10,000</option> <option value="15000">15,000</option> <option value="20000">20,000</option> </select></td> </tr> <tr> <td><div align="right"><span class="form_text">type </span> <div class="form_text_desc">Body = 200x249 | Header = 468x60</div> </div></td> <td><select name="ssladtype" id="ssladtype"> <option>select ad size</option> <option value="1">header ad</option> <option value="2">body ad</option> </select></td> </tr> <tr> <td><div align="right" class="form_text">upload image<span class="form_text_desc"><br /> .GIF, .JPG only, 300k max </span><br /> </div></td> <td><input type="file" name="file" /></td> </tr> <tr> <td><div align="right"><span class="form_text">your website url<br /> </span><span class="form_text_desc">i.e. http://www.yoursite.com</span> </div> </div></td> <td><input name="txturl" type="text" id="txturl" /></td> </tr> <tr> <td><div align="right"><span class="form_text">image description</span> <div class="form_text_desc">i.e YOUR COMPANY NAME ...</div> </div></td> <td><input name="txtalt" type="text" id="txtalt" /></td> </tr> <tr> <td colspan="2"><div align="center"> <input type="submit" name="submit" value="Add" /> <input type="reset" name="Reset" value="Reset" /> </div></td> </tr> </form> Quote Link to comment https://forums.phpfreaks.com/topic/63081-php-insert-data-and-upload-file-at-the-same-time/#findComment-314297 Share on other sites More sharing options...
premiso Posted August 2, 2007 Share Posted August 2, 2007 $target_path = "images/"; $target_path = $target_path . basename( $_POST['txtname']); move_uploaded_file($_FILES['file']['tmp_name'], $target_path); I highly suggest looking at this site closer, as it seems you just skimmed over it and ignored most of it. http://us3.php.net/features.file-upload Quote Link to comment https://forums.phpfreaks.com/topic/63081-php-insert-data-and-upload-file-at-the-same-time/#findComment-314300 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.