Php 表单和数据库
我试图从提交的值中创建一个数据库值,并插入到同一个数据库中。我正在创建的值是Php 表单和数据库,php,Php,我试图从提交的值中创建一个数据库值,并插入到同一个数据库中。我正在创建的值是$tprice='$Price'*'$Pquantity' 在下面的代码中,但它不是插入 <?php $submit = $_POST['Add']; //form data $Sname = mysql_real_escape_string(htmlentities(strip_tags($_POST['Sname']))); $Pname = mysql_real_escape_string(htmlenti
$tprice='$Price'*'$Pquantity'代码>
在下面的代码中,但它不是插入
<?php
$submit = $_POST['Add'];
//form data
$Sname = mysql_real_escape_string(htmlentities(strip_tags($_POST['Sname'])));
$Pname = mysql_real_escape_string(htmlentities(strip_tags($_POST['Pname'])));
$Pidno = mysql_real_escape_string(htmlentities(strip_tags($_POST['Pidno'])));
$Psize = mysql_real_escape_string(htmlentities(strip_tags($_POST['Psize'])));
$Pcolour = mysql_real_escape_string(htmlentities(strip_tags($_POST['Pcolour'])));
$Pquantity = $_POST['Pquantity'];
$Weblink = mysql_real_escape_string(htmlentities(strip_tags($_POST['Weblink'])));
$Price = mysql_real_escape_string(htmlentities(strip_tags($_POST['Price'])));
$date = date("Y-m-d");
echo " ('','$Sname','$Pname','$Pidno','$Psize','$Pcolour','$Pquantity','$Weblink','$Price','$Uname')";
if('POST' === $_SERVER['REQUEST_METHOD'])
{
if ($Sname&&$Pname&&$Pidno&&$Weblink&&$Price)
{
if (is_numeric($Price))
{
$repeatheck = mysql_query("SELECT * FROM repplac WHERE Uname = '{$_SESSION['username']}' AND Pidno ='$Pidno' AND Sname='$Sname'");
$count = mysql_num_rows($repeatheck);
if($count!=0)
{
die ('PRODUCT ALREADY IN BASKET YOU CAN INCREASE OR DECREASE QUANTITY');
}
else
//echo'$Price';
$tprice = '$Price' * '$Pquantity';
//echo"$tprice";
$queryreg = mysql_query("
INSERT INTO repplac VALUES ('','$Sname','$Pname','$Pidno','$Psize','$Pcolour','$Pquantity','$Weblink','$Price','$tprice','$date','{$_SESSION['username']}')
");
}
else
echo 'price field requires numbers';
}
else
echo 'please fill in all required * fields ';
}
?>
这行:
$tprice = '$Price' * '$Pquantity';
无效:它将尝试将文本字符串$Price
和$Pquantity
相乘(因为在带单引号的字符串中,变量名不会被解释)
只需完全失去引用:
$tprice = $Price * $Pquantity;
补充说明:
- 您应该确保
$Pquantity
是一个整数。否则,人们可以通过指定0.1
- 消毒时,
strip_tags()
和htmlentities()
调用过多。我不会使用这两种方法,并且在输出数据时执行htmlentities()
欢迎使用堆栈溢出!您在查询中没有进行任何错误检查,因此,当查询失败时,您没有获得任何信息也就不足为奇了。您需要在调用mysql\u query()
后检查错误。如何做到这一点,在or中也有概述,而且“它不起作用”从来都不是一个好的错误描述。到底哪里出了问题?您会遇到什么错误?首先,当我回显$tprice的值时,它显示为$tprice,所以我想知道为什么,但它实际上捕获了单个值。您的问题到底是什么?当我插入值时,$tprice值不会插入到$queryreg=mysql\u查询(“插入到repplac值中”)(“%$Sname”、“%$Pname”、“$Pidno”、“$Psize”、“$Pcolour”、“$Pquantity”、“$Weblink”、“$Price”、“$tprice”、“$date”、“{$\u SESSION['username']}”)或死亡(mysql\u error());