【发布时间】:2011-11-26 15:48:33
【问题描述】:
我有一张表,该表由来自一个数据库的 MS Sql 查询填充,该数据库为我提供来自患者就诊的值以及这些就诊产生的收入。我有一个复选框,用于填充相应的文本输入框,显示该访问已付费。我的问题是如何使用 MySQL 和 PHP 添加/更新多行。有两种不同的数据库(MySQL 和 MS SQL)。
HTML 表格。
<table>
<thead>
<tr>
<th>First Name / Last Name</th>
<th>Alias</th>
<th>Status</th>
<th class="amountDue">$00,000.00</th>
<th colspan="2" class="appliedAmount">$00,000.00<?th>
<th class="variance">$00,000.00</th>
<th>Complete</th>
</tr>
<tr>
<td><td>
<td><td>
<td><?php echo $patRow['VisitNum']; ?></td>
<td><?php echo $patRow['VisitName']; ?></td>
<td><?php echo $patRow['AmountDue']; ?></td>
<td><input type="checkbox"></td>
<td><input type="text" name="Amount[]"></td>
<!-- Invoice Populated from Database --!>
<td><select class="invoiceNumber">
<option>4565</option>
</select>
</tr>
</tr>
</table>
现在是 PHP。
<?php
require('../assets/dbconnect.php');
$size_array = count($_POST['Amount']);
for ($i=0; $i<$size_array; $i++){
$query = 'INSERT INTO webportal.test (id, PSID, SysPatVisitID, AmountDue, Amount, InvoiceNum)'.
" VALUES ('', '".mysql_real_escape_string($_POST['PSID'][$i])."',
'".mysql_real_escape_string($_POST['SysPatVisitID'][$i])."'',
'".mysql_real_escape_string($_POST['AmountDue'][$i])."'',
'".mysql_real_escape_string($_POST['Amount'][$i])."'',
'".mysql_real_escape_string($_POST['InvoiceNum'][$i])."')
ON DUPLICATE KEY UPDATE content=VALUES(
'".mysql_real_escape_string($_POST['AmountDue'][$i])."'',
'".mysql_real_escape_string($_POST['Amount'][$i])."'',
'".mysql_real_escape_string($_POST['InvoiceNum'][$i])."''
";
$result = mysql_query($query) or die (mysql_error());
}
所以这就是我到目前为止所拥有的,当我尝试插入我的数据库时,它只插入第一条记录,我得到一个错误。最终我想用 Jquery 传递这些变量,但我只需要先让 PHP 工作。
【问题讨论】:
-
@RogerLindsjö - 它返回一个 mysql id 错误,但是当我检查我的数据库时,它只插入第一行信息,所以这让我相信我的 ID 插入语句有问题- 它不是在插入/更新时自动递增。
-
请停止使用过时的
mysql_*函数,学习如何使用PDO和prepared statements。