473,324 Members | 2,193 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

send one table values to another table with new fields how?

How do I send one table values to another table with new fields?
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. include("menu.php"); 
  3. include("db.php"); 
  4.  //$id=$_REQUEST['id'];
  5.   $min=$_REQUEST['min'];
  6.  $exam=$_REQUEST['exam'];
  7.  $class=$_REQUEST['class'];    
  8.  ?>
  9. </head>
  10. <body> 
  11. <table>
  12.   <tr>
  13.     <td>
  14.  
  15.  <?php 
  16. //echo  $id=$_POST['id']; echo "<br>";
  17.   $min=$_POST['min'];  echo "<br>";
  18.   $mentry=$_POST['mentry'];  echo "<br>";
  19.  
  20. if ($mentry=="Batch")
  21. {
  22.  
  23.     if(mysqli_connect_errno())
  24.       {
  25.          echo "Failed to connect to MYSQL: " .mysqli_connect_error();
  26.       }
  27.       //$sql="select id,name,regno,class,batch,institute from student where class = '$class'";
  28.         $sql="SELECT * FROM student WHERE batch ='$min' and class='$class'"; 
  29.          $resource=mysql_query($sql,$conn); ?>
  30.  
  31.           Selected students from:<?php echo $_REQUEST['class']?> Std and<?php echo $_REQUEST['min']?> Batch.
  32.     <?php      echo "<table align=\"center\" border=\"1\" width=\"100%\">
  33.         <tr>
  34.              <td><b>Id</b></td> 
  35.              <td><b>Name</b></td> 
  36.              <td><b>Register number</b></td> 
  37.               <td><b>Class</b></td> 
  38.               <td><b>Batch</b></td>
  39.               <td><b>Exam</b></td> 
  40.               <td><b>Institute</b></td>
  41.               <td><b>Student mark</b></td>   
  42.               <td><b>Add mark</b></td> 
  43.         </tr> ";
  44.         while($row=mysql_fetch_array($resource))
  45.           { 
  46.               echo "<tr> 
  47.                          <td>".$row['id']."</td> 
  48.                          <td>".$row['name']."</td> 
  49.                          <td>".$row['regno']."</td> 
  50.                          <td>".$row['class']."</td> 
  51.                          <td>".$row['batch']."</td>
  52.                          <td>".$_REQUEST['exam']."</td>
  53.                          <td>".$row['institute']."</td>
  54.                           <form name=\"addmarks\" method=\"post\" action=\"storemark.php\">
  55.                          <td>"."<input type=\"text\" name=\"stumark\">"."</td>/*this is my new field as student mark but it cant be send next page i give this name to belowed <a href link> it give undeifinded index stumark so  pls help me how to store this details */
  56. </form>
  57.  
  58.                         <td><a href=\"storemark.php?id= ".$row['id']."&amp;exam= ".$_REQUEST['exam']."&amp;regno= ".$row['regno']."
  59.                         &amp;name= ".$row['name']."&amp;class= ".$row['class']."&amp;batch= ".$row['batch']."
  60.                         &amp;institute= ".$row['institute']."&amp;fname= ".$row['fname']."&amp;mname= ".$row['mname']."&amp;mobile= ".$row['mobile']."
  61.                         &amp;email= ".$row['email']."\"><input type='submit' name='addmark' value='Add mark'>
  62.                         <img width='100px' height='20px'src=\"send.png\"/></a>
  63.                         </td>
  64.  
  65.               </tr>";
  66.           } 
  67.           echo "</table>";
  68. } ?>
  69.  
---------------i want store this data's to following table--
Expand|Select|Wrap|Line Numbers
  1. <?php 
  2. include("menu.php");
  3. include('db.php'); 
  4.  $id=$_REQUEST['id']; 
  5.  $exam=$_REQUEST['exam'];
  6.  $regno=$_REQUEST['regno'];
  7.  $name=$_REQUEST['name'];
  8.  $class=$_REQUEST['class'];
  9.  $batch=$_REQUEST['batch'];
  10.  $institute=$_REQUEST['institute'];
  11.  $fname=$_REQUEST['fname'];
  12.  $mname=$_REQUEST['mname'];
  13.  $mobile=$_REQUEST['mobile'];
  14.  $email=$_REQUEST['email'];
  15.  $stumark=$_REQUEST['stumark'];
  16.  
  17. echo "<pre>", print_r($_REQUEST, true), "</pre>";
  18.  
  19.  if(mysqli_connect_errno())
  20. {
  21.     echo "Failed to connect to MYSQL: " .mysqli_connect_error();
  22.  
  23. echo $sql=("insert into ".$exam." (id,exam,regno,name,class,batch,institute,fname,mname,mobile,email,stumark) values ('','$exam','$regno','$name','$class','$batch','$institute','$fname','$mname','$mobile','$email','$_POST[stumark]')");
  24.  
  25. if(!mysql_query($sql,$conn))
  26.           {
  27.               die ("An unexpected error occured while saving the record, Please try again!");
  28.           }
  29.           else
  30.          {
  31.            echo "marks added successfully!";
  32.            mysql_close($conn);
  33.           }
  34. //header("Location:examentry.php");
  35. ?>
  36.  
Feb 3 '15 #1
4 2287
Claus Mygind
571 512MB
In your receiving app where you want to update the 2nd table:

1) get rid of this line
Expand|Select|Wrap|Line Numbers
  1.     echo "<pre>", print_r($_REQUEST, true), "</pre>";
  2.  
2)you don't want to "echo" the $sql variable so delete "echo"

3)
Another way to write the same code for the $sql statment is using the "set" method, which makes it easier for the programmer (you) to read would be like this
Expand|Select|Wrap|Line Numbers
  1.     $sql= 'insert into '.$exam.' set 
  2.         id ="", 
  3.         exam ='.$exam.', 
  4.         regno ='.$regno.',
  5.         name ='.$name.',
  6.         class ='.$class.',
  7.         batch ='.$batch.',
  8.         institute ='.$institute.',
  9.         fname ='.$fname.',
  10.         mname ='.$mname.',
  11.         mobile ='.$mobile.',
  12.         email ='.$email.',
  13.         stumark ='.$_POST[stumark];
  14.  
Feb 5 '15 #2
dear Claus Mygin,
i am happy with your solution but i get some trouble to store this "stumark" field to my table here i attach my updated query with "stumark" int this query working 100% correct but my table not get this value of stumark, and i copy and paste the update query to my wampp its updated and stored successfully.but i run webpage its not store to db.
here i attach my updated php file.
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head><title>sub vendors upadate</title>
  3. <?php
  4. include("menu.php"); ?>
  5. </head>
  6. <body>
  7. <?php 
  8. echo "<pre>", print_r($_REQUEST, true), "</pre>";
  9. $id=$_GET['id'];
  10. include("db.php");
  11.  
  12. if(isset($_GET['id']))
  13. {
  14.  
  15. if(isset($_POST['submit']))
  16. {  
  17. //$id=$_POST['id'];
  18. $exam=$_POST['exam']; echo"<br>";
  19.  $regno=$_POST['regno'];echo"<br>";
  20.  $name=$_POST['name'];echo"<br>";
  21.  $class=$_POST['class'];echo"<br>";
  22.  $batch=$_POST['batch'];echo"<br>";
  23.  $institute=$_POST['institute'];echo"<br>";  
  24.  $stumark=$_POST['stumark'];echo"<br>";
  25.  $fname=$_POST['fname'];echo"<br>";
  26.  $mname=$_POST['mname'];echo"<br>";
  27.  $mobile=$_POST['mobile'];echo"<br>";
  28.  $email=$_POST['email'];echo"<br>";
  29. echo  $sql=("update ".$_REQUEST['exam']." SET exam= '$exam',regno= '$regno',name= '$name',class= '$class',batch= '$batch',
  30. institute= '$institute',stumark= '$stumark',fname= '$fname',mname= '$mname',mobile= '$mobile',email='$email' where id='$id'");
  31. if($sql)
  32. {
  33. //header('location:vendorsview.php');
  34. }
  35. }
  36. }
Feb 6 '15 #3
Claus Mygind
571 512MB
You missed inserting all the . between your string and parameters.

You have
Expand|Select|Wrap|Line Numbers
  1. echo  $sql=("update ".$_REQUEST['exam']." SET exam= '$exam',regno= '$regno',
It should be. I've underlined the . to point them out to you.
Expand|Select|Wrap|Line Numbers
  1. echo  $sql=("update ".$_REQUEST['exam']." SET exam= '.$exam.',regno= '.$regno.',
Feb 13 '15 #4
Claus Mygind
571 512MB
Here is a more complete example of what I think you are trying to do. Many assumptions had to be made about what you are doing. But if you remove all my comments you will see very few line of code are required to update a table with new information (see the 2nd code insert below in this reply). And remember this is only one way to do it.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3.     /*
  4.     ---------------------------------------
  5.     you don't need to create an output
  6.     page if all you are doing is updating
  7.     the database table.  Just return the
  8.     update result to the calling page.
  9.  
  10.     see example below.
  11.     ---------------------------------------
  12.     */
  13. //    <html> ---NOT NEEDED
  14. //        <head>
  15. //            <title>sub vendors upadate</title>---NOT NEEDED
  16. //            include("menu.php"); 
  17. //        </head>---NOT NEEDED
  18.  
  19. //        <body>---NOT NEEDED
  20.  
  21.  
  22. try  /* Error trap entire the applet. */
  23. {
  24.  
  25.  
  26.         /*
  27.         --------------------------------
  28.         store your input parameters
  29.         in a simple variable $c,
  30.  
  31.         it just makes it easier to read
  32.         --------------------------------
  33.         */
  34.         $c = $_REQUEST;
  35.  
  36. /*-EXAMPLE OF WHAT SHOULD BE IN YOUR DB.PHP MODULE-----------------------------
  37.         /*
  38.         ---------------------------------
  39.         We must assume this is where 
  40.         you have your database connection
  41.         ---------------------------------
  42.         */
  43. //        include("db.php");
  44.  
  45.         /*
  46.         -----------------------------------------------------
  47.         HERE IS ONE EXAMPLE OF WHAT SHOULD BE INSIDE YOUR DB.PHP MODULE
  48.  
  49.         there are several methods to connect your database
  50.         like pdo and mysqli.  this is just one example of
  51.         what should be in your db.php library module.
  52.  
  53.         if you add a class like this one below you can add
  54.         your own custom functions inside of it which you can
  55.         call from your other apps
  56.         -----------------------------------------------------
  57.         */
  58.         class dbcnx extends mysqli
  59.         {
  60.         } // end custom class dbcnx
  61.  
  62.  
  63.         /*
  64.         -----------------------------------------------
  65.         after you create your extended mysqli class
  66.         assign it to an easy to understand variable
  67.         like this
  68.  
  69.         Note the reference to your custom class dbcnx
  70.  
  71.         Be sure to add your login parameters
  72.         -----------------------------------------------
  73.         */
  74.         $db = new dbcnx("000.000.00.0", "your user login", "your user's password", "your database name");
  75.  
  76.         /* check connection to make sure it connected otherwise stop */
  77.         if (mysqli_connect_errno()) {
  78.             throw new Exception("Connection to database failed");
  79.             exit();
  80.         }
  81.  
  82. /*-END EXAMPLE OF WHAT SHOULD BE IN YOUR DB.PHP MODULE-----------------------------*/
  83.  
  84. /*
  85. ---------------------------------------------
  86. Now back to your program to update the table
  87. ---------------------------------------------
  88. */
  89.  
  90.  
  91.         /*
  92.         ----------------------------
  93.         Use this test to see 
  94.         if you passed the parameter
  95.  
  96.         Remember you stored the input
  97.         parameters in $c
  98.         ----------------------------
  99.         */
  100.         if(isset($c['id']))
  101.         {
  102.             /*
  103.             -----------------------------------------
  104.             we will assume all your table columns
  105.             are CHAR, so we will enclosed the values
  106.             in quotes " ", otherwise the database
  107.             update will fail.
  108.             -----------------------------------------
  109.             */
  110.  
  111.             $sql=(    'UPDATE '    .$c['exam'].
  112.                     ' SET exam="' .$c['exam'].'" '.
  113.                     ',regno="'    .$c['regno'].'" '.
  114.                     ',name="'     .$c['name'].'" '.
  115.                     ',class="'    .$c['class'].'" '.
  116.                     ',batch="'    .$c['batch'].'" '.
  117.                     ',institute="'.$c['institute'].'" '.
  118.                     ',stumark="'  .$c['stumark'].'" '.
  119.                     ',fname="'    .$c['fname'].'" '.
  120.                     ',mname="'    .$c['mname'].'" '.
  121.                     ',mobile="'   .$c['mobile'].'" '.
  122.                     ',email="'    .$c['email'].'" '.
  123.                 ' WHERE id="'     .$c['id'].'")';
  124.  
  125.             /*
  126.             -----------------------------------------------
  127.             To execute the $sql statement we now reference
  128.             our database connection which was defined as
  129.             $db in our custom class.
  130.             -----------------------------------------------
  131.             */
  132.  
  133.             if($db->query($sql))
  134.             {
  135.  
  136.                 //header('location:vendorsview.php');
  137.                 /*
  138.                 if this is what you want to do here
  139.                 you better read the manual
  140.  
  141.                 http://php.net/manual/en/function.header.php
  142.  
  143.                 to make sure you include the exit command
  144.  
  145.  
  146.                 Personally I would return a success or failure
  147.                 notice to the original calling page instead
  148.  
  149.                 */
  150.  
  151.                 $responseText = 'There was '.$db->affected_rows.' row(s) updated';
  152.                 /*
  153.                 ---------------------------------
  154.                 this should give you a count of 1 
  155.                 if the update was sucessful
  156.                 ---------------------------------
  157.                 */
  158.  
  159.             }else{
  160.                 /*if your query fails exit the program here */
  161.                 throw new Exception("sql query failed");
  162.             }
  163.         }
  164.  
  165. } // end try
  166. catch (exception $e) 
  167. {
  168.     /* if a error occured it would end up here*/
  169.     $responseText = 'An error occured :<br>' .$e->getMessage();
  170. }
  171.  
  172. /*
  173. either you return some text of the success or the error
  174. */
  175. echo $responseText;
  176. ?
If your database connection program is properly setup as I have suggested above, this is the only code needed to update your table
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. try  /* Error trap entire the applet. */
  3. {
  4.         $c = $_REQUEST;
  5.         if(isset($c['id']))
  6.         {
  7.             $sql=(    'UPDATE '    .$c['exam'].
  8.                     ' SET exam="' .$c['exam'].'" '.
  9.                     ',regno="'    .$c['regno'].'" '.
  10.                     ',name="'     .$c['name'].'" '.
  11.                     ',class="'    .$c['class'].'" '.
  12.                     ',batch="'    .$c['batch'].'" '.
  13.                     ',institute="'.$c['institute'].'" '.
  14.                     ',stumark="'  .$c['stumark'].'" '.
  15.                     ',fname="'    .$c['fname'].'" '.
  16.                     ',mname="'    .$c['mname'].'" '.
  17.                     ',mobile="'   .$c['mobile'].'" '.
  18.                     ',email="'    .$c['email'].'" '.
  19.                 ' WHERE id="'     .$c['id'].'")';
  20.  
  21.             if($db->query($sql))
  22.             {
  23.                 $responseText = 'There was '.$db->affected_rows.' row(s) updated';
  24.             }else{
  25.                 /*if your query fails exit the program here */
  26.                 throw new Exception("sql query failed");
  27.             }
  28.         }
  29.  
  30. } // end try
  31. catch (exception $e) 
  32. {
  33.     $responseText = 'An error occured :<br>' .$e->getMessage();
  34. }
  35.  
  36. echo $responseText;
  37. ?>
  38.  
Feb 16 '15 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Deja User | last post by:
This is probably very simple but I can't figure out a way to update one table from another table. Here is an example: ------ TABLEA ------ first_name last_name
1
by: Mike9900 | last post by:
What is the best way to copy DataRow from one table to another table, without copying its structure, which means copying only its data. -- Mike
1
by: thengfen | last post by:
Hi! Im having a problem in transfering a set of records from a table to another table. The scenario is when i select combo box (Course taken such as diploma in IT), then the process will...
1
by: Sakakini | last post by:
How can I append last entry from one table to another table???
4
by: xoozlez | last post by:
Hi there, How do I insert new records from a dbo table to another table? This is what I have: 1 dbo_company 1 Member (table) I made a query in dbo_company with the criteria I only want to...
3
by: anil2083 | last post by:
How to migrate the comma separated values from one table to another table? suppose we have table i.e XYZ and we have comma separated values in few columns i.e( column_name and values are...
3
by: shubham rastogi | last post by:
hello guys I want to copy or insert records into the previously created table from another table.. For example I have two tables A and B .... I want to copy or insert records from table B into...
2
by: amitsukte | last post by:
Hi Everyone how should i update multiple columns of a table from another table... Suppose I have table A and B and having four columns each table A(col1,col2,col3,col4) B(col1,col2,col3,col4) ...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.