简体   繁体   English

无法使用PHP在MySQL中插入所有数据

[英]Can't insert all data in MySQL using PHP

The title is not disclosing the whole thing for sure. 标题并没有公开透露整个事情。 Right now I'm working on a Lab Software. 现在我正在研究实验室软件。 Here, all the individual test names & their details are in a table (say name 'test_info') & all the bills generated for those tests are in another table (say name 'bill_info') & when it's time to insert the test reports I'm storing it in another table (say name 'rpt_info'). 在这里,所有单独的测试名称及其详细信息都在一个表格中(比如名称'test_info'),为这些测试生成的所有账单都在另一个表中(比如名称'bill_info')&何时插入测试报告我将它存储在另一个表中(比如名称'rpt_info')。

There are a few group tests which are a collection of tests. 有一些小组测试是一组测试。 I made a column named 'group' if the value is null then they are under no group test & when their is a value (which is name of a test) means this test is under that group test. 我创建了一个名为'group'的列,如果该值为null,则它们不进行组测试,当它们是一个值(它是测试的名称)时,表示该测试在该组测试下。

Now, take a look at what forced me to ask a question here. 现在,看看是什么迫使我在这里问一个问题。 When its time to enter the value of test report, I just call them like- 当它输入测试报告的价值时,我只是称他们为 -

           <?php
          $gtsql = "SELECT * FROM `bill_info` where `test_id` = '" . $pid."' and `group`='y'";
        $reslt = mysql_query($gtsql);
        while ($data = mysql_fetch_assoc($reslt))
        {
            $test_inf=  "SELECT * FROM `test_info` where `group` = '". $data["pr_name"]."'";
            $test_qr=  mysql_query($test_inf);
             $k = 1;
             while ($test = mysql_fetch_assoc($test_qr)) {

            echo '<tr><td width="80"></td><td align="left"> <input name="name" id="name" style="border:none; background: transparent;" value="' .  $test["name"] . '" readonly/></td><td width="144" align="left"> <INPUT name="result" id="result" /></td><td width="125" align="left"><input name="unit" id="unit" style="border:none; background: transparent;" value="' . $test["unit"] . '" readonly/></td><td align="left">'; 
$ref= $test["ref_range"];
$ref2= $test["ref_range2"];
$ref3= $test["ref_range3"];
if($ref=!"")
{ echo '<br/><input name="ref_range" id="ref_range" style="border:none; background: transparent;" value="'.$test["ref_range"].'" readonly/>';} 
if ($ref2=!"")
{ echo '<br/><input name="ref_range2" id="ref_range2" style="border:none; background: transparent;" value="'.$test["ref_range2"].'" readonly/>';} else { echo "";}
if ($ref3=!"")
{ echo '<br/><input name="ref_range3" id="ref_range3" style="border:none; background: transparent;" value="'.$test["ref_range3"].'" readonly/>';} else { echo "";}; 

'
</td></tr>
';
            $k++;
             }
    }


         ?>

This is working fine & showing all the tests & their info perfectly on the screen to the user. 这工作正常,并在屏幕上向用户显示所有测试及其信息。 But when I try to insert these data in 'rpt_info' table then the problem appears. 但是当我尝试在'rpt_info'表中插入这些数据时,就会出现问题。 I've tried 'while', 'for' but can't insert the value to the table. 我试过'while','for'但是不能将值插入表中。 Here is the insert code that I am running right now. 这是我现在正在运行的插入代码。 One more thing to share to you friends, to get the perfect number of tests I made a row in another table (name 'sell') where I put individual test (bill) records. 还有一件事可以分享给你的朋友,为了得到完美的测试数量,我在另一张桌子(名称'sell')中排成一行,在那里我放了个别测试(账单)记录。 the row named 'num'. 名为'num'的行。 I calculate the 'num' value to find out how many times I want to run the code. 我计算'num'值来找出我想运行代码的次数。 the $num comes perfect when I do echo that one. 当我回应那个时, $num是完美的。 but every time I got only one row being inserted. 但每次我只插入一行。

 <?php
  if(isset($_POST['posted']))
  {

$prsql = "SELECT SUM(num) FROM `sell` where `test_id`='".$_POST['test_id']."'";
$prrs = mysql_query($prsql);
$datas = mysql_fetch_assoc($prrs);
$num= $datas['SUM(num)'];
$i=1;
while ($i < $num)
  {
$str_tst = "INSERT INTO `rpt_info` (`pt_id`,`test_id`,`result`,`name`,`unit`,`ref_range`, `ref_range2`, `ref_range3`, `rcv_date`, `dlv_date`, `age`, `test_cat`)VALUES ( '" . $_POST['pt_id'] . "', '" . $_POST['test_id'] . "', '" . $_POST['result'] . "', '" . $_POST['name'] . "', '" . $_POST['unit'] . "', '" . $_POST['ref_range'] . "', '" . $_POST['ref_range2'] . "', '" . $_POST['ref_range3'] . "', '" . $_POST['rcv_date']. "', '" . $_POST['dlv_date'] . "', '" . $_POST['pt_age'] . "', '" .  $_POST['test_cat']. "')";

 $i++;

  }
?>

<?php
   if(mysql_query($str_tst))
     {
        echo '................';
}
?>

Is there anyone who can help me with this? 有没有人可以帮我这个?

This is my first question & may be I can't clear this to all of you. 这是我的第一个问题,可能是我无法向大家清楚这一点。 If you want to be more clear about the problem then feel free please to ask me questions. 如果你想更清楚这个问题,请随意问我问题。

1) Your $num= $datas['SUM(num)']; 1)你的$num= $datas['SUM(num)']; doesn't exist. 不存在。 Try 尝试

$prsql = "SELECT SUM(num) as SumNum FROM `sell` where `test_id`='".$_POST['test_id']."'";

And you can get it using $num= $datas['SumNum']; 你可以使用$num= $datas['SumNum'];获得它$num= $datas['SumNum'];

2) $str_tst is a simple string, you don't tell it to execute the query. 2) $str_tst是一个简单的字符串,你不要告诉它执行查询。 You execute it using mysql_query($str_tst) or die mysql_error(); 你使用mysql_query($str_tst) or die mysql_error();执行它或者使用mysql_query($str_tst) or die mysql_error(); . Use 采用

while ($i < $num)
  {
$str_tst = "INSERT INTO `rpt_info` (`pt_id`,`test_id`,`result`,`name`,`unit`,`ref_range`, `ref_range2`, `ref_range3`, `rcv_date`, `dlv_date`, `age`, `test_cat`)VALUES ( '" . $_POST['pt_id'] . "', '" . $_POST['test_id'] . "', '" . $_POST['result'] . "', '" . $_POST['name'] . "', '" . $_POST['unit'] . "', '" . $_POST['ref_range'] . "', '" . $_POST['ref_range2'] . "', '" . $_POST['ref_range3'] . "', '" . $_POST['rcv_date']. "', '" . $_POST['dlv_date'] . "', '" . $_POST['pt_age'] . "', '" .  $_POST['test_cat']. "')";
mysql_query($str_tst) or die mysql_error(); // Each loop execute the query
 $i++;

  }

3) Your while loop isn't correct. 3)你的while循环不正确。 I think you're missing one insert. 我想你错过了一个插页。 You begin to 1 and you go, for example, to 10, so you are inserting 9 rows BECAUSE you're using < instead of <= 你开始1,然后你去10,所以你插入9行,因为你正在使用<而不是<=

Advice : Stop using mysql_ . 建议:停止使用mysql_ Please change to mysqli_ or PDO 请更改为mysqli_或PDO

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

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