Putting values into a MySQL table using a PHP form
I'm using a PHP form to put values into a table I have. Everything works fine, except the problem is that instead of the text in the dropdown, it puts the value of the dropdown selection.
Also, when I try to concatenate my dates and times, it somehow makes another MySQL row and puts the slashes and colons in that row.
Any help to fix that?
(I didn't make the form, I used a form-maker)
Here's the code:
<div id="form_container">
<ul >
<li id="li_4" >
<label class="description" for="element_4">restaurant </label>
<div>
<select class="element select medium" id="element_4" name="element_4">
<option value="" selected="selected"></option>
<option value="1" >Burger King</option>
<option value="2" >McDonald's</option>
<option value="3" >Wendy's</option>
<option value="4" >KFC</option>
<option value="5" >Taco Bell</option>
<option value="6" >Domino's Pizza</option>
<option value="7" >Pizza Hut</option>
<option value="8" >Quizno's</option>
<option value="9" >Chili's</option>
<option value="10" >TGI Friday's</option>
开发者_如何学Python<option value="11" >Starbucks</option>
<option value="12" >Red Lobster</option>
<option value="13" >Olive Garden</option>
</select>
</div>
</li> <li id="li_1" >
<label class="description" for="element_1">date </label>
<span>
<input id="element_1_1" name="element_1_1" class="element text" size="2" maxlength="2" value="" type="text"> /
<label for="element_1_1">MM</label>
</span>
<span>
<input id="element_1_2" name="element_1_2" class="element text" size="2" maxlength="2" value="" type="text"> /
<label for="element_1_2">DD</label>
</span>
<span>
<input id="element_1_3" name="element_1_3" class="element text" size="4" maxlength="4" value="" type="text">
<label for="element_1_3">YYYY</label>
</span>
<span id="calendar_1">
<img id="cal_img_1" class="datepicker" src="calendar.gif" alt="Pick a date.">
</span>
<script type="text/javascript">
Calendar.setup({
inputField : "element_1_3",
baseField : "element_1",
displayArea : "calendar_1",
button : "cal_img_1",
ifFormat : "%B %e, %Y",
onSelect : selectDate
});
</script>
</li> <li id="li_2" >
<label class="description" for="element_2">time </label>
<span>
<input id="element_2_1" name="element_2_1" class="element text " size="2" type="text" maxlength="2" value=""/> :
<label>HH</label>
</span>
<span>
<input id="element_2_2" name="element_2_2" class="element text " size="2" type="text" maxlength="2" value=""/> :
<label>MM</label>
</span>
<span>
<input id="element_2_3" name="element_2_3" class="element text " size="2" type="text" maxlength="2" value=""/>
<label>SS</label>
</span>
<span>
<select class="element select" style="width:4em" id="element_2_4" name="element_2_4">
<option value="AM" >AM</option>
<option value="PM" >PM</option>
</select>
<label>AM/PM</label>
</span>
</li> <li id="li_3" >
<label class="description" for="element_3">catering </label>
<span>
<input id="element_3_1" name="element_3" class="element radio" type="radio" value="1" />
<label class="choice" for="element_3_1">Yes</label>
<input id="element_3_2" name="element_3" class="element radio" type="radio" value="2" />
<label class="choice" for="element_3_2">No</label>
</span>
</li> <li id="li_5" >
<label class="description" for="element_5">driver </label>
<div>
<select class="element select medium" id="element_5" name="element_5">
<option value="" selected="selected"></option>
<option value="1" >Derek</option>
<option value="2" >Joe</option>
</select>
</div>
</li> <li id="li_6" >
<label class="description" for="element_6">destination </label>
<div>
<select class="element select medium" id="element_6" name="element_6">
<option value="" selected="selected"></option>
<option value="1" >NYC</option>
<option value="2" >LA</option>
<option value="3" >Chicago</option>
<option value="4" >Boston</option>
<option value="5" >Miami</option>
<option value="6" >San Francisco</option>
<option value="7" >Seattle</option>
<option value="8" >Other</option>
</select>
</div>
</li>
<li class="buttons">
<input type="hidden" name="form_id" value="76657" />
<input id="saveForm" class="button_text" type="submit" name="submit" value="Submit" />
</li>
</ul>
</form>
</div>
<img id="bottom" src="bottom.png" alt="">
</body>
</html>
PHP code
<?php
$restaurant = $_POST['element_4'];
$dateM = $_POST['element_1_1'];
$dateD = $_POST['element_1_2'];
$dateY = $_POST['element_1_3'];
$timeH = $_POST['element_2_1'];
$timeM = $_POST['element_2_2'];
$timeS = $_POST['element_2_3'];
$amPM = $_POST['element_2_4'];
$catering = $_POST['element_3'];
$driver = $_POST['element_5'];
$destination = $_POST['element_6'];
$date = $dateM . "/" . $dateD . "/" . $dateY;
$time = $timeH . ":" . $timeM . ":" . $timeS . " " . $amPM;
$con = mysql_connect("localhost","asdf","asdf");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("tableName", $con);
mysql_query("INSERT INTO stats (restaurant, date, time, catering, driver, destination)
VALUES ('$restaurant', '$date', '$time', '$catering', '$driver', '$destination')");
mysql_close($con);
?>
Just set the value to match the text, Example:
<option value="Burger King">Burger King</option>
<option value="McDonald's">McDonald's</option>
If you leave out the values the form will submit the text between the option tags such as...
<option>Burger King</option>
<option>McDonald's</option>
However, for normalisation purposes I would opt for using the numeric value and have a separate db table for my restaurants. Such that the name of the restaurant is only ever in one place in the database and referenced eslewhere by the restaurant primary key. You will still be able to access the restaurant name using join queries later.
Also you could dynamically generate your dropdown options using a query which accesses the restaurant db table.
Firstly, it's worth mentioning that there is no user-input handling to check for SQL injection, which is pretty dangerous in any public-facing website. So fix that using mysql_strip_slashes() or similar. If you want the value of the actual text in the dropdown selection, you're going to have to change the value parameter to the tag. That's the only way.
Everything works fine, except the problem is that instead of the text in the dropdown, it puts the value of the dropdown selection.
Yes, that's what's meant to happen. As per other answers you will need to put the text in the "value" attribute instead of 1, 2, 3 etc.
With regards to the other problem with the dates, you could try printing the generated SQL query to the screen so you can inspect exactly what is going on.
I don't see any form open tag in your html... unless there is a form tag your form cannot be submitted. also on the PHP script do a var_dump($_POST);
精彩评论