Reputation: 25
Hello I'm trying to save the value in the text input field to be saved in database.
What I'm trying to do is If I choose singing the singing will be saved and same with dancing If I choose Other in the selection the input will show and I'll be entering the value of other and that value I entered in the input text will be saved in the database.
The problem with my code is If I choose other and put a value in the text, the data value will be saved is only other.
Talent:
<form method=post action=test2.php>
<select name="talent" onchange="if( this.value=='other' ) { this.form['other'].style.visibility='visible' }else { this.form['other'].style.visibility='hidden' };" required/>
<option value="Dancing">Dancing</option>
<option value="Singing">Singing</option>
<option value="other">Other</option>
<input type="text" name="other" style="visibility:hidden;" />
</select>
<button type=submit>
</form>
$sql="INSERT INTO database (Talent) VALUES('$_POST[Talent]',)";
Upvotes: 0
Views: 989
Reputation: 553
You are inserting $_POST[Talent] in database. So if you select 'Other' option in dropdown,then instead of text box value,'Other' value will be inserted in db.
Try this
if($_POST['Talent'] == 'other'])
{
$other = $_POST['other'];//considering 'other' as name of your rextbox
$sql="INSERT INTO database (Talent) VALUES('$other',)";
}
else
{
$sql="INSERT INTO database (Talent) VALUES('$_POST['Talent']',)";
}
Upvotes: 0
Reputation: 543
You're input field saves the value in $_POST['other']. So you need to check, if other is selected and then insert the input field. So you're final solution will be:
if($_POST['talent'] == "other"])
$talent = $_POST['other'];
else
$talent = $_POST['talent'];
$sql="INSERT INTO database (Talent) VALUES('$talent',)";
Hint Never directly insert POST-values in your database. It's a big risk. For further informations read here: http://www.codeproject.com/Tips/407379/How-to-Protect-from-SQL-Injection-in-PhP-based-web Still you also need to escape it. Just read what you can do ;)
Upvotes: 1