PHP~列计数与第1行的值计数不匹配
我试图插入到两个表中,但出现此错误 错误:插入到提供帮助金额值40000.00列计数与第1行的值计数不匹配` 下面是我的插入代码PHP~列计数与第1行的值计数不匹配,php,mysql,Php,Mysql,我试图插入到两个表中,但出现此错误 错误:插入到提供帮助金额值40000.00列计数与第1行的值计数不匹配` 下面是我的插入代码 <?php session_start(); { //Include database connection details include('../../dbconnect.php'); $amount = strip_tags($_POST['cat']); $field1amount = $_POST['cat'];
<?php
session_start(); {
//Include database connection details
include('../../dbconnect.php');
$amount = strip_tags($_POST['cat']);
$field1amount = $_POST['cat'];
$field2amount = $field1amount + ($field1amount*0.5);
$sql = "INSERT INTO provide_help (amount) VALUES ( $field1amount)";
if (mysqli_query($conn, $sql))
$sql = "INSERT INTO gh (ph_id, amount) VALUES (LAST_INSERT_ID(), $field2amount)";
if (mysqli_query($conn, $sql))
{
$_SESSION['ph'] ="<center><div class='alert alert-success' role='alert'>Request Accepted.</div></center>";
header("location: PH.php");
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
}
?>
我只是将$field1amount更改为$field2amount
但是我不想这样,我想得到$field1amount的值并插入它
请提供任何帮助,谢谢。问题是因为您输入的数字中有逗号,而不是字符串。你需要通过40000.00或40000.00。MySQL将其解释为两个值:40和000.00 使用预先准备好的语句将缓解这一问题和您的安全问题,因为绑定将40000.00解释为字符串。让您开始学习的一个非常基本的示例是:
$sql = "INSERT INTO provide_help (amount) VALUES (?)";
$stmt = $mysqli->prepare($sql);
/*
- the "s" below means string
- NOTE you should still validate the $_POST value,
don't just accept whatever is sent through your form -
make sure it matches the format you're expecting at least
or you'll have data validation issues later on
*/
$stmt->bindParam("s", $field1amount);
$stmt->execute($fieldAmount1);
$result = $res->fetch_assoc();
问题是因为您要传递的数字中有逗号,而不是字符串。你需要通过40000.00或40000.00。MySQL将其解释为两个值:40和000.00 使用预先准备好的语句将缓解这一问题和您的安全问题,因为绑定将40000.00解释为字符串。让您开始学习的一个非常基本的示例是:
$sql = "INSERT INTO provide_help (amount) VALUES (?)";
$stmt = $mysqli->prepare($sql);
/*
- the "s" below means string
- NOTE you should still validate the $_POST value,
don't just accept whatever is sent through your form -
make sure it matches the format you're expecting at least
or you'll have data validation issues later on
*/
$stmt->bindParam("s", $field1amount);
$stmt->execute($fieldAmount1);
$result = $res->fetch_assoc();
您对查询非常开放,应该真正使用而不是连接查询。特别是因为你根本没有逃避用户的输入!这也可以解决逗号的问题。您对查询非常开放,应该使用而不是串联查询。特别是因为你根本没有逃避用户的输入!这也会解决你的逗号问题。我的建议实际上是尽可能使用PDO。请参阅连接到数据库并准备语句、绑定参数和执行查询。我的建议实际上是尽可能使用PDO。请参阅连接到数据库以及准备语句、绑定参数和执行查询的步骤。