mysqli VALUE部分没有从html表单中获取输入

时间:2015-03-22 08:01:05

标签: php html sql mysqli

如何将用户输入存储到两个html表单值的Insert VALUES中? 在脚本的最后一行,VALUES不会从表单

中存储这些内容
<?php
error_reporting(0);
require 'connectit.php';
mysqli_query($db, "INSERT INTO bills (bill_name, bill_cost)
VALUES ($_POST['bill_name'], $_POST['bill_cost'])";
?>

// html form
<form action="updatebill.php" method="post">
  <input type="text" name="bill_name" placeholder="Bill name"><br>
  <input type="text" name="bill_cost" placeholder="Bill cost"><br>
  <input type="submit" value="Submit">
</form>

2 个答案:

答案 0 :(得分:0)

$link = mysqli_connect("localhost", "root", "", "demo");

// Check connection
if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}

// Escape user inputs for security
$bill_name = mysqli_real_escape_string($link, $_POST['bill_name']);
$bill_cost = mysqli_real_escape_string($link, $_POST['bill_cost']);


// attempt insert query execution
$sql = "INSERT INTO bills (bill_name,bill_cost) VALUES ('$bill_name', '$bill_cost ')";
if(mysqli_query($link, $sql)){
    echo "Records added successfully.";
} else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}

// close connection
mysqli_close($link);

答案 1 :(得分:0)

您的$_POST值为空,因为您的表单方法未设置为POST,请使用

<form action="updatebill.php" method="post">