PHP错误 - 将用户表单插入数据库

时间:2014-03-23 23:56:12

标签: php mysql

当我尝试通过PHP命令将用户生成的数据提交到数据库时,我收到一个奇怪的错误。当我点击下面的提交按钮时,而不是运行其功能的PHP页面,我在浏览器上显示原始代码。我的HTML页面底部有一个命令,如下所示:

<form action="insert.php" method="post">
<input type="submit">
</form>

因此,当用户点击提交按钮时,调用PHP文件insert.php(详见下文)将答案输入数据库,将每个答案分成自己的字段。

以下是我正在使用的代码:

<?php
$con=mysqli_connect("host","username","password","database");
// Check connection
if (mysqli_connect())
  {
  echo "Failed to connect to MySQL: " . mysqli_errno();
  }

$sql="INSERT INTO Persons (Name, Serif, Width, Height, Spacing, Weight)
VALUES
('$_POST[answer]','$_POST[answer]','$_POST[answer]','$_POST[answer]','$_POST[answer]','$_POST[answer]')";

if (!mysqli_query($con,$sql))
  {
  die('Error: ' . mysqli_error($con));
  }
echo "1 record added";

mysqli_close($con);
?>

现在,问题是a而不是a(在这种情况下是否存在功能差异?)。他们看起来像:

<form class="testAns" id="widthAns">
<input type="radio" name="answer" value="skinny">-25%
<input type="radio" name="answer" value="skinny">-10%
<input type="radio" name="answer" value="mid">normal
<input type="radio" name="answer" value="fat">+10%
<input type="radio" name="answer" value="fat">+25%
</form>

<form class="testAns" id="spaceAns">
<input type="radio" name="answer" value="small">-25%
<input type="radio" name="answer" value="small">-10%
<input type="radio" name="answer" value="mid">normal
<input type="radio" name="answer" value="wide">+10%
<input type="radio" name="answer" value="wide">+25%
</form>

<form class="testAns" id="weightAns">
<input type="radio" name="wanswer" value="light">-25%
<input type="radio" name="answer" value="light">-10%
<input type="radio" name="answer" value="mid">normal
<input type="radio" name="answer" value="heavy">+10%
<input type="radio" name="answer" value="heavy">+25%
</form>


<form method="post" action="insert.php" class="testAns" id="heightAns">
<input type="radio" name="answer" value="short">-25%
<input type="radio" name="answer" value="short">-10%
<input type="radio" name="answer" value="mid">normal
<input type="radio" name="answer" value="tall">+10%
<input type="radio" name="answer" value="tall">+25%
</form>

重要的部分是与每个按钮相关联的“值”登录到数据库。例如,如果用户选择“+ 10%”,我希望能够记录单词“heavy”。然后有两个文本输入字段:

<form id="intro">
City: <input type="text" name="answer"><br>
Why you are using this tool:<input type="text" name="answer">
</form>

因此,对于这些文本字段,我需要将用户输入记录为答案。

2 个答案:

答案 0 :(得分:0)

我看到你修复了PHP的东西。

现在您需要在表单中填入数据。这个:

<form action="insert.php" method="post">
    <input type="submit">
</form>

仅发送提交值。您需要在该表单标记内添加输入字段,否则将不会发送任何其他内容。因此,既然您要发送一个答案数组,那么您应该添加它们(将它们添加为文本字段,作为示例):

<form action="insert.php" method="post">
    <input type="text" name="answer[]" />
    <input type="text" name="answer[]" />
    etc...
    <input type="submit" />
</form>

并确保在将任何内容写入数据库之前过滤所有用户输入,否则我的好友Bobby Tables可能会来访问您。

答案 1 :(得分:0)

确保 XAMPP控制面板 Apache MySQL 正在运行。然后检查输入字段是否在<form action='insert.php' method='POST'> input fields </form>

您的HTML代码如下所示:

<html>
<body>

<form action='insert.php' method='POST'>

<table>
<tr><td>Width: </td><td>
<input type="radio" name="width" value="skinny">-25%
<input type="radio" name="width" value="skinny">-10%
<input type="radio" name="width" value="mid">normal
<input type="radio" name="width" value="fat">+10%
<input type="radio" name="width" value="fat">+25%
</td></tr>

<tr><td>Spacing: </td><td>    
<input type="radio" name="spacing" value="small">-25%
<input type="radio" name="spacing" value="small">-10%
<input type="radio" name="spacing" value="mid">normal
<input type="radio" name="spacing" value="wide">+10%
<input type="radio" name="spacing" value="wide">+25%
</td></tr>

<tr><td>Weight: </td><td>
<input type="radio" name="weight" value="light">-25%
<input type="radio" name="weight" value="light">-10%
<input type="radio" name="weight" value="mid">normal
<input type="radio" name="weight" value="heavy">+10%
<input type="radio" name="weight" value="heavy">+25%
</td></tr>

<tr><td>Height: </td><td>    
<input type="radio" name="height" value="short">-25%
<input type="radio" name="height" value="short">-10%
<input type="radio" name="height" value="mid">normal
<input type="radio" name="height" value="tall">+10%
<input type="radio" name="height" value="tall">+25%
</td></tr>

<tr><td>City: </td><td><input type="text" name="city"></td></tr>
<tr><td>Why you are using this tool: </td><td><input type="text" name="tool"></td></tr>
<tr><td></td><td><input type='submit'></td></tr>

</table>

</form>

</body>
</html>

你在创建php文件时使用了什么? DREAMWEAVER?记事本?试试这个:保存你的文件,另存为类型:所有文件并将其命名为 insert.php

<?php
$con=mysqli_connect("localhost","YourUsername","YourPassword(if any)","NameOfYourDatabase");
// Check connection
if (mysqli_connect())
  {
  echo "Failed to connect to MySQL: " . mysqli_errno();
  }

$width=$_POST['width'];
$spacing=$_POST['spacing'];
$weight=$_POST['weight'];
$height=$_POST['height'];
$city=mysqli_real_escape_string($con,$_POST['city']);
$tool=mysqli_real_escape_string($con,$_POST['tool']);

/* REAL ESCAPE STRING WOULD PREVENT A BIT OF SQL INJECTION */

$sql="INSERT INTO Persons (Name, Serif, Width, Height, Spacing, Weight)
VALUES
('$city','$tool','$width','$height','$spacing','$weight')";

if (!mysqli_query($con,$sql))
  {
  die('Error: ' . mysqli_error($con));
  }
echo "1 record added";

mysqli_close($con);
?>