【问题标题】:Textarea is empty in mySQL database field PHPmySQL数据库字段PHP中的Textarea为空
【发布时间】:2015-12-04 21:56:01
【问题描述】:

我创建了一个 HTML 表单并尝试将其字段中的数据保存到数据库中。输入、下拉菜单和单选按钮已正确存储,但文本区域始终作为空字段添加到数据库中。代码如下:

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)
    &nbsp;
    <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
    &nbsp;
    <input type="radio" name="Would you consider studying Engineering at university?" value="no">No
    &nbsp;
    <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
    &nbsp;
    <input type="radio" name="Has today's session inspired you to consider becoming an engineer?" value="no">No
    &nbsp;
    <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
    &nbsp;
    <input type="radio" name="Before today's session did you know much about engineering?" value="no">No
    &nbsp;
    <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
    &nbsp;
    <input type="radio" name="Has todays session taught you much about engineering?" value="no">No
    &nbsp;
    <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>';
}

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] => 
)

感谢您的帮助。

【问题讨论】:

  • 您确定test_input() 功能正常工作吗?尝试调试它的返回值。 var_dump($data)print_r($data)
  • 你不能这样做:&lt;textarea type="text",试试&lt;textarea
  • @Chris test_input 函数适用于其他类型的字段,但对于 textarea 它不打印任何内容。
  • @Rasclatt 我删除了 type="text",还是不行
  • 帖子的原始打印中也有什么显示吗?:print_r($_POST);

标签: php html mysql


【解决方案1】:

在文本区域中使用name="Comments"。因为这就是$_POST['Comments'] 所链接的内容。

假设你的代码有

<textarea name="What was the most enjoyable part about the visit today" placeholder="Your answer here" maxlength="1000" cols="60" rows="10"></textarea>

你的帖子应该是$_POST['What was the most enjoyable part about the visit today']

如果你想将评论作为数组传递

<textarea name="Comments[]" placeholder="Your answer here" maxlength="1000" cols="60" rows="10"></textarea>

提示:使用下面的代码在页面 feedback_form_send.php 上进行调试,以检查通过 post on submit 发送给它的值;

if($_POST){
echo '<pre>';
print_r($_POST);
echo '</pre>';
}

已编辑

建议:您的代码容易受到 Sql 注入的影响。使用 PDO 或 mysqli 代替 mysql

已编辑

我的打印显示带有值

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
)

所以你的 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
));

【讨论】:

  • 调试功能打印我在表单中输入的所有值。但是只有前三个(直到性别)存储在数据库中。
  • 您能否将通过 print_r 和您的插入查询获得的内容添加到问题中:)
  • @user2758935 已编辑答案。看看能不能解决:)
  • wc @user2758935 看到我再次编辑向您介绍 PDO。这是查询数据库的安全方法。祝你好运:)
【解决方案2】:
$comment = test_input($_POST['Comments']);
$sql = "INSERT INTO form1 (Comment) VALUES ('$comment')";
$res = mysql_query($sql);

【讨论】:

  • 请考虑编辑您的帖子,以添加更多关于您的代码的作用以及它为何能解决问题的说明。大部分只包含代码的答案(即使它正在工作)通常不会帮助 OP 理解他们的问题。
  • 请不要这样做,因为它会导致 SQL 注入的极端情况!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-18
  • 2020-05-17
  • 2011-09-08
  • 1970-01-01
  • 2012-10-19
  • 1970-01-01
相关资源
最近更新 更多