Help with upload a 'record' that has drop down options... to database.

Here is what I am trying to do.

Create a “record” with following options.
. Name
. Select category
. Select Country
. Select Province
. Select City
. Upload image
Submit

Can someone quickly create a php code for this so that I can match it to mine?

I am able to upload ‘name’ ‘image’ to the database fine. However, I am having a difficulty having the drop down options show up in their respective database columns.

Also I have 3 different tables for Country,Province,City in the database; but the options shown in those dropdowns are done in javascript as oppose to using a “GET” function to retrieve them from the database. Is that wrong way to do it? I could not find any other way to do it while having the same dropdown function.

Please add your code, it’s easier for us to help fixing your code than writing new code that may or may not be used :slight_smile:

Alright here we go.

My mysql database “records” table looks like this.

id
Author_id
Title
Category_id
City
Province
Country
Path
Created(Date)

Note that from all the above fields, i was only able to make the id, Title and Category_id work. As well I have done resize image and upload to database path before in different session, so i know that’ll work.

So I need help with the rest.

Functions.php
[php]function get_cat() {
$data = mysql_query(“SELECT * FROM categories”);
while($cat = mysql_fetch_array($data)) {
echo ‘’ .$cat[‘Title’] . ‘’;
}
}

function add_record($title, $category, $user_id) {
$user_id = (int)$user_id;
$title = $_POST[‘title’];
$category = $_POST[‘category’];

$query = mysql_query("INSERT INTO records VALUES(null, '$user_id', '$title', '$category', '', '', '', '', '')");

}[/php]

add.php
[php]<?php
include “core/init.php”;
ob_start();
include “includes/overall/header.php”;

if (empty($_POST) === false) {
$required_fields = array(‘title’, ‘category’, ‘country’, ‘province’, ‘city’);

foreach($_POST as $key=>$value) {
	if (empty($value) && in_array($key, $required_fields) === true) {
		$errors[] = 'Please fill in all fields.';
		break 1;
	}
}

if (empty($errors) === true) { 
	
	if (!isset($_POST['title']) === true) {
		$errors[] = 'Please enter a title.';
	}
	
	if (!isset($_POST['category']) === true) {
		$errors[] = 'Please select a category.';
	}
	
	if (!isset($_POST['city']) === true) {
		$errors[] = 'Please select a city.';
	}
	
	if (!isset($_POST['province']) === true) {
		$errors[] = 'Please select a province.';
	}
	
	
}

}
?>

Add a record

<?php
if(isset($_GET['success']) === true && empty($_GET['success']) === true) {
	echo "Your record has been uploaded.";
} else {
	if (empty($_POST) === false && empty($errors) === true) {
		add_record($title, $category, $user_id);
		
		header('Location: add.php?success');
		exit();
		
	} else if (empty($errors) === false) {
		echo output_errors($errors);
	}
			
	?>
<form action="" method="post" enctype="multipart/form-data">
	<ul>
		<li>
			Record Title:<br>
			<input type="text" name="title" >
		</li>
		<li>
			Select Category:<br>
			<select name='category' id="category" >
				<?php get_cat();  ?>
			</select>
		</li><br>
		<li>
			<select name='country' id="country" >
				<option>Canada</option>
			</select><br>
			<!-- The first select list -->
			<select name="province" id="province" onchange="SList.getSelect('city', this.value);">
				<option value="select_province">Select Province</option>
				<option value="Alberta">Alberta</option>
				<option value="British Columbia">British Columbia</option>
				<option value="Manitoba">Manitoba</option>
				<option value="New Brunswick">New Brunswick</option>
				<option value="Newfoundland">Newfoundland</option>
				<option value="Northwest Territories">Northwest Territories</option>
				<option value="Nova Scotia">Nova Scotia</option>
				<option value="Nunavut">Nunavut</option>
				<option value="Ontario">Ontario</option>
				<option value="Prince Edward Island">Prince Edward Island</option>
				<option value="Quebec">Quebec</option>
				<option value="Saskatchewan">Saskatchewan</option>
				<option value="Yukon">Yukon</option>
			</select><br>
			<!-- Tag for the second dropdown list-->
			<span id="city"></span><br>
		</li>
		<li>
			Upload image:<br>
			<input type="file" name="image" >
		</li><br>
		<li>
			<input type="submit" name="submit" value="Add Deal" >
		</li>
	</ul>
</form>
<?php }

include “includes/overall/footer.php”; ?>[/php]

Btw, I know I’m using old Mysql that is depreciated. Truth is, it’s been a great ice breaker. I will switch over to Mysqli or PDO after i am done with this project.

No one got anything?

Sponsor our Newsletter | Privacy Policy | Terms of Service