textarea在MySQL数据库字段PHP中为空 [英] Textarea is empty in mySQL database field PHP
问题描述
我创建了一个HTML表单并试图将数据从其字段保存到数据库中。输入,下拉菜单和单选按钮被正确存储,但textarea总是作为空字段添加到数据库中。以下是代码:
HTML:
< head>
< link rel =stylesheettype =text / csshref =feedback_form_style.css>
< / head>
< form name =feedbackformmethod =postclass =basic-greyaction =feedback_form_send.php>
< h1>意见表单
< span>请回答以下问题。< / span>
< / h1>
< table width =450px>
< / tr>
< tr>
< td valign =top>
< label for =School>学校< / label>
< / td>
< td valign =top>
< input type =textname =Schoolplaceholder =您的学校名称maxlength =50size =30required /> < ;!在Safari中不需要支持>
< / td>
< / tr>
< tr>
< td valign =top>
< label for =年份>年份< / label>
< / td>
< td valign =top>
< input type =textname =Yearplaceholder =你是哪一年maxlength =50size =30/>
< / td>
< / tr>
< tr>
< td valign =top>
< label for =Gender>性别< / label>
< / td>
< td valign =top>
< select name =Gender>
< option value =male/>男性< / option>
< option value =female/>女性< / option>
< / select>
< / td>
< / tr>
< / table>
< p>< / p> <!EMPTYLINE>
< table>
< tr>
< td valign =top>
< label for =你认为和工程师做了什么>你认为工程师会做什么? < /标签>
< / td>
< / tr>
< tr>
< td valign =top>
< textarea name =你认为工程师做什么placeholder =你的答案在这里maxlength =1000cols =60rows =10/>< / textarea>
< / td>
< / tr>
< tr> < TD>< p为H.;< / P>< / TD>< / TR> <!EMPTYLINE>
< tr>
< td valign =top>
< / td>
< / tr>
< tr>
< td valign =top>
< textarea name =什么是今天访问最令人愉快的部分placeholder =您的答案在这里maxlength =1000cols =60rows =10>< / textarea>
< / td>
< / tr>
< tr> < TD>< p为H.;< / P>< / TD>< / TR> <!EMPTYLINE>
< tr>
< td valign =top>
< label for =有什么你不喜欢的?>有没有你不喜欢的东西? < /标签>
< / td>
< / tr>
< tr>
< td valign =top>
< input type =radioname =有什么你不喜欢的? value =yes>是(请在下面详细说明)
& nbsp;
< input type =radioname =有什么你不喜欢的? value =no>无
< / td>
< / tr>
< tr>
< td valign =top>
< textarea name =有什么你不喜欢的(评论)? placeholder =Details heremaxlength =1000cols =60rows =10>< / textarea>
< / td>
< / tr>
< tr> < TD>< p为H.;< / P>< / TD>< / TR> <!EMPTYLINE>
< tr>
< td valign =top>
< / td>
< / tr>
< tr>
< td valign =top>
< input type =radioname =你会考虑在大学学习工程吗? value =是>是
& nbsp;
< input type =radioname =你会考虑在大学学习工程吗? value =no>否
& nbsp;
< input type =radioname =你会考虑在大学学习工程吗? value =确定>不确定
< / td>
< / tr>
< tr> < TD>< p为H.;< / P>< / TD>< / TR> <!EMPTYLINE>
< tr>
< td valign =top>
< label for =今天的会议是否激励你考虑成为一名工程师?>今天的会议是否激励你考虑成为一名工程师? < /标签>
< / td>
< / tr>
< tr>
< td valign =top>
< input type =radioname =今天的会议是否激励你考虑成为一名工程师? value =是>是
& nbsp;
< input type =radioname =今天的会议是否激励你考虑成为一名工程师? value =no>否
& nbsp;
< input type =radioname =今天的会议是否激励你考虑成为一名工程师? value =确定>不确定
< / td>
< / tr>
< tr> < TD>< p为H.;< / P>< / TD>< / TR> <!EMPTYLINE>
< tr>
< td valign =top>
< label for =在今天的会议之前,你对工程知识有多了解?>在今天的会议之前,你对工程有很多了解吗? < /标签>
< / td>
< / tr>
< tr>
< td valign =top>
< input type =radioname =在今天的会议之前,你对工程有多少了解? value =是>是
& nbsp;
< input type =radioname =在今天的会议之前,你对工程有多少了解? value =no>否
& nbsp;
< input type =radioname =在今天的会议之前,你对工程有多少了解?值=不确定>有点
< / td>
< / tr>
< tr> < TD>< p为H.;< / P>< / TD>< / TR> <!EMPTYLINE>
< tr>
< td valign =top>
< label for =今天的课程是否让你了解了很多关于工程的知识?>今天的课程是否教给你很多有关工程的知识? < /标签>
< / td>
< / tr>
< tr>
< td valign =top>
< input type =radioname =今天的课程是否让你了解了很多关于工程的知识? value =是>是
& nbsp;
< input type =radioname =今天的课程是否让你了解了很多关于工程的知识? value =no>否
& nbsp;
< input type =radioname =今天的课程是否让你了解了很多关于工程的知识?值=不确定>有点
< / td>
< / tr>
< tr> < TD>< p为H.;< / P>< / TD>< / TR> <!EMPTYLINE>
< tr>
< td valign =top>
< / td>
< / tr>
< tr>
< td valign =top>
< / td>
< / tr>
< tr> < TD>< p为H.;< / P>< / TD>< / TR> <!EMPTYLINE>
< tr>
< td valign =top>
< label for =评论/建议/问题>评论/建议/问题< / label>
< / td>
< / tr>
< tr>
< td valign =top>
< textarea name =评论/建议/问题placeholder =在此输入您的意见,建议和问题maxlength =1000cols =60rows =10>< / textarea>
< / td>
< / tr>
< tr> < TD>< p为H.;< / P>< / TD>< / TR> <!EMPTYLINE>
< tr>
< td colspan =2style =text-align:center>
< input type =submitclass =submitvalue =Submit>
< / td>
< / tr>
< tr>
< td colspan =2style =text-align:center>
感谢您的反馈
< / td>
< / tr>
< tr>
< td colspan =2style =text-align:center>
©工程中的女孩2015
< / td>
< / tr>
< / table>
< / form>
PHP:
$ value = test_input($ _ POST ['School']);
$ value2 = test_input($ _ POST ['您认为工程师会怎么做']);
$ sql =插入到form1(input1,input2)VALUES('$ value','$ value2');
if($ _ POST){
echo'< pre>;
print_r($ _ POST);
echo'< / pre>';
print_r($ _ POST)的输出:
Array
(
[School] => 123
[Year] =>
[性别] =>雄
[What_do_you_think_an_engineer_does] => 234
[What_was_the_most_enjoyable_part_about_the_visit_today] =>
[Was_there_anything_you_did_not_like_(评语)?] =>
[Based_on_today's_session,_what_do_you_think_an_engineer_does? ] =>
[评论/建议/问题] =>
)
感谢您的帮助。
使用 name =Comments
在textarea。因为这就是 $ _ POST ['Comments']
链接到的内容。
假设您的代码
< textarea name =今天访问最愉快的部分是什么placeholder =您的答案在这里maxlength =1000 cols =60rows =10>< / textarea>
你的帖子应该是 $ _ POST ['最愉快的部分是什么今天的访问']
如果您想以数组形式传递评论
< textarea name =Comments []placeholder =您的答案在这里maxlength =1000cols =60rows =10>< / textarea>
提示:使用以下代码在页面feedback_form_send.php上进行调试,以检查发送给哪些值它通过后提交;
if($ _ POST){
echo'< pre>;
print_r($ _ POST);
echo'< / pre>';
}
编辑
建议:您的代码易受Sql注入攻击。使用PDO或mysqli代替mysql
EDITED
我的打印带有值显示
Array
(
[School] => myschool
[Year] => 2018
[性别] =>男性
[What_do_you_think_an_engineer_does] =>工程师睡觉
[What_was_the_most_enjoyable_part_about_the_visit_today] =>很好
[Was_there_anything_you_did_not_like?] =>是
[Was_there_anything_you_did_not_like_评论)?] =>太美丽
[Would_you_consider_studying_Engineering_at_university?] =>是
[Has_today's_session_inspired_you_to_consider_becoming_an_engineer?] => no
[Before_today's_session_did_you_know_much_about_engineering?] =>不确定
[Has_todays_session_taught_you_much_about_engineering?] =>是
[Based_on_today's_session,_what_do_you_think_an_engineer_does?] =>工程师努力工作
[评论/建议/问题] => n o评论,我建议做的事情
)
所以你的php变量应该被赋值喜欢这个。 这只是一个可以理解的演示。在使用之前,请务必记得清理输入
$ school = $ _ POST ['School'];
$ year = $ _ POST ['Year'];
$ gender = $ _ POST ['Gender'];
$ whatengineerdoes = $ _ POST ['What_do_you_think_an_engineer_does'];
$ enjoyable_part = $ _ POST ['What_was_the_most_enjoyable_part_about_the_visit_today'];
$ youdontlike = $ _ POST ['Was_there_anything_you_did_not_like?'];
$ didnotlike = $ _ POST ['Was_there_anything_you_did_not_like_(comment)?'];
$ wouldyou = $ _ POST ['would_you_consider_studying_Engineering_at_university?'];
$ session_inspired = $ _ POST [Has_today's_session_inspired_you_to_consider_becoming_an_engineer?];
$ before_session = $ _ POST [Before_today's_session_did_you_know_much_about_engineering?];
$ has_taught = $ _ POST ['Has_todays_session_taught_you_much_about_engineering?'];
$ based_on = $ _ POST [Based_on_today's_session,_what_do_you_think_an_engineer_does?];
$ comments = $ _ POST ['Comments / Suggestions / Questions'];
编辑
可以将上面的数据插入到你的表中
$ dbhost =localhost;
$ dbname =mydatabase;
$ dbusername =root;
$ dbpassword =mypppasss;
$ link = new PDO(mysql:host = $ dbhost; dbname = $ dbname,$ dbusername,$ dbpassword);
$ statement = $ link-> prepare(insert into form1(input1,input2,input3,input4,input5,input6,input7,input8,input9,input10,input11,input12,input13)
VALUES (:INP1,:INP2,:inp3,:INP4,:INP5,:inp6,:inp7,:inp8,:inp9,:inp10,:inp11,:inp12,:inp13));
$ statement-> execute(array(
inp1 => $ school,
inp2 => $ year,
inp3 => $ gender,
inp4 => $ whatengineerdoes,
inp5 => $ enjoyable_part,
inp6 => $ youdontlike,
inp7 => $ didnotlike,
inp8 => $ wouldyou ,
inp9 => $ session_inspired,
inp10 => $ before_session,
inp11 => $ has_taught,
inp12 => $ based_on,
inp13 => $ comments
));
i created an HTML form and trying to save the data from its fields to the database. Inputs, drop-down menus and radio button are stored correctly however the textarea is always added to the database as empty field. Here is the code:
HTML:
<head>
<link rel="stylesheet" type="text/css" href="feedback_form_style.css">
</head>
<form name="feedbackform" method="post" class="basic-grey" action="feedback_form_send.php">
<h1>Feedback form
<span>Please answer the questions below.</span>
</h1>
<table width="450px">
</tr>
<tr>
<td valign="top">
<label for="School">School</label>
</td>
<td valign="top">
<input type="text" name="School" placeholder="Name of your school" maxlength="50" size="30" required/> <! required not supported in Safari>
</td>
</tr>
<tr>
<td valign="top">
<label for="Year">Year</label>
</td>
<td valign="top">
<input type="text" name="Year" placeholder="Which year you are" maxlength="50" size="30"/>
</td>
</tr>
<tr>
<td valign="top">
<label for="Gender">Gender</label>
</td>
<td valign="top">
<select name="Gender">
<option value="male"/>Male</option>
<option value="female"/>Female</option>
</select>
</td>
</tr>
</table>
<p></p> <!emptyline>
<table>
<tr>
<td valign="top">
<label for="What do you think and engineer does">What do you think an engineer does? </label>
</td>
</tr>
<tr>
<td valign="top">
<textarea name="What do you think an engineer does" placeholder="Your answer here" maxlength="1000" cols="60" rows="10"/></textarea>
</td>
</tr>
<tr> <td><p></p></td></tr> <!emptyline>
<tr>
<td valign="top">
<label for="What was the most enjoyable part about the visit today?">What was the most enjoyable part about the visit today? </label>
</td>
</tr>
<tr>
<td valign="top">
<textarea name="What was the most enjoyable part about the visit today" placeholder="Your answer here" maxlength="1000" cols="60" rows="10"></textarea>
</td>
</tr>
<tr> <td><p></p></td></tr> <!emptyline>
<tr>
<td valign="top">
<label for="Was there anything you did not like?">Was there anything you did not like? </label>
</td>
</tr>
<tr>
<td valign="top">
<input type="radio" name="Was there anything you did not like?" value="yes">Yes (plese give details below)
<input type="radio" name="Was there anything you did not like?" value="no">No
</td>
</tr>
<tr>
<td valign="top">
<textarea name="Was there anything you did not like (comment)?" placeholder="Details here" maxlength="1000" cols="60" rows="10"></textarea>
</td>
</tr>
<tr> <td><p></p></td></tr> <!emptyline>
<tr>
<td valign="top">
<label for="Would you consider studying Engineering at university?">Would you consider studying Engineering at university? </label>
</td>
</tr>
<tr>
<td valign="top">
<input type="radio" name="Would you consider studying Engineering at university?" value="yes">Yes
<input type="radio" name="Would you consider studying Engineering at university?" value="no">No
<input type="radio" name="Would you consider studying Engineering at university?" value="unsure">Unsure
</td>
</tr>
<tr> <td><p></p></td></tr> <!emptyline>
<tr>
<td valign="top">
<label for="Has today's session inspired you to consider becoming an engineer?">Has today's session inspired you to consider becoming an engineer? </label>
</td>
</tr>
<tr>
<td valign="top">
<input type="radio" name="Has today's session inspired you to consider becoming an engineer?" value="yes">Yes
<input type="radio" name="Has today's session inspired you to consider becoming an engineer?" value="no">No
<input type="radio" name="Has today's session inspired you to consider becoming an engineer?" value="unsure">Unsure
</td>
</tr>
<tr> <td><p></p></td></tr> <!emptyline>
<tr>
<td valign="top">
<label for="Before today's session did you know much about engineering?">Before today's session did you know much about engineering? </label>
</td>
</tr>
<tr>
<td valign="top">
<input type="radio" name="Before today's session did you know much about engineering?" value="yes">Yes
<input type="radio" name="Before today's session did you know much about engineering?" value="no">No
<input type="radio" name="Before today's session did you know much about engineering?" value="unsure">A little
</td>
</tr>
<tr> <td><p></p></td></tr> <!emptyline>
<tr>
<td valign="top">
<label for="Has todays session taught you much about engineering?">Has todays session taught you much about engineering? </label>
</td>
</tr>
<tr>
<td valign="top">
<input type="radio" name="Has todays session taught you much about engineering?" value="yes">Yes
<input type="radio" name="Has todays session taught you much about engineering?" value="no">No
<input type="radio" name="Has todays session taught you much about engineering?" value="unsure">A little
</td>
</tr>
<tr> <td><p></p></td></tr> <!emptyline>
<tr>
<td valign="top">
<label for="Based on today's session, what do you think an engineer does?">Based on today's session, what do you think an engineer does? </label>
</td>
</tr>
<tr>
<td valign="top">
<textarea name="Based on today's session, what do you think an engineer does?" placeholder="Your answer here" maxlength="1000" cols="60" rows="10"></textarea>
</td>
</tr>
<tr> <td><p></p></td></tr> <!emptyline>
<tr>
<td valign="top">
<label for="Comments/Suggestions/Questions">Comments/Suggestions/Questions </label>
</td>
</tr>
<tr>
<td valign="top">
<textarea name="Comments/Suggestions/Questions" placeholder="Enter your comments, suggestions and questions here" maxlength="1000" cols="60" rows="10"></textarea>
</td>
</tr>
<tr> <td><p></p></td></tr> <!emptyline>
<tr>
<td colspan="2" style="text-align:center">
<input type="submit" class="submit" value="Submit">
</td>
</tr>
<tr>
<td colspan="2" style="text-align:center">
Thank you for your feedback
</td>
</tr>
<tr>
<td colspan="2" style="text-align:center">
© Girls in Engineering 2015
</td>
</tr>
</table>
</form>
PHP:
$value = test_input($_POST['School']);
$value2 = test_input($_POST['What do you think an engineer does']);
$sql = "INSERT INTO form1 (input1,input2) VALUES ('$value', '$value2')";
if($_POST){
echo '<pre>';
print_r($_POST);
echo '</pre>';
}
Output from print_r($_POST):
Array
(
[School] => 123
[Year] =>
[Gender] => male
[What_do_you_think_an_engineer_does] => 234
[What_was_the_most_enjoyable_part_about_the_visit_today] =>
[Was_there_anything_you_did_not_like_(comment)?] =>
[Based_on_today's_session,_what_do_you_think_an_engineer_does?] =>
[Comments/Suggestions/Questions] =>
)
Thanks for your help.
use name="Comments"
in the textarea. as that is what the $_POST['Comments']
is linked to.
Suppose your code have
<textarea name="What was the most enjoyable part about the visit today" placeholder="Your answer here" maxlength="1000" cols="60" rows="10"></textarea>
your post should be $_POST['What was the most enjoyable part about the visit today']
if you want to pass Comments as an array
<textarea name="Comments[]" placeholder="Your answer here" maxlength="1000" cols="60" rows="10"></textarea>
Hint: Use thed code below for debugging on the page feedback_form_send.php to check what values were sent to it through post on submit;
if($_POST){
echo '<pre>';
print_r($_POST);
echo '</pre>';
}
EDITED
Advice: Your code is vulnerable to Sql injection. use PDO or mysqli instead of mysql
EDITED
my print shows with values
Array
(
[School] => myschool
[Year] => 2018
[Gender] => male
[What_do_you_think_an_engineer_does] => engineer sleeps
[What_was_the_most_enjoyable_part_about_the_visit_today] => it was nice
[Was_there_anything_you_did_not_like?] => yes
[Was_there_anything_you_did_not_like_(comment)?] => too beutiful
[Would_you_consider_studying_Engineering_at_university?] => yes
[Has_today's_session_inspired_you_to_consider_becoming_an_engineer?] => no
[Before_today's_session_did_you_know_much_about_engineering?] => unsure
[Has_todays_session_taught_you_much_about_engineering?] => yes
[Based_on_today's_session,_what_do_you_think_an_engineer_does?] => engineers do hard work
[Comments/Suggestions/Questions] => no comments, i suggest to do things
)
So Your php variables should be assigned something like this. This is just a demonstration to make it understandable. Always do remember to sanitize the input before using
$school=$_POST['School'];
$year=$_POST['Year'];
$gender=$_POST['Gender'];
$whatengineerdoes=$_POST['What_do_you_think_an_engineer_does'];
$enjoyable_part=$_POST['What_was_the_most_enjoyable_part_about_the_visit_today'];
$youdontlike=$_POST['Was_there_anything_you_did_not_like?'];
$didnotlike=$_POST['Was_there_anything_you_did_not_like_(comment)?'];
$wouldyou=$_POST['Would_you_consider_studying_Engineering_at_university?'];
$session_inspired=$_POST["Has_today's_session_inspired_you_to_consider_becoming_an_engineer?"];
$before_session=$_POST["Before_today's_session_did_you_know_much_about_engineering?"];
$has_taught=$_POST['Has_todays_session_taught_you_much_about_engineering?'];
$based_on=$_POST["Based_on_today's_session,_what_do_you_think_an_engineer_does?"];
$comments=$_POST['Comments/Suggestions/Questions'];
EDITED
HERE is example how you can insert the above data into your table
$dbhost = "localhost";
$dbname = "mydatabase";
$dbusername = "root";
$dbpassword = "mypppasss";
$link = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbusername,$dbpassword);
$statement = $link->prepare("INSERT INTO form1(input1,input2,input3,input4,input5,input6,input7,input8,input9,input10,input11,input12,input13)
VALUES(:inp1,:inp2,:inp3,:inp4,:inp5,:inp6,:inp7,:inp8,:inp9,:inp10,:inp11,:inp12,:inp13)");
$statement->execute(array(
inp1=>$school,
inp2=>$year,
inp3=>$gender,
inp4=>$whatengineerdoes,
inp5=>$enjoyable_part,
inp6=>$youdontlike,
inp7=>$didnotlike,
inp8=>$wouldyou,
inp9=>$session_inspired,
inp10=>$before_session,
inp11=>$has_taught,
inp12=>$based_on,
inp13=>$comments
));
这篇关于textarea在MySQL数据库字段PHP中为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!