Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to update or insert data into a table using a loop structure
    primarykey
    data
    text
    <p>Please assist me on this one. I have two DB's, one keeps track of user and system activities and the other keeps track of only user activities. The purpose of the codes below is to first of all retreive all tables from the second DB. These tables have the following columns:</p> <p>(id |date |debit |credit |number |description |accounttype |company_id)</p> <p>The idea is to retrieve all tables and only their |debit|credit| columns. Already these fields (|debit|credit|) have some values in them. After retrieval, I should be able to update or if possible do a new insertion to any or all the tables that have been retrieved from the DB.</p> <p>Below are the codes I've written. It retrieves alright but can't do an insertion or update. Please assist me</p> <p>The same page calls itself.</p> <pre><code>//Connection and Extraction from DB //DB Connection exists //SQL Query $sql = "show tables from $companyname"; $results = mysql_query($sql) or die(mysql_error()); $count = 1; while($row = mysql_fetch_array($results)) { $results2 = mysql_query("SELECT * FROM `$row[0]` LIMIT 1") or die('Error'.mysql_error()); while($row2 = mysql_fetch_array($results2)) { $debit = $row2['debit']; $credit = $row2['credit']; echo "&lt;tr&gt;&lt;td&gt;$count. &lt;/td&gt;&lt;td width='30%'&gt;".ucwords($row[0])."&lt;/td&gt;&lt;td width='30%'&gt; &lt;input type='text' name='debit' value='$debit' align='right'&gt;&lt;/td&gt; &lt;td width='30%'&gt;&lt;input type='text' name='credit' value='$credit' align='right'&gt;&lt;/td&gt;&lt;/tr&gt;"; } $count++; } //Insertion into DB if(isset($_POST['submit'])) { //SQL Query $sql3 = "show tables from $companyname"; $results3 = mysql_query($sql3) or die(mysql_error()); $count = 1; while($row3 = mysql_fetch_array($results3)) { $results4 = mysql_query("SELECT * FROM `$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]' WHERE `id`=`$row4[id]`"); echo "UPDATE `$row3[0]` SET `debit`= '$row4[debit]' , `credit`= '$row4[credit]' WHERE `id`=`$row4[id]` &lt;br&gt;"; echo $num; } $count++; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload