daveylong Posted October 25, 2012 Share Posted October 25, 2012 I am trying to store information sent via a form into a MySQL DB. It doesn't load the next page nor does it store the information. My header requires init.php which I can confirm has the correct database connection credentials. The following is my HTML and "uploader" script - I know it must be something silly but this is where the issue lies, in the html and/or the uploader.php. If someone could run through my code and point out each issue (and possibly a re-work of my code) it would be very much appreciated! Thank you!! HTML (I've reduced the Date of Birth options so there's less code here) <h2>Choose Your File</h2> <form id="submit-photo" action="index.php?p=uploader" enctype="multipart/form-data" method="POST"> <input type="hidden" name="MAX_FILE_SIZE" value="5242880" /> <div id="FileUpload"> <input type="file" name="photo" id="BrowserHidden" onchange="getElementById('FileField').value = getElementById('BrowserHidden').value;" /> <div id="BrowserVisible"><input type="text" id="FileField" /></div> <span class="error"><?php if(isset($_SESSION['flash_message']['photo'])) echo $_SESSION['flash_message']['photo'] ?> </span></div> <fieldset> <label for="name">Name</label> <input type="text" name="name" id="name"> </fieldset> <fieldset> <label for="dob">DOB</label> <div class="dob-select"> <select name="dob_day" id="dob_day"> <option value="01">01</option> <option value="02">02</option> <option value="03">03</option> <option value="04">04</option> <option value="05">05</option> </select> </div> <div class="dob-select"> <select name="dob_month" id="dob_month"> <option value="01">Jan</option> <option value="02">Feb</option> <option value="03">Mar</option> <option value="04">Apr</option> </select> </div> <div class="dob-select"> <select name="dob_year" id="dob_year"> <option value="2012">2012</option> <option value="2011">2011</option> <option value="2010">2010</option> </select> </div> </fieldset> <fieldset> <label for="postcode">Postcode</label> <input type="text" class="short" name="postcode" id="postcode"> </fieldset> <fieldset> <label for="email">Email</label> <input type="email" name="email" id="email"> </fieldset> <fieldset> <label for="subscribe"><input type="checkbox" class="left" id="subscribe"> <p class="left">subscribe</p></label> <input type="submit" name="submit"> </fieldset> </form> DB Columns - id (auto-incremented) - name - photo (path to file) - email - date (date of birth: day, month, year dropdown selectors to be combined to form this) - postcode - subscribe (should be 0 or 1) - approve - created (timestamp) Uploader PHP <?php $error = array(); require_once 'init.php'; //Is request? if(strtolower($_SERVER['REQUEST_METHOD']) == 'post') { //$friend = ( $_POST['friend'] == 'true' ) ? 1 : 0; $required_array = array( 'name' => 'Name', 'dob_day' => 'Day', 'dob_month' => 'Month', 'dob_year' => 'Year', 'postcode' => 'Postcode', 'email' => 'Email Address', 'subscribe' => 'subscribe' ); $required_error = array(); foreach( $required_array as $field_name => $field ) { if(!isset($_POST[$field_name]) OR empty($_POST[$field_name]) OR $_POST[$field_name] == '') { $required_error[$field_name] = 'Please insert your '. $field; } } $_POST['email'] = verify_email($_POST['email']); if($_POST['email'] == FALSE && !isset($error['email'])) $error['email'] = 'Please use a valid email address'; //Validate the form key if(!isset($_POST['form_key']) || !$formKey->validate()) { //Form key is invalid, show an error $error['general'] = 'Use the real form!'; } else { if((!empty($_FILES["photo"])) && ($_FILES['photo']['error'] == 0)) { $filename = basename($_FILES['photo']['name']); $ext = substr($filename, strrpos($filename, '.') + 1); //Check if the file is JPEG image and it's size is less than 5Mb if ( ($ext == "jpg") && ($_FILES["photo"]["type"] == "image/jpeg") && ($_FILES["photo"]["size"] <= 5242880) ) { //Determine the path to which we want to save this file $newname = str_replace( ' ', '_', trim( strip_tags( $_POST['name'] ) ) ) . _ . $formKey->generateKey() . '_' . time() . '.jpg'; //Check if the file with the same name is already exists on the server if (!file_exists($newname)) { if (sizeof($required_error) == 0) { //Attempt to move the uploaded file to it's new place if ((move_uploaded_file($_FILES['photo']['tmp_name'], './photos/'. $newname))) { $move_status = 'done'; } else { $error['photo'] = "A problem occurred during file upload!"; } } } else { $error['photo'] = "File ".$_FILES["photo"]["name"]." already exists"; } } else { $error['photo'] = "Only .jpg images under 5Mb are accepted for upload". $_FILES["photo"]["size"] . $_FILES["photo"]["type"] . '====' . $ext; } } else { $error['photo'] = "No photo uploaded"; } } $error = $error + $required_error; if (sizeof($error) == 0 AND $move_status == 'done') { $_POST['date'] = $_POST['dob_day'].'-'.$_POST['dob_month'].'-'.$_POST['dob_year']; $query = sprintf("INSERT INTO `$db_name`.`submissionform` (`id` , `name` , `photo` , `email` , `date` , `postcode` , `subscribe` , `approve` , `created` ) VALUES ( NULL , '%s', '%s', '%s', '%s', '%s', '%s', '0', CURRENT_TIMESTAMP );", mysql_real_escape_string($_POST['name']), mysql_real_escape_string($newname), mysql_real_escape_string($_POST['email']), mysql_real_escape_string($_POST['date']), mysql_real_escape_string($_POST['postcode']), mysql_real_escape_string($_POST['subscribe']), mysql_real_escape_string($_POST['approve']), mysql_real_escape_string($_POST['message']) ); mysql_query('SET AUTOCOMMIT=0'); $result1 = mysql_query($query); $last_id = mysql_insert_id(); if ($result1) $success = 'Done'; else $error['general'] = 'Error when submitting your form, please try again.'; //mysql_free_result($result); mysql_close(); } } if ($success == 'Done') { $page = 'uploader'; include 'header.php'; echo '<img height="782" style="float:left;" src="./assets/img/success.png" />'; include 'footer.php'; } else { $_SESSION['flash_message'] = $error; $_SESSION['recent_field'] = $_POST; header('Location: ./index.php'); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/269897-issue-with-submitting-form-and-storing-in-a-mysql-db/ Share on other sites More sharing options...
daveylong Posted October 25, 2012 Author Share Posted October 25, 2012 I hope this helps... this issue is really bugging me Quote Link to comment https://forums.phpfreaks.com/topic/269897-issue-with-submitting-form-and-storing-in-a-mysql-db/#findComment-1387670 Share on other sites More sharing options...
PFMaBiSmAd Posted October 25, 2012 Share Posted October 25, 2012 Ummm. What have you done to determine what execution path your code is taking? Do you have php's error_reporting set to E_ALL and display_errors set to ON in your master php.ini so that php would report and display all the errors it detects? What exactly do you get when you submit the form? If you get a blank page, what does the 'view source' in your browser show? Quote Link to comment https://forums.phpfreaks.com/topic/269897-issue-with-submitting-form-and-storing-in-a-mysql-db/#findComment-1387671 Share on other sites More sharing options...
daveylong Posted October 25, 2012 Author Share Posted October 25, 2012 Ummm. What have you done to determine what execution path your code is taking? Do you have php's error_reporting set to E_ALL and display_errors set to ON in your master php.ini so that php would report and display all the errors it detects? What exactly do you get when you submit the form? If you get a blank page, what does the 'view source' in your browser show? I haven't set up any debugging - new to PHP to be honest. How would I set up the error reporting? when I submit, it shows "uploading progress" in the browser status bar and at 100% it loads the page again. Quote Link to comment https://forums.phpfreaks.com/topic/269897-issue-with-submitting-form-and-storing-in-a-mysql-db/#findComment-1387673 Share on other sites More sharing options...
White_Lily Posted October 25, 2012 Share Posted October 25, 2012 If he is only getting a blank page it would only the main tags in the source such as doctype, html, head, and body. nothing else. Quote Link to comment https://forums.phpfreaks.com/topic/269897-issue-with-submitting-form-and-storing-in-a-mysql-db/#findComment-1387674 Share on other sites More sharing options...
White_Lily Posted October 25, 2012 Share Posted October 25, 2012 (edited) create a file in your main directory called "php.ini" type in that file: display_errors = on error_reporting = -1 save and refresh your webpage -1 shows every single possible error so that the developer can solve as many problems as he/she can. Edited October 25, 2012 by White_Lily Quote Link to comment https://forums.phpfreaks.com/topic/269897-issue-with-submitting-form-and-storing-in-a-mysql-db/#findComment-1387675 Share on other sites More sharing options...
daveylong Posted October 25, 2012 Author Share Posted October 25, 2012 create a file in your main directory called "php.ini" type in that file: display_errors = on error_reporting = -1 save and refresh your webpage -1 shows every single possible error so that the developer can solve as many problems as he/she can. Did this but doesn't display any error? Page does the same thing - uploads file and loads same page. Quote Link to comment https://forums.phpfreaks.com/topic/269897-issue-with-submitting-form-and-storing-in-a-mysql-db/#findComment-1387677 Share on other sites More sharing options...
White_Lily Posted October 25, 2012 Share Posted October 25, 2012 try restarting your web-server Quote Link to comment https://forums.phpfreaks.com/topic/269897-issue-with-submitting-form-and-storing-in-a-mysql-db/#findComment-1387679 Share on other sites More sharing options...
daveylong Posted October 25, 2012 Author Share Posted October 25, 2012 I did it this way- by placing ini_set('display_errors', 'On'); error_reporting(E_ALL); at the top of uploader.php Errors are: Notice: Undefined variable: success in ..../uploader.php on line 100 Warning: Cannot modify header information - headers already sent by (output started at..../uploader.php:100) in ....uploader.php on line 110 Quote Link to comment https://forums.phpfreaks.com/topic/269897-issue-with-submitting-form-and-storing-in-a-mysql-db/#findComment-1387680 Share on other sites More sharing options...
White_Lily Posted October 25, 2012 Share Posted October 25, 2012 Change E_ALL to -1 to show all errors. The warning is the problem btw. I think there is a topic in the PHP Coding category on Header Errors. Quote Link to comment https://forums.phpfreaks.com/topic/269897-issue-with-submitting-form-and-storing-in-a-mysql-db/#findComment-1387681 Share on other sites More sharing options...
White_Lily Posted October 25, 2012 Share Posted October 25, 2012 Also look at the line its specifying to see if you already have that information set. Quote Link to comment https://forums.phpfreaks.com/topic/269897-issue-with-submitting-form-and-storing-in-a-mysql-db/#findComment-1387682 Share on other sites More sharing options...
daveylong Posted October 25, 2012 Author Share Posted October 25, 2012 All fixed was that I was missing a name for "subscribe" and <?php $formKey->outputKey(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/269897-issue-with-submitting-form-and-storing-in-a-mysql-db/#findComment-1387683 Share on other sites More sharing options...
White_Lily Posted October 25, 2012 Share Posted October 25, 2012 Good. Mark this as solved. Quote Link to comment https://forums.phpfreaks.com/topic/269897-issue-with-submitting-form-and-storing-in-a-mysql-db/#findComment-1387684 Share on other sites More sharing options...
daveylong Posted October 25, 2012 Author Share Posted October 25, 2012 Thank you Quote Link to comment https://forums.phpfreaks.com/topic/269897-issue-with-submitting-form-and-storing-in-a-mysql-db/#findComment-1387760 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.