Using html select to insert data into MySql db?

Hello everyone.

Off late I’ve had the need to use select tags in html to upload user information into the database. However I cannot get this done because I do not know what is happening.

<form>
<li class="short-input select">
<label for="property-type"> Property type:
<select name="property-type">
   <option name="apartment" value="apartment">apartment</option>
   <option name="bungalow" value="bungalow">bungalow</option>
   <option name="row-house" value="row-house">row-house</option>
   <option name="penthouse" value="penthouse">penthouse</option>
</select>
</label>
</li>

<input type="submit" name="submit" />
 </form>

That is my select tag structure. In my PHP I have got the following:

if (isset ($_POST['submit'])) {

$sql = "INSERT INTO properties (property-type) VALUES (:property-type)";
								$stmt = $pdo->prepare($sql);
								$stmt->bindParam(':property-type', $_POST['property-type']);
								$stmt->execute();

}

I can see I’m inserting $_POST[‘property-type’] into the db but how can I make that whichever option is selected?

Thanks in advance.

That’s what that code does. When you hit the ‘submit’ button, the current selection in your “property-type” field is passed through to the php code as $_POST[‘property-type’], so you should see that value going into your table.

What is happening that caused you to ask the question?

Note that the “name” parameter in each of your option tags has no effect - the “value” parameter contains the value passed into the $_POST array. The name is taken from the select tag that surrounds the various options.

Is it the NAME that is passed via POST, or is it the ID?

:slight_smile:

Be sure to add the action and method attributes to your form tag. If the method is not specified, the form will send info with GET through the url… In most cases, POST is a better option.
The action will direct form to the specified page. When empty, form is pointing to same page as the form or add a page name, e.g. processing.php to direct to that page.

<form action="" method="post">

I’m also wondering about those li tags. Does the mark-up pass validation?

I suppose if you add the ul tags it would be fine.

<?php
if (isset ($_POST['submit'])) {

    $sql = "INSERT INTO properties (property-type) VALUES (:property-type)";
    $stmt = $pdo->prepare($sql);
    $stmt->bindParam(':property-type', $_POST['property-type']);
    $stmt->execute();

}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title></title>

<style type="text/css">
 li.c1 {list-style: none}
</style>
</head>
<body>
<form action="" method="post">
    <ul>
        <li class="short-input select"><label for="property-type">Property type: <select name="property-type">
            <option value="apartment">apartment</option>
            <option value="bungalow">bungalow</option>
            <option value="row-house">row-house</option>
            <option value="penthouse">penthouse</option>
        </select></label></li>
        <li class="c1"><input type="submit" name="submit"></li>
    </ul>
</form>
</body>
</html>

Forms pass the values of the input, textarea and select fields that have a NAME to the server. They do not pass anything to the server by ID and since the option tags are a there simply to supply the allowed values and text for the select they don’t have a name attribute.

The FOR attribute on a label attaches the label to the form field with the corresponding ID.

In PHP submitting the form loads either the $_GET array or the $_POST array depending on whether the form tag has method=“POST” or not.

PHP unlike other server side languages does not load multiple fields with the same NAME as an array, instead it overwrites them so hat only the lst one in the form for each name gets passed.

I am getting an error and I do not understand why. The error goes:
SQLSTATE[HY093]: Invalid parameter number: parameter was not defined

Here is my full code.

<?php

include $_SERVER['DOCUMENT_ROOT']. '/inc/db.php';

$status = '';

session_start();

if (!isset ($_SESSION['password'])) {
	header("Location: index.php");
	}

$page_name = 'member';
$current_class = 'class="active"';
$image = '';	

if (isset ($_POST['submit'])) {
				
			if (
			(empty ($_POST['title'])) || 
			(empty ($_POST['location'])) || 
			(empty ($_POST['category'])) || 
			(empty ($_POST['price'])) || 
			(empty ($_POST['description'])) || 
			(empty ($_POST['address'])) || 
			(empty ($_POST['keywords']))
				)
				
				 	{			
					$status = "Please fill in the required fields";				
				 	}
			
								
						else {
								if (isset ($_FILES ['image']['tmp_name'])) {
										$image = file_get_contents ($_FILES ['image']['tmp_name']);
										}

								try {
									$sql = "INSERT INTO properties 
									(title, location, category, property_type, price, description, address, featured, keywords, thumbnail) 
									VALUES (:title, :location, :category, :property-type, :price, :description, :address, :featured, :keywords, :image)";
									$stmt = $pdo->prepare($sql);
									$stmt->bindParam(':title', $_POST['title']);
									$stmt->bindParam(':location', $_POST['location']);
									$stmt->bindParam(':category', $_POST['category']);
									$stmt->bindParam(':property-type', $_POST['property-type']);
									$stmt->bindParam(':price', $_POST['price']);
									$stmt->bindParam(':description', $_POST['description']);
									$stmt->bindParam(':address', $_POST['address']);
									$stmt->bindParam(':featured', $_POST['featured']);
									$stmt->bindParam(':keywords', $_POST['keywords']);
									$stmt->bindParam(':image', $image);
									$stmt->execute();
									$status = "Property added successfully!";
									
									$number = $pdo->query("SELECT id from properties order by id desc limit 1");
									$number = $number -> fetch (PDO::FETCH_OBJ);
									$number = $number->id; 
									header ("Location: single-edit.php?id=$number");
									}

									catch (PDOException $e) {
										echo 'sorry.'. $e->getMessage();
										}
							}
}

		
						
include 'inc/header.php';

?>

<?php include 'inc/sidebar.php'; ?>

<div class="admin-main"><br />

<h3> Add new listing <span class="red"><?php echo $status; ?></span></h3><br />

<main>

<form action="" method="post" enctype="multipart/form-data" class="add-property-form">

<li>
<label for="title">Title: <br />
<input type="text" name="title" maxlength="42" placeholder="For eg. 3 bhk for sale" />
</label>
</li>

<li class="short-input">
<label for="location">Location: <br />
<input type="text" name="location" placeholder="For eg. West London"  />
</label>
</li>

<li class="short-input">
<label for="category">Category: <br />
<input type="text" name="category" maxlength="5" placeholder="For eg. 3 bhk"  />
</label>
</li>

<li class="short-input select">
<label for="property-type"> Property type:
<select name="property-type">
   <option name="apartment" value="apartment">apartment</option>
   <option name="bungalow" value="bungalow">bungalow</option>
   <option name="row-house" value="row-house">row-house</option>
   <option name="penthouse" value="penthouse">penthouse</option>
</select>
</label>
</li>
<div class="clear"></div>

<li class="short">
<label for="price">Price: <br />
<input type="text" name="price" placeholder="For eg. $3,00,000"/>
</label>
</li>

<li class="description">
<label for="description">Property Description: <br />
<textarea  id="description" name="description" placeholder="Enter a detailed description of your property" />
</textarea>
</label>
</li>

<li class="address">
<label for="address">Landmark/Address: <br />
<input type="text" name="address" placeholder="For eg. Tower of hamlets"/>
</textarea>
</label>
</li>


<li>
<label for="featured"> Show as featured property? <span style="color:red;">(specify yes or no)</span><br />
<input type="text" name="featured" maxlength="3" value="No"/>
</label>
</li>

<li>
<label for="keywords"> Keywords <br />
<input type="text" name="keywords" placeholder="For eg. 2 BHK For Sale/Rent/Lease, West London, Properties in England, Europe" />
</label>
</li><br />

<li class="image-upload">Select image:
<input type="file" name="image" />
</li><br />
<br />
<input type="submit" name="submit" />

</form>

</main>

</div><!-- end admin main -->

<div class="clear"></div>

<script type="text/javascript">
    CKEDITOR.replace( 'description' );
</script>
<?php include 'inc/footer.php'; ?>

Yes, the entire code has been posted and was working ok before I decided to add the new select tags. Now it doesn’t work and I get the error SQLSTATE[HY093]: Invalid parameter number: parameter was not defined

Disregarding the invalid HTML that is likely to cause trouble once you get past this. “number” appears 6 times in your code example.

$number = $pdo->query("SELECT id from properties order by id desc limit 1");
$number = $number -> fetch (PDO::FETCH_OBJ);
$number = $number->id; 
header ("Location: single-edit.php?id=$number");

See the problem?

*hint, compare with the code examples here → http://php.net/manual/en/pdostatement.fetch.php

Thanks for your response. I deleted that block of code entirely but I still get the error. Moreover, the code prior to that $number bit doesn’t function like it should.

Disregarding the invalid HTML

Are you referring to me not using the ul tags? I didn’t think leaving them out would be a html problem. Or are you referring to something else?

You didn’t try fixing it?

Any way, just to be clear, the code you posted is the single-edit.php file?
And when you pasted the view-source mark-up into http://validator.w3.org/ it didn’t give you any errors?

The reason I mention having valid mark-up is because if it isn’t valid you can’t rely on it working as you hope it to. It might, but it might not.

Oh no. This is member.php and when the insert completes, I get the last id and open that in the single-edit.php so the uploader can then add an image gallery for that property id.

Thank you all for your inputs. I rewrote and reorganised the whole page because it had gotten quit messy after addition upon addition of code and now it is working.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.