Jump to content

Issue With Submitting Form And Storing In A Mysql Db


daveylong

Recommended Posts

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');
}
?>

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 by White_Lily
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.