自动填充根据另一个数据库字段的数据填充数据库字段50%

时间:2017-01-27 14:37:07

标签: php mysql

我希望将字段更新为其他字段在数据库中的50% 表

field1(40,000)从提交的表单中获取值

field2(#)所以这个field2将更新为field1的50%,在这种情况下为6万

请任何可以帮助mysql的机构Query.below是插入第一个字段的代码

<?php
session_start(); {

//Include database connection details
include('../../dbconnect.php');

$amount =  strip_tags($_POST['someid']);

//$days = str_replace('/', '-',$_POST['endtime']);

if ($amount == "" ) {
    $_SESSION['ph'] = "<center><div class='alert alert-danger' role='alert'>Please Fill The Fields</div></center>";
    header("location: PH.php");
    exit;
}

$sql = "INSERT INTO transactions (amount) VALUES ('$amount')";

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);
}
?>

1 个答案:

答案 0 :(得分:1)

$field1amount = $_POST['formid'];
$field2amount = $field1amount + ($field1amount*0.5);

$sql = "INSERT INTO transactions (Field1,Field2) VALUES ('$field1amount','$field2amount');

代码也容易受到sql注入,不要使用mysql_或mysqli_使用pdo和预编译语句。