LeggoMaEggo
LeggoMaEggo

Reputation: 541

Save selected option in dropdown menu in php

I want to insert an image into a selected category in the drop down menu. Didn't want to hard code the menu in html so it can be dynamically updated by phpmyadmin to menu. Currently, all images insert in Greeting_Cards table because it is a place holder for now.

I have tried saving it as a variable: $selected = $_POST['tables']; and passing it as $selected rather than greeting_cards, but that throws back a notice of undefined index and doesn't add to any table at all.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>upload</title>

</head>

<body>

<form action="upload.php" method="POST" enctype="multipart/form-data">
<input type="file" name="image" />
<input type="submit" name="submit" value="upload" />
</form>


<?php
require_once('/var/db_file.php');

//To do after submit button
if(isset($_POST['submit']))
{
	mysql_connect("localhost","root", $pass);
	mysql_select_db("images");

	$imageName = mysql_real_escape_string($_FILES["image"]["name"]);
	$imageData = mysql_real_escape_string(file_get_contents($_FILES["image"]["tmp_name"]));
	$imageType = mysql_real_escape_string($_FILES["image"]["type"]);

	/* Drop down menu */
	$dbname = "images";
	$sql = "SHOW TABLES FROM $dbname";
	$result = mysql_query($sql);
	$tableNames=array();

	while($row = mysql_fetch_row($result)){
		$tableNames[] = $row[0];
	}

	echo '<select name="tables" id="tables">';
	foreach($tableNames as $name){
		echo '<option value="' . $name . '">' . $name . '</option>';
	}
	echo '</select>';
	/* Drop down menu end */

    $selected = $_POST['tables'];

	echo '<br>';

	if(substr($imageType,0,5) == "image"){
		mysql_query("INSERT INTO `$selected` VALUES('','$imageName','$imageData')");
		echo "Image Uploaded!";
	}
	else{
		echo "Has to be an image!";
	}
}


?>

</body>
</html>

Edit 1: Added the $selected variable in, instead of Greeting_Cards Fix: Moved ending tag of form to encompass the php code. Thanks for your help!

Upvotes: 0

Views: 1463

Answers (1)

alariva
alariva

Reputation: 2139

In this code snippet, the $_POST['tables'] is not getting the assigned value, as the form does not have the select dropdown named tables.

Despite the dropdown is being echoed, it is outside the <form>, thus not being submitted.

Upvotes: 1

Related Questions