我想在php mysql中添加两个文本框的数值

时间:2017-04-26 06:03:44

标签: php mysql

我想添加两个文本框的值并存储两个文本框值,使用php作为下面的代码汇总到mysql数据库表中

        <?php
    //Include Database
    include 'db.php';
    $data = new Databases;
    $msg = '';
    if(isset($_POST["submit"]))
    {
     $insert_data = array(
     'first' => mysqli_real_escape_string($data->con, $_POST['first']),
     'Second' => mysqli_real_escape_string($data->con, $_POST['Second']),
     );
     if($data->insert('sum', $insert_data))
     {
     $msg = "Inserted Data Successfully!";
     }
    }
    ?>

    <?php
    if(isset($msg))
    {
     echo $msg;
    }
    ?>

4 个答案:

答案 0 :(得分:2)

尝试这一个而不是数组。

  $sum = $_POST['first'] + $_POST['Second'];
  if($data->insert('sum',$sum)){
   $msg = "Inserted Data Successfully!";
  }else{
   $msg = "Inserted Data Successfully!";
  }

答案 1 :(得分:0)

以下示例

 if(isset($_POST["submit"]))
    {
        $first => mysqli_real_escape_string($data->con, $_POST['first']);
        $second => mysqli_real_escape_string($data->con, $_POST['Second']);
        $insert_data = $first + $second ; // sum of two number
        if($data->insert('sum', $insert_data))
        {
            $msg = "Inserted Data Successfully!";
        }
    }

答案 2 :(得分:0)

<?php
//Include Database
include 'db.php';
$data = new Databases;
$msg = '';

if(isset($_POST["submit"])) {

    $first = $_POST['first'];
    $Second = $_POST['Second'];

    $total =  $first+$Second;

    $insert_data = array('total' => mysqli_real_escape_string($data->con, $total));

    if($data->insert('sum', $insert_data)) {
        $msg = "Inserted Data Successfully!";
    }

}

if(isset($msg)) {
    echo $msg;
}

?>

答案 3 :(得分:0)

可以尝试: 假设你在mysql表中有三列viz。 'first''second''sum'

<?php
//Include Database
include 'db.php';
$data = new Databases;
$msg = '';
if(isset($_POST["submit"]))
    {
     $first =mysqli_real_escape_string($data->con, $_POST['first']);
     $second = mysqli_real_escape_string($data->con, $_POST['Second']);
     $sum = $first+$second;

    $insert_data = array(
       'first' => $first,
       'Second' => $second,
       'sum' => $sum
     );
    $inserted = $data->insert('sum', $insert_data);
    if ($inserted)
     {
      $msg = "Inserted Data Successfully!";
     }
}
?>

<?php
if(isset($msg))
{
 echo $msg;
}
?>