Edit multiple MySQL rows using PHP where primary key is same for all rows
I am referencing the URL http://www.shotdev.com/php/php-mysql/php-mysql-edit-update-data/ to edit rows from a MySQL table. The page displays fine with the edit button besides each row. However, upon clicking any of the edit button on any of the rows, it shows input/edit for the last row.
The reason why this would be occurring maybe because we have primary key(auto-incremented) as "abc-2010-2011-1" (abc-2010-2011-2, abc-2010-2011-3) & not id. Secondly, there are multiple rows referencing the same primary key such as abc-2010-2011-1. So when we try editing the first row from the table, it tries editing/displays input for the last row of the array.
<?
$objConnect = mysql_connect("localhost","app","abc123") or die(mysql_error());
$objDB = mysql_select_db("quotation");
$serial=$objResult['serial_no'];
$parti=$objResult['particulars'];
$qnt=$objResult['quantity'];
$untp=$objResult['unit_price'];
$i=1;
foreach(serial_no as $val) {
$strSQL = "SELECT * FROM table1 WHERE complete_qtn_no = '".$_GET["CusID[]"]. "' and serial_no=$val[$i] ; ";
$objQuery = mysql_query($strSQL);
$objResult = mysql_fetch_array($objQuery);
$i++;
}
if(!$objResult)
{
echo "Quotation number not found =".$_GET["CusID"];
}
else
{
?>
Below is entries from table1,
+----+------------------------+------------+------------------------------------+-----------+-------------+----------+------------+---------+----------+-----------+-------------------+------------+------------+---------+---------+-----------+
| id | to_addr | app_date | subject_quote | serial_no | particulars | quantity | unit_price | tamount | delivery | payment | complete_qtn_no | tax_value1 | tax_value2 | tax1 | tax2 | net_total |
+----+------------------------+------------+------------------------------------+-----------+-------------+----------+------------+---------+----------+-----------+-------------------+------------+------------+---------+---------+-----------+
| 79 | test | 01/06/11 | Quotation as per your requirements | 2 | TESTING2 | 78 | 98.56 | 7687.68 | 2 Days | Immediate | ABC-QTN-2010-11-20 | 16 | 0 | 1230.03 | 0 | 8917.71 |
| 78 | test1 | 01/06/11 | Quotation as per your requirements | 1 | TESTING | 16 | 78.89 | 1262.24 | 2 Days | Immediate | ABC-QTN-2010-11-20 | 3 | 16 | 37.8672 | 201.958 | 1502.07 |
| 77 | test3| | Quotation as per your requirements | 3 | Cable | 68 | 89.76 | 6103.68 | 2 Days | Immediate | ABC-QTN-2010-11-19 | 3 | 12.5 | 183.11 | 762.96 | 7049.75 |
| 76 | test4| | Quotation as per your requirements | 2 | Motor | 16 | 78.8 | 1260.8 | 2 Days | Immediate | ABC-QTN-2010-11-19 | 16 | 0 | 201.728 | 0 | 1462.53 |
| 75 | test5 | | Quotation as per your requirements | 1 | Pipe | 10 | 15 | 150 | 2 Days | Immediate | abc-QTN-2010-11-19 | 3 | 16 | 4.5 | 24 | 178.5 |
+----+------开发者_运维知识库------------------+------------+------------------------------------+-----------+-------------+----------+------------+---------+----------+-----------+-------------------+------------+------------+---------+---------+-----------+
Thank you for your help in advance.
Change your query to:
$strSQL = "SELECT * FROM table1 WHERE id = ".$_GET["id"];
精彩评论