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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • webtechbharathi
    New Member
    • May 2014
    • 9

    #1

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

    How do I send one table values to another table with new fields?
    Code:
    <?php
    include("menu.php"); 
    include("db.php"); 
     //$id=$_REQUEST['id'];
      $min=$_REQUEST['min'];
     $exam=$_REQUEST['exam'];
     $class=$_REQUEST['class'];    
     ?>
    </head>
    <body> 
    <table>
      <tr>
        <td>
        
     <?php 
    //echo  $id=$_POST['id']; echo "<br>";
      $min=$_POST['min'];  echo "<br>";
      $mentry=$_POST['mentry'];  echo "<br>";
     
    if ($mentry=="Batch")
    {
    	
        if(mysqli_connect_errno())
          {
    	     echo "Failed to connect to MYSQL: " .mysqli_connect_error();
          }
    	  //$sql="select id,name,regno,class,batch,institute from student where class = '$class'";
    	    $sql="SELECT * FROM student WHERE batch ='$min' and class='$class'"; 
    		 $resource=mysql_query($sql,$conn); ?>
     
    		  Selected students from:<?php echo $_REQUEST['class']?> Std and<?php echo $_REQUEST['min']?> Batch.
    	<?php	  echo "<table align=\"center\" border=\"1\" width=\"100%\">
    		<tr>
    		     <td><b>Id</b></td> 
    		     <td><b>Name</b></td> 
    		     <td><b>Register number</b></td> 
    		      <td><b>Class</b></td> 
    			  <td><b>Batch</b></td>
    		      <td><b>Exam</b></td> 
    			  <td><b>Institute</b></td>
    			  <td><b>Student mark</b></td>   
    			  <td><b>Add mark</b></td> 
    		</tr> ";
            while($row=mysql_fetch_array($resource))
    	      { 
    	          echo "<tr> 
    			             <td>".$row['id']."</td> 
    						 <td>".$row['name']."</td> 
    						 <td>".$row['regno']."</td> 
    						 <td>".$row['class']."</td> 
    						 <td>".$row['batch']."</td>
    						 <td>".$_REQUEST['exam']."</td>
    						 <td>".$row['institute']."</td>
    						  <form name=\"addmarks\" method=\"post\" action=\"storemark.php\">
    						 <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 */
    </form>
    						 
    						<td><a href=\"storemark.php?id= ".$row['id']."&amp;exam= ".$_REQUEST['exam']."&amp;regno= ".$row['regno']."
    						&amp;name= ".$row['name']."&amp;class= ".$row['class']."&amp;batch= ".$row['batch']."
    						&amp;institute= ".$row['institute']."&amp;fname= ".$row['fname']."&amp;mname= ".$row['mname']."&amp;mobile= ".$row['mobile']."
    						&amp;email= ".$row['email']."\"><input type='submit' name='addmark' value='Add mark'>
    						<img width='100px' height='20px'src=\"send.png\"/></a>
    						</td>
    					    
    			  </tr>";
    	      } 
    		  echo "</table>";
    } ?>
    ---------------i want store this data's to following table--
    Code:
    <?php 
    include("menu.php");
    include('db.php'); 
     $id=$_REQUEST['id']; 
     $exam=$_REQUEST['exam'];
     $regno=$_REQUEST['regno'];
     $name=$_REQUEST['name'];
     $class=$_REQUEST['class'];
     $batch=$_REQUEST['batch'];
     $institute=$_REQUEST['institute'];
     $fname=$_REQUEST['fname'];
     $mname=$_REQUEST['mname'];
     $mobile=$_REQUEST['mobile'];
     $email=$_REQUEST['email'];
     $stumark=$_REQUEST['stumark'];
     
    echo "<pre>", print_r($_REQUEST, true), "</pre>";
     
     if(mysqli_connect_errno())
    {
    	echo "Failed to connect to MYSQL: " .mysqli_connect_error();
    } 
    
    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]')");
    
    if(!mysql_query($sql,$conn))
    		  {
    			  die ("An unexpected error occured while saving the record, Please try again!");
    		  }
    		  else
    		 {
    		   echo "marks added successfully!";
    		   mysql_close($conn);
    		  }
    //header("Location:examentry.php");
    ?>
    Last edited by Frinavale; Feb 3 '15, 09:29 PM. Reason: Added the question to the body of the thread to give the code context.
  • Claus Mygind
    Contributor
    • Mar 2008
    • 571

    #2
    In your receiving app where you want to update the 2nd table:

    1) get rid of this line
    Code:
        echo "<pre>", print_r($_REQUEST, true), "</pre>";
    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
    Code:
        $sql= 'insert into '.$exam.' set 
    		id ="", 
    		exam ='.$exam.', 
    		regno ='.$regno.',
    		name ='.$name.',
    		class ='.$class.',
    		batch ='.$batch.',
    		institute ='.$institute.',
    		fname ='.$fname.',
    		mname ='.$mname.',
    		mobile ='.$mobile.',
    		email ='.$email.',
    		stumark ='.$_POST[stumark];

    Comment

    • webtechbharathi
      New Member
      • May 2014
      • 9

      #3
      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.bu t i run webpage its not store to db.
      here i attach my updated php file.
      Code:
      <html>
      <head><title>sub vendors upadate</title>
      <?php
      include("menu.php"); ?>
      </head>
      <body>
      <?php 
      echo "<pre>", print_r($_REQUEST, true), "</pre>";
      $id=$_GET['id'];
      include("db.php");
      
      if(isset($_GET['id']))
      {
      
      if(isset($_POST['submit']))
      {  
      //$id=$_POST['id'];
      $exam=$_POST['exam']; echo"<br>";
       $regno=$_POST['regno'];echo"<br>";
       $name=$_POST['name'];echo"<br>";
       $class=$_POST['class'];echo"<br>";
       $batch=$_POST['batch'];echo"<br>";
       $institute=$_POST['institute'];echo"<br>";  
       $stumark=$_POST['stumark'];echo"<br>";
       $fname=$_POST['fname'];echo"<br>";
       $mname=$_POST['mname'];echo"<br>";
       $mobile=$_POST['mobile'];echo"<br>";
       $email=$_POST['email'];echo"<br>";
      echo  $sql=("update ".$_REQUEST['exam']." SET exam= '$exam',regno= '$regno',name= '$name',class= '$class',batch= '$batch',
      institute= '$institute',stumark= '$stumark',fname= '$fname',mname= '$mname',mobile= '$mobile',email='$email' where id='$id'");
      if($sql)
      {
      //header('location:vendorsview.php');
      }
      }
      }
      Last edited by Rabbit; Feb 6 '15, 05:45 PM. Reason: Please use [code] and [/code] tags when posting code or formatted data.

      Comment

      • Claus Mygind
        Contributor
        • Mar 2008
        • 571

        #4
        You missed inserting all the . between your string and parameters.

        You have
        Code:
        echo  $sql=("update ".$_REQUEST['exam']." SET exam= '$exam',regno= '$regno',
        It should be. I've underlined the . to point them out to you.
        Code:
        echo  $sql=("update ".$_REQUEST['exam']." SET exam= '[B][U].[/U][/B]$exam[B][U].[/U][/B]',regno= '[B][U].[/U][/B]$regno[B][U].[/U][/B]',

        Comment

        • Claus Mygind
          Contributor
          • Mar 2008
          • 571

          #5
          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.

          Code:
          <?php
          
          	/*
          	---------------------------------------
          	you don't need to create an output
          	page if all you are doing is updating
          	the database table.  Just return the
          	update result to the calling page.
          
          	see example below.
          	---------------------------------------
          	*/
          //	<html> ---NOT NEEDED
          //		<head>
          //			<title>sub vendors upadate</title>---NOT NEEDED
          //			include("menu.php"); 
          //		</head>---NOT NEEDED
          	
          //		<body>---NOT NEEDED
          
          		
          try  /* Error trap entire the applet. */
          {
          		
          		
          		/*
          		--------------------------------
          		store your input parameters
          		in a simple variable $c,
          
          		it just makes it easier to read
          		--------------------------------
          		*/
          		$c = $_REQUEST;
          
          /*-EXAMPLE OF WHAT SHOULD BE IN YOUR DB.PHP MODULE-----------------------------
          		/*
          		---------------------------------
          		We must assume this is where 
          		you have your database connection
          		---------------------------------
          		*/
          //		include("db.php");
          
          		/*
          		-----------------------------------------------------
          		HERE IS ONE EXAMPLE OF WHAT SHOULD BE INSIDE YOUR DB.PHP MODULE
          
          		there are several methods to connect your database
          		like pdo and mysqli.  this is just one example of
          		what should be in your db.php library module.
          
          		if you add a class like this one below you can add
          		your own custom functions inside of it which you can
          		call from your other apps
          		-----------------------------------------------------
          		*/
          		class dbcnx extends mysqli
          		{
          		} // end custom class dbcnx
          
          
          		/*
          		-----------------------------------------------
          		after you create your extended mysqli class
          		assign it to an easy to understand variable
          		like this
          
          		Note the reference to your custom class dbcnx
          
          		Be sure to add your login parameters
          		-----------------------------------------------
          		*/
          		$db = new dbcnx("000.000.00.0", "your user login", "your user's password", "your database name");
          		 
          		/* check connection to make sure it connected otherwise stop */
          		if (mysqli_connect_errno()) {
          			throw new Exception("Connection to database failed");
          			exit();
          		}
          
          /*-END EXAMPLE OF WHAT SHOULD BE IN YOUR DB.PHP MODULE-----------------------------*/
          
          /*
          ---------------------------------------------
          Now back to your program to update the table
          ---------------------------------------------
          */
           
          
          		/*
          		----------------------------
          		Use this test to see 
          		if you passed the parameter
          
          		Remember you stored the input
          		parameters in $c
          		----------------------------
          		*/
          		if(isset($c['id']))
          		{
          			/*
          			-----------------------------------------
          			we will assume all your table columns
          			are CHAR, so we will enclosed the values
          			in quotes " ", otherwise the database
          			update will fail.
          			-----------------------------------------
          			*/
          	 
          			$sql=(	'UPDATE '    .$c['exam'].
          					' SET exam="' .$c['exam'].'" '.
          					',regno="'    .$c['regno'].'" '.
          					',name="'     .$c['name'].'" '.
          					',class="'    .$c['class'].'" '.
          					',batch="'    .$c['batch'].'" '.
          					',institute="'.$c['institute'].'" '.
          					',stumark="'  .$c['stumark'].'" '.
          					',fname="'    .$c['fname'].'" '.
          					',mname="'    .$c['mname'].'" '.
          					',mobile="'   .$c['mobile'].'" '.
          					',email="'    .$c['email'].'" '.
          				' WHERE id="'     .$c['id'].'")';
          
          			/*
          			-----------------------------------------------
          			To execute the $sql statement we now reference
          			our database connection which was defined as
          			$db in our custom class.
          			-----------------------------------------------
          			*/
          
          			if($db->query($sql))
          			{
          
          				//header('location:vendorsview.php');
          				/*
          				if this is what you want to do here
          				you better read the manual
          
          				http://php.net/manual/en/function.header.php
          
          				to make sure you include the exit command
          				
          				
          				Personally I would return a success or failure
          				notice to the original calling page instead
          
          				*/
          
          				$responseText = 'There was '.$db->affected_rows.' row(s) updated';
          				/*
          				---------------------------------
          				this should give you a count of 1 
          				if the update was sucessful
          				---------------------------------
          				*/
          
          			}else{
          				/*if your query fails exit the program here */
          				throw new Exception("sql query failed");
          			}
          		}
          
          } // end try
          catch (exception $e) 
          {
          	/* if a error occured it would end up here*/
          	$responseText = 'An error occured :<br>' .$e->getMessage();
          }
          
          /*
          either you return some text of the success or the error
          */
          echo $responseText;
          ?
          If your database connection program is properly setup as I have suggested above, this is the only code needed to update your table
          Code:
          <?php
          try  /* Error trap entire the applet. */
          {
          		$c = $_REQUEST;
          		if(isset($c['id']))
          		{
          			$sql=(	'UPDATE '    .$c['exam'].
          					' SET exam="' .$c['exam'].'" '.
          					',regno="'    .$c['regno'].'" '.
          					',name="'     .$c['name'].'" '.
          					',class="'    .$c['class'].'" '.
          					',batch="'    .$c['batch'].'" '.
          					',institute="'.$c['institute'].'" '.
          					',stumark="'  .$c['stumark'].'" '.
          					',fname="'    .$c['fname'].'" '.
          					',mname="'    .$c['mname'].'" '.
          					',mobile="'   .$c['mobile'].'" '.
          					',email="'    .$c['email'].'" '.
          				' WHERE id="'     .$c['id'].'")';
          
          			if($db->query($sql))
          			{
          				$responseText = 'There was '.$db->affected_rows.' row(s) updated';
          			}else{
          				/*if your query fails exit the program here */
          				throw new Exception("sql query failed");
          			}
          		}
          
          } // end try
          catch (exception $e) 
          {
          	$responseText = 'An error occured :<br>' .$e->getMessage();
          }
          
          echo $responseText;
          ?>
          Last edited by Claus Mygind; Feb 16 '15, 01:36 PM. Reason: corrected one coding error

          Comment

          Working...