How do I send one table values to another table with new fields? -
<?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']."&exam= ".$_REQUEST['exam']."&regno= ".$row['regno']."
-
&name= ".$row['name']."&class= ".$row['class']."&batch= ".$row['batch']."
-
&institute= ".$row['institute']."&fname= ".$row['fname']."&mname= ".$row['mname']."&mobile= ".$row['mobile']."
-
&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-- -
<?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");
-
?>
-
4 2287
In your receiving app where you want to update the 2nd table:
1) get rid of this line -
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 -
$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];
-
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. - <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');
-
}
-
}
-
}
You missed inserting all the . between your string and parameters.
You have - echo $sql=("update ".$_REQUEST['exam']." SET exam= '$exam',regno= '$regno',
It should be. I've underlined the . to point them out to you. - echo $sql=("update ".$_REQUEST['exam']." SET exam= '.$exam.',regno= '.$regno.',
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. -
<?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 -
<?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;
-
?>
-
Sign in to post your reply or Sign up for a free account.
Similar topics
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
|
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
|
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...
|
by: Sakakini |
last post by:
How can I append last entry from one table to another table???
|
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...
|
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...
|
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...
|
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)
...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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
|
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...
| |