php - How to update or insert data into a table using a loop structure -


please assist me on one. have 2 db's, 1 keeps track of user , system activities , other keeps track of user activities. purpose of codes below first of retreive tables second db. these tables have following columns:

(id |date |debit |credit |number |description |accounttype |company_id)

the idea retrieve tables , |debit|credit| columns. these fields (|debit|credit|) have values in them. after retrieval, should able update or if possible new insertion or tables have been retrieved db.

below codes i've written. retrieves alright can't insertion or update. please assist me

the same page calls itself.

//connection , extraction db //db connection exists //sql query $sql = "show tables $companyname"; $results = mysql_query($sql) or die(mysql_error()); $count = 1; while($row = mysql_fetch_array($results)) { $results2 = mysql_query("select * `$row[0]` limit 1") or die('error'.mysql_error()); while($row2 = mysql_fetch_array($results2)) { $debit = $row2['debit']; $credit = $row2['credit']; echo "<tr><td>$count. </td><td width='30%'>".ucwords($row[0])."</td><td width='30%'> <input type='text' name='debit' value='$debit' align='right'></td> <td width='30%'><input type='text' name='credit' value='$credit' align='right'></td></tr>"; } $count++; } //insertion db if(isset($_post['submit'])) { //sql query $sql3 = "show tables $companyname"; $results3 = mysql_query($sql3) or die(mysql_error()); $count = 1; while($row3 = mysql_fetch_array($results3)) { $results4 = mysql_query("select * `$row3[0]` limit 1") or die('error '.mysql_error()); $debit = $_post['debit']; $credit = $_post['credit']; while($row4 = mysql_fetch_array($results4)) { $query = mysql_query("update `$row3[0]` set `debit`= '$row4[debit]' , `credit`= '$row4[credit]' `id`=`$row4[id]`"); echo "update `$row3[0]` set `debit`= '$row4[debit]' , `credit`= '$row4[credit]' `id`=`$row4[id]` <br>"; echo $num; } $count++; } } 

  1. your database design bad should have keepd data in 1 schema, keeping seperate auto_increment company using dubbel field primary key

    create table company_transaktions ( company_name varchar(50) not null, id mediumint unsigned not null auto_increment, date date not null, debit float not null, credit float not null, number meniumint not null, description tinytext not null, accounttype tinytext not null, company_id mediumint unsigned not null, primary key (company_name,id) ); 

    or if company_id think is, should use primary key (company_id,id)

  2. the php problem

    you store 2 postdata in 2 variables

    $debit = $_post['debit']; $credit = $_post['credit']; 

    but never use them, going value of last row, have same name.

    so first thing need rename inputs:

    echo "<tr>\n"; echo "<td>$count. </td>\n"; echo "<td width='30%'>".ucwords($row[0])."</td>\n"; echo "<td width='30%'><input type='text' name='{$row[0]}[debit]' value='$debit' align='right'></td>\n"; echo "<td width='30%'><input type='text' name='{$row[0]}[credit]' value='$credit' align='right'>\n"; echo "</td>\n"; echo "</tr>"; 

    then can fetch right 1 on each loop:

    $debit = mysql_real_escape_string($_post[$row3[0]]['debit']); $credit = mysql_real_escape_string($_post[$row3[0]]['credit']); 

    and use them in query

    $result = mysql_query("update `$row3[0]` set `debit`= '$debit' , `credit`= '$credit' `id`=`$row4[id]`"); 

Comments

Popular posts from this blog

javascript - backbone.js Collection.add() doesn't `construct` (`initialize`) an object -

php - Get uncommon values from two or more arrays -

Adding duplicate array rows in Php -