Php / mysql:数据没有插入表中

时间:2018-04-07 23:04:41

标签: php mysql sql

所以我试图创建这个表单但是,我无法将数据输入到表中。每当我点击继续按钮时,都没有真正发生。我没有得到任何错误或错误。当我检查我的数据库时,没有添加值。 Idk它是如何发生的。我花了好几个小时试图了解问题是什么,但我无法识别它,它已经到了我感到绝望的地步,并决定发布这个问题所以请帮助我。这是我的代码:

<?php

$conn = new mysqli("localhost", "root", "", "KFC");
if(isset($_POST['Submit_btn']))
{
    session_start();
    $RID= $_POST['R_ID'];
    $Name= $_POST['Name'];
    $S_Description=($_POST['S_Description']);
    $L_Description=($_POST['L_Description']);
    $Nutritional_value=($_POST['Nutritional_value']);
    $Cost=($_POST['Cost']);
    $TypeOfMeal=($_POST['TypeOfMeal']);
    $SubmittedBy=($_POST['SubmittedBy']);
    $sql= "INSERT INTO `recipe`(`R_ID`, `Name`, `S_Description`, `L_Description`, `Nutritional_value`, `Cost`, `TypeOfMeal`, `SubmittedBy`) VALUES ($RID,$Name,$S_Description,$L_Description,$Nutritional_value,$Cost,$TypeOfMeal,$SubmittedBy)";
    mysqli_query($conn,$sql);

}
mysqli_close($conn);
?>
<html>
<body>
<div id="bodyContent">
<h1> Registration </h1>
</div>
<form method="post">
<table>
<tr>
<td>R_ID: </td>
<td>
<input type="text" name="R_ID" class="textInput">
</td>
</tr>

<tr>
<td>Name: </td>
<td>
<input type="text" name="Name" class="textInput">
</td>
</tr>

<tr>
<td>Small Description: </td>
<td>
<input type="text" name="S_Description" class="textInput">
</td>
</tr>

<tr>
<td>Long Description: </td>
<td>
<input type="text" name="L_Description" class="textInput">
</td>
</tr>

<tr>
<td>Nutritional value: </td>
<td>
<input type="text" name="Nutritional_value" class="textInput">
</td>
</tr>

<tr>
<td>Cost: </td>
<td>
<input type="number" name="Cost" class="textInput">
</td>
</tr>

<tr>
<td>Type Of Meal: </td>
<td>
<input type="text" name="TypeOfMeal" class="textInput">
</td>
</tr>

<tr>
<td>UserID: </td>
<td>
<input type="Number" name="SubmittedBy" class="textInput">
</td>
</tr>

<tr>
<td> </td>
<td>
<input type="submit" name="Submit_btn" value="Continue">
</td>
</tr>

</table>
</form>
</body>
</html>

3 个答案:

答案 0 :(得分:0)

好的一些事情:

$SubmittedBy=($_POST['SubmittedBy']);

你不需要$ _POST周围的括号。但是,如果你要使用它中的数据,特别是在SQL中,但不仅要清理输入。以下是mysqli_real_escape_string的示例,因此它看起来像这样:

$SubmittedBy=mysqli_real_escape_string($conn, $_POST['SubmittedBy']);

还有其他方法,如PDO :: quote或使用预处理语句,您可能也想查看这些方法。

接下来你的sql插入没有引号的值:

$sql= "INSERT INTO `recipe`(`R_ID`, `Name`, `S_Description`, `L_Description`, `Nutritional_value`, `Cost`, `TypeOfMeal`, `SubmittedBy`) VALUES ('$RID','$Name','$S_Description','$L_Description','$Nutritional_value','$Cost','$TypeOfMeal','$SubmittedBy')";

编辑:感谢Funk Forty Niner指出丢失的$conn

答案 1 :(得分:-1)

尝试替换

<?php
// This prevents SQL injection threat
$RID = mysqli_real_escape_string($conn, $RID);
$Name = mysqli_real_escape_string($conn, $Name);
$S_Description = mysqli_real_escape_string($conn, $S_Description);
$L_Description = mysqli_real_escape_string($conn, $L_Description);
$Nutritional_value = mysqli_real_escape_string($conn, $Nutritional_value);
$Cost = mysqli_real_escape_string($conn, $Cost);
$TypeOfMeal = mysqli_real_escape_string($conn, $TypeOfMeal);
$SubmittedBy = mysqli_real_escape_string($conn, $SubmittedBy);

$sql= "INSERT INTO `recipe`(`R_ID`, `Name`, `S_Description`, `L_Description`, `Nutritional_value`, `Cost`, `TypeOfMeal`, `SubmittedBy`) VALUES ('$RID','$Name','$S_Description','$L_Description','$Nutritional_value','$Cost','$TypeOfMeal','$SubmittedBy')";

但更重要的是,在您的DEV环境中激活错误显示,但在您托管您的网站时将其删除(PROD环境):

<?php
ini_set('display_errors', '1');
error_reporting(E_ALL);

答案 2 :(得分:-4)

我不确定但输入类型编号是$ _POST中的字符串库存,尝试使用intval()转换它们