简体   繁体   English

如何计算值并将其保存到数据库中

[英]How to calculated and save the value into database

I'm try to calculated the value but some of it doesn't function. 我正在尝试计算该值,但其中某些功能不起作用。 And it also doesn't insert into database. 而且它也不会插入数据库。 Please somebody help me. 请有人帮我。 I'm doing my final year project right now. 我现在正在做我的最后一年项目。

<?php
error_reporting(E_ALL & ~E_NOTICE);

$no1=$_POST[peruntukan_diluluskan];
$no2=$_POST[jumlah_perbelanjaan];
$tolak6=$_POST[baki];

$tolak6=($no1-$no2);


$query=" INSERT INTO form (baki)
VALUES ('$tolak6')"; 
$result=mysql_query($query);

 ?>

<table width="800" border="0" align="center">
          <tr>
            <td width="30"><strong>1</strong></td>
            <td width="170"><span class="gh"><strong>Peruntukan diluluskan (Approved budget)</strong></span><strong> </strong></td>
            <td width="10">:</td>
            <td width="472"><?php echo $record["peruntukan_diluluskan"]; ?><input name="peruntukan_diluluskan" type="hidden" id="peruntukan_diluluskan" readonly ="readonly" value="<?php echo $record["peruntukan_diluluskan"]; ?>" size="50" /></td>
          </tr>
          <tr>
            <td width="30"><strong>2</strong></td>
            <td width="170"><span class="hb"><span class="hb"><strong>Jumlah Perbelanjaan (Amount Spent) </strong></span></span><strong></strong></td>
            <td width="10">:</td>
            <td width="472"><?php echo $record["jumlah_perbelanjaan"]; ?><input name="jumlah_perbelanjaan"type="hidden" id="jumlah_perbelanjaan" readonly ="readonly"  value="<?php echo $record["jumlah_perbelanjaan"]; ?>" size="30" /></td>
          </tr>
           <tr>
            <td width="30"><strong>3</strong></td>
            <td width="170"><span class="hb"><span class="hb"><strong>Baki (Balance) </strong></span></span><strong></strong></td>
            <td width="10">:</td>
            <td width="472"><?php  echo $tolak6;
 ?><input type="hidden" name="baki" id="baki" value="<?php  echo $tolak6;
 ?>" size="50" /></td>
          </tr>

Your problem looks like you're not connected to a database. 您的问题似乎没有连接到数据库。

You need to use some MySQL connections to establish which server the DB lives on, and which DB you want to connect to. 您需要使用一些MySQL连接来建立该数据库所在的服务器,以及要连接到哪个数据库。

$record is never populated, either. 也不会填充$record

You'll probably want to throw the $_POST variables inside quotes, too: 您可能还希望将$ _POST变量也放在引号内:

$no1=$_POST['peruntukan_diluluskan'];
$no2=$_POST['jumlah_perbelanjaan'];
$tolak6=$_POST['baki'];  //this is immediately overwritten.. 
$tolak6=($no1-$no2);

PS You should do a better job of paramaterizing your queries. PS:您应该更好地将您的查询参数化。 (: (:

编写正确的MySql插入格式

INSERT INTO table_name ( field1, field2,...fieldN ) VALUES ( value1, value2,...valueN ); 

missing db connection 缺少数据库连接

add like this 像这样添加

<?php
$servername = "localhost";
$username = "root";
$password = "";
$mysql_database = "testdb"; //your database name



$conn = mysql_connect($servername, $username, $password, $mysql_database);

 // $conn  is db connection variable 

if (!$conn) 
{

    die("Connection failed");
    }
     ?> 

and you have to use POST value like 并且您必须使用POST值,例如

     $no1=$_POST['peruntukan_diluluskan'];
      $no2=$_POST['jumlah_perbelanjaan'];
      $tolak6=$_POST['baki']; //here you have to use different variable 

     $tolak6=($no1-$no2); //here you overwriting the same variable 

and missing connection variable 和缺少连接变量

     $query=" INSERT INTO form (baki)
    VALUES ('$tolak6')"; 
    $result=mysql_query($conn,$query); 

Use Below statement. 使用下面的语句。

$con=mysql_connect("hostname", "username", "password") or die(mysq_error());
mysql_select_db("database_name", $con);
$query=" INSERT INTO form (baki)
VALUES ('$tolak6')"; 
$result=mysql_query($query);

Now a days php support mysqli driver. 现在已经有php支持mysqli驱动了。

I suggest u to first check the form element. 我建议你先检查表单元素。 Use post method inside form <form method="post" action="your php file name"> Then create a connection with your local host and use that connection variable to make transaction between database. 在表单<form method="post" action="your php file name">使用post方法,然后与本地主机创建连接,并使用该连接变量在数据库之间进行事务。 $con = mysqli_connect("localhost","usename","password","databasename");

Then create query for insert and make transaction 然后创建查询以进行插入并进行交易

`$q = "insert into tablename(field1,field2,...,fieldn) values (val1,val2,...valn)";

Mysqli_query($q);` Mysqli_query($ q);`

Try it. 试试吧。 Happy coding... 祝您编程愉快...

<?php
error_reporting(E_ALL & ~E_NOTICE);

$no1=$_POST['peruntukan_diluluskan'];
$no2=$_POST['jumlah_perbelanjaan'];
$tolak6=$_POST['baki'];
$tolak6=($no1-$no2);

$con= mysql_connect("localhost","root","") or die( " Error"."Connection failed: ".mysql_error());
mysql_select_db("testdb") or die( " Error"."DB selection failed: ".mysql_error());
$query="INSERT INTO form (baki) VALUES ('$tolak6')";
$result=mysql_query($con,$query) or die(mysql_errno($con,$query));

?>

<table width="800" border="0" align="center">
    <tr>
        <td width="30"><strong>1</strong></td>
        <td width="170"><span class="gh"><strong>Peruntukan diluluskan (Approved budget)</strong></span><strong> </strong></td>
        <td width="10">:</td>
        <td width="472"><?php echo $record["peruntukan_diluluskan"]; ?><input name="peruntukan_diluluskan" type="hidden" id="peruntukan_diluluskan" readonly ="readonly" value="<?php echo $record["peruntukan_diluluskan"]; ?>" size="50" /></td>
    </tr>
    <tr>
        <td width="30"><strong>2</strong></td>
        <td width="170"><span class="hb"><span class="hb"><strong>Jumlah Perbelanjaan (Amount Spent) </strong></span></span><strong></strong></td>
        <td width="10">:</td>
        <td width="472"><?php echo $record["jumlah_perbelanjaan"]; ?><input name="jumlah_perbelanjaan"type="hidden" id="jumlah_perbelanjaan" readonly ="readonly"  value="<?php echo $record["jumlah_perbelanjaan"]; ?>" size="30" /></td>
    </tr>
    <tr>
        <td width="30"><strong>3</strong></td>
        <td width="170"><span class="hb"><span class="hb"><strong>Baki (Balance) </strong></span></span><strong></strong></td>
        <td width="10">:</td>
        <td width="472"><?php  echo $tolak6;
            ?><input type="hidden" name="baki" id="baki" value="<?php  echo $tolak6;
            ?>" size="50" /></td>
    </tr>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM