Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
sample form in html
<form action="search.php" method="get">
<label>Name:
<input type="text" name="keyname" />
</label>
<input type="submit" value="Search" />
</form>
When I am trying to search it displays entire row with all fields. That's good. but not capture that details when i click on Edit.
search.php file:
//capture search term and remove spaces at its both ends if the is any
$searchTerm = trim($_GET['keyname']);
//check whether the name parsed is empty
if($searchTerm == "")
{
echo "Enter name you are searching for.";
exit();
}
//database connection info
$host = "xxx"; //server
$db = "xxx"; //database name
$user = "xxx"; //dabases user name
$pwd = "xxx"; //password
//connecting to server and creating link to database
$link = mysqli_connect($host, $user, $pwd, $db);
//MYSQL search statement
$query = "SELECT * FROM customer_details WHERE id LIKE '%$searchTerm%'";
$results = mysqli_query($link, $query);
/* check whethere there were matching records in the table
by counting the number of results returned */
if(mysqli_num_rows($results) >= 1)
{
$output = "";
echo '<table border=1 width=999>
<tr>
<th valign=top>ID</th>
<th valign=top>Name</th>
<th valign=top>Telephone</th>
<th valign=top>E-mail</th>
<th valign=top>Country Visiting for</th>
<th valign=top>Visa Category</th>
<th valign=top>Other Category</th>
<th valign=top>Passport No</th>
<th valign=top>Remarks</th>
<th valign=top>Date(Created)</th>
<th valign=top>Updated Remarks</th>
<th valign=top>Updated Date</th>
</tr>';
while ($row = mysqli_fetch_array($results)) {
echo '
<tr>
<td>'.$row['id'].'</td>
<td>'.$row['name'].'</td>
<td>'.$row['Telephone'].'</td>
<td>'.$row['E_mail'].'</td>
<td>'.$row['country'].'</td>
<td>'.$row['visa_categeory'].'</td>
<td>'.$row['other_category'].'</td>
<td>'.$row['passport_no'].'</td>
<td>'.$row['remarks'].'</td>
<td>'.$row['date'].'</td>
<td>'.$row['updated_remarks'].'</td>
<td>'.$row['updated_date'].'</td>
</tr>';
}
echo '
</table>';
echo $output;
}
else
echo "There was no matching record for the name " . $searchTerm;
?>
<div>Edit</div>
and my update.php file looking like this.
<?php
$host="xxx"; // Host name
$username="xxx"; // Mysql username
$password="xxx"; // Mysql password
$db_name="xxx"; // Database name
$tbl_name="customer_details"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// get value of id that sent from address bar
$id=intval($_GET['id']);
// Retrieve data from database
$sql="SELECT * FROM $tbl_name WHERE id='$id'";
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<form name="form1" method="post" action="update_ac.php">
<td>
<table align="center" style="margin-left:100px" width="600" border="0" cellspacing="1" cellpadding="0">
<tr>
<td align="left"><strong>ID</strong></td><td align="left">
<input name="id" disabled type="text" id="id" value="<?php echo $rows['id']; ?>">
</td></tr>
<tr>
<td align="left"><strong>Name</strong></td><td align="left">
<input name="name" disabled type="text" id="name" value="<?php echo $rows['name']; ?>">
</td></tr>
<tr>
<td align="left"><strong>Telephone</strong></td><td align="left">
<input name="Telephone" disabled type="text" id="Telephone" value="<?php echo $rows['Telephone']; ?>">
</td></tr>
<tr>
<td align="left"><strong>Country Visiting for</strong></td><td align="left">
<input name="country" disabled type="text" id="country" value="<?php echo $rows['country']; ?>">
</td></tr>
<tr>
<td align="left"><strong>Visa Category</strong></td><td align="left">
<input name="visa_categeory" disabled type="text" id="visa_categeory" value="<?php echo $rows['visa_categeory']; ?>">
</td></tr>
<tr>
<td align="left"><strong>Other Category</strong></td><td align="left">
<input name="other_category" disabled type="text" id="other_category" value="<?php echo $rows['other_category']; ?>">
</td></tr>
<tr>
<td align="left"><strong>Remarks</strong></td><td>
<textarea name="remarks" id="remarks" value="<?php echo $rows['remarks']; ?>" size="20"><?php echo $rows['remarks']; ?></textarea>
</td></tr><tr>
<td align="left"><strong>Date</strong></td><td>
<input name="date" type="text" id="date" value="<?php echo $rows['date']; ?>" size="20">
</td></tr>
<tr>
<td align="left"><strong>Modified Remarks</strong></td><td>
<textarea name="updated_remarks" id="updated_remarks" value="<?php echo $rows['updated_remarks']; ?>" size="20"><?php echo $rows['updated_remarks']; ?></textarea> </td></tr>
<tr>
<td align="left"><strong>Modified Date</strong></td><td>
<input name="updated_date" type="text" id="updated_date" value="<?php echo $rows['updated_date']; ?>" size="20">
</td></tr>
<tr>
<td> </td>
</tr>
<tr>
<td>
<input name="id" type="hidden" id="id" value="<?php echo $rows['id']; ?>">
</td>
<td align="left">
<input type="submit" name="Submit" value="Update">
</td>
<td> </td>
</tr>
</table>
</td>
</form>
</tr>
</table>
<?php
// close connection
mysql_close();
?>
and finally update_ac.php looking like this.
<?php
$host="xxx"; // Host name
$username="xxx"; // Mysql username
$password="xxx"; // Mysql password
$db_name="xxx"; // Database name
$tbl_name="customer_details"; // Table name
$id=$_POST['id'];
$name=$_POST['name'];
$remarks=$_POST['remarks'];
$date=$_POST['date'];
$updated_remarks=$_POST['updated_remarks'];
$updated_date=$_POST['updated_date'];
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// update data in mysql database
$sql="UPDATE $tbl_name SET id='$id', name='$name', remarks='$remarks', date='$date', updated_remarks='$updated_remarks', updated_date='$updated_date' WHERE id='$id'";
$result=mysql_query($sql);
// if successfully updated.
if($result){
echo "Successful";
echo "<BR>";
header('Location: list_records.php');
}
else {
echo "ERROR";
}
?>
Now I am Explaining this.
When I am trying to search with id it displays entire row(all fields).
But When I click on Edit in that file it doesn't capture that row details(that fields) I am asking How to capture that row details(all fields).
Thanks in Advance.
I figured out whats wrong with your code. Actually you are not passing any specific ID to your update.php page. You need to this part Edit inside your while loop and then pass id of each row inside the link. Update your update.php code like below, i think it will work:
//capture search term and remove spaces at its both ends if the is any
$searchTerm = trim($_GET['keyname']);
//check whether the name parsed is empty
if($searchTerm == "")
{
echo "Enter name you are searching for.";
exit();
}
//database connection info
$host = "xxx"; //server
$db = "xxx"; //database name
$user = "xxx"; //dabases user name
$pwd = "xxx"; //password
//connecting to server and creating link to database
$link = mysqli_connect($host, $user, $pwd, $db);
//MYSQL search statement
$query = "SELECT * FROM customer_details WHERE id LIKE '%$searchTerm%'";
$results = mysqli_query($link, $query);
/* check whethere there were matching records in the table
by counting the number of results returned */
if(mysqli_num_rows($results) >= 1)
{
//$output = "";
echo '<table border=1 width=999>
<tr>
<th valign=top>ID</th>
<th valign=top>Name</th>
<th valign=top>Telephone</th>
<th valign=top>E-mail</th>
<th valign=top>Country Visiting for</th>
<th valign=top>Visa Category</th>
<th valign=top>Other Category</th>
<th valign=top>Passport No</th>
<th valign=top>Remarks</th>
<th valign=top>Date(Created)</th>
<th valign=top>Updated Remarks</th>
<th valign=top>Updated Date</th>
<th valign=top>Action</th>
</tr>';
while ($row = mysqli_fetch_array($results)) {
echo '
<tr>
<td>'.$row['id'].'</td>
<td>'.$row['name'].'</td>
<td>'.$row['Telephone'].'</td>
<td>'.$row['E_mail'].'</td>
<td>'.$row['country'].'</td>
<td>'.$row['visa_categeory'].'</td>
<td>'.$row['other_category'].'</td>
<td>'.$row['passport_no'].'</td>
<td>'.$row['remarks'].'</td>
<td>'.$row['date'].'</td>
<td>'.$row['updated_remarks'].'</td>
<td>'.$row['updated_date'].'</td>
<td>Edit</td>
</tr>';
}
echo '
</table>';
//echo $output;
}
else
echo "There was no matching record for the name " . $searchTerm;
?>
<div></div>
The proble you are facing is because the $id variable you have used in the search.php for HREF to update.php doesnt exist. place the DB value fetched in that variable first.
Also, its better to reuse the code using $_SERVER['PHP_SELF'] rather than all different files.
Related
I have two php files, one file submits data to a second file for an update action into mysql database.
below is the code for the file that submits data
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="test_mysql"; // Table name
$server_name="localhost";
// Create connection
$con = new mysqli($server_name, $username, $password, $db_name , 3306);
if ($con->connect_error) {
die("Connection failed: " . $con->connect_error);
}
// get value of id that sent from address bar
$id=$_GET['id'];
// Retrieve data from database
$sql="SELECT * FROM $tbl_name WHERE id='$id'";
$result = $con->query($sql);
$rows = $result->fetch_assoc();
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<form name="form1" method="post" action="update_ac.php">
<td>
<table width="100%" border="0" cellspacing="1" cellpadding="0">
<tr>
<td> </td>
<td colspan="3"><strong>Update data in mysql</strong> </td>
</tr>
<tr>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
</tr>
<tr>
<td align="center"> </td>
<td align="center"><strong>Name</strong></td>
<td align="center"><strong>Lastname</strong></td>
<td align="center"><strong>Email</strong></td>
</tr>
<tr>
<td> </td>
<td align="center">
<input name="name" type="text" id="name" value="<?php echo $rows['name']; ?>">
</td>
<td align="center">
<input name="lastname" type="text" id="lastname" value="<?php echo $rows['lastname']; ?>" size="15">
</td>
<td>
<input name="email" type="text" id="email" value="<?php echo $rows['email']; ?>" size="15">
</td>
</tr>
<tr>
<td> </td>
<td>
<input name="id" type="hidden" id="id" value="<?php echo $rows['id']; ?>">
</td>
<td align="center">
<input type="submit" name="Submit" value="Submit">
</td>
<td> </td>
</tr>
</table>
</td>
</form>
</tr>
</table>
<?php
// close connection
$con->close();
?>
the second file for the update action is presented below
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="test_mysql"; // Table name
$server_name="localhost";
// Create connection
$con = new mysqli($server_name, $username, $password, $db_name , 3306);
if ($con->connect_error) {
die("Connection failed: " . $con->connect_error);
}
// update data in mysql database
$sql="UPDATE $tbl_name SET name='$name', lastname='$lastname', email='$email' WHERE id='$id'";
$result=$con->query($sql);
// if successfully updated.
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='list_records.php'>View result</a>";
}
else {
echo "ERROR";
}
?>
the error page is presented below.
An suggestions to fix the problem
Well you haven't set those values yet that's why it's getting an error.
First you must wrap your second file to check if it has submitted the form. Then set those variables inside.
<?php
if(isset($_POST['Submit'])) {
$name = $_POST['name'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$id = $_POST['id'];
// rest of your code goes here
}
Make changes to your second file as
<?php
if(isset($_POST['submit'])) {
$name = $_POST['name'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$id = $_POST['id'];
// add you rest of code
}
You should define some variables before line
$sql="UPDATE $tbl_name SET name='$name', lastname='$lastname', email='$email' WHERE id='$id'";
For example
$tbl_name = 'mytable';
$name = 'ABC';
$lastname = 'XYZ';
$email = 'abc#example.com';
$sql="UPDATE $tbl_name SET name='$name', lastname='$lastname', email='$email' WHERE id='$id'";
User this at top of your request page.
$name=$_POST['name'];
$lastname=$_POST['lastname'];
$email=$_POST['email'];
$id=$_POST['id'];
In your second file where you write sql query to update data,
You are using :-
undefine variable
name, lastname, email, id
get posted value in these variable like that, before sql query:-
$name= $_POST['name'];
$lastname= $_POST['lastname'];
I have 2 PHP files for this. The first one update.php contains the user form to update the row. The next one,update_ac contains the coding to carry out this update. The problem is i do not get a proper output
<?php
$host = "localhost"; // Host name
$username = "root"; // Mysql username
$password = ""; // Mysql password
$db_name = "yumyum"; // Database name
$tbl_name = "food"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password") or die("cannot connect");
mysql_select_db("$db_name") or die("cannot select DB");
// get value of id that sent from address bar
//$id=$_GET['id'];
$id = $_REQUEST['id'];
// Retrieve data from database
$sql = "SELECT * FROM $tbl_name WHERE id='$id'";
$result = mysql_query($sql);
$rows = mysql_fetch_array($result);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<form name="form1" method="post" action="update_ac.php">
<td>
<table width="100%" border="0" cellspacing="1" cellpadding="0">
<tr>
<td align="center"><strong>name</strong></td>
<td align="center"><strong>price</strong></td>
<td align="center"><strong>Quantity</strong></td>
</tr>
<td align="center">
<input name="name" type="text" id="name" value="<? echo $line['name']; ?>">
</td>
<td align="center">
<input name="price" type="text" id="price" value="<? echo $line['price']; ?>" size="15">
</td>
<td align="center">
<input name="Quantity" type="text" id="Quantity" value="<? echo $line['Quantity']; ?>" size="15">
</td>
<td>
<input name="id" type="hidden" id="id" value="<? echo $line['id']; ?>">
</td>
<td align="center">
<input type="submit" name="Submit" value="Submit">
</td>
</tr>
</table>
</td>
</form>
</tr>
</table>
<?php
// close connection
mysql_close();
?>
**This is update_ac.php**
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="yumyum"; // Database name
$tbl_name="food"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// update data in mysql database
$sql="UPDATE $tbl_name SET name='$name', price='$price', Quantity='$Quantity' WHERE id='$id'";
$result=mysql_query($sql);
// if successfully updated.
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='yumhome.php'>View result</a>";
}
else {
echo "ERROR";
}
?>
At First
change the variable $lines to $rows
Then in update_ac.php
get all the value using $_POST[];
because in form method="post"
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I'm trying to create a script that updates data from a table in one database to a table in another database. I'm self-made and my abilities are very limited. I'm sure there's a better way of doing this but it might be out of my reach. Anyway, this is how I'm trying to do it.
The first document selects the data from the table I want to get the information from. It contains a link that allows me to select the row I want to copy. This link basically creates a form with the information I want to copy into the second database. The idea is that when I press submit, it sends the information to the second database and it updates the fields.
The problem I'm having is that, even though it says it has executed the query successfully, it simply doesn't update the second database.
Can anyone see why this is not working?
These are the scripts I'm using:
TEST-DISPLAY.php
<?php
$host="localhost"; // Host name
$username="username"; // Mysql username
$password="password"; // Mysql password
$db_name="db1"; // Database name
$tbl_name="table1"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td>
<table width="400" border="1" cellspacing="0" cellpadding="3">
<tr>
<td colspan="4"><strong>List data from mysql </strong> </td>
</tr>
<tr>
<td align="center"><strong>Uid</strong></td>
<td align="center"><strong>User Name</strong></td>
<td align="center"><strong>Password</strong></td>
<td align="center"><strong>Email</strong></td>
<td align="center"><strong>Update</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td><? echo $rows['uid']; ?></td>
<td><? echo $rows['user']; ?></td>
<td><? echo $rows['password']; ?></td>
<td><? echo $rows['email']; ?></td>
// link to update.php and send value of id
<td align="center">update</td>
</tr>
<?php
}
?>
</table>
</td>
</tr>
</table>
<?php
mysql_close();
?>
TEST-EDIT.php
<?php
$host="localhost"; // Host name
$username="username"; // Mysql username
$password="password"; // Mysql password
$db_name="db1"; // Database name
$tbl_name="table1"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// get value of id that sent from address bar
$uid=$_GET['uid'];
// Retrieve data from database
$sql="SELECT * FROM $tbl_name WHERE uid='$uid'";
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<form name="form1" method="post" action="TEST-EXEC.php">
<td>
<table width="100%" border="0" cellspacing="1" cellpadding="0">
<tr>
<td> </td>
<td colspan="3"><strong>Update data in mysql</strong> </td>
</tr>
<tr>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
</tr>
<tr>
<td align="center"> </td>
<td align="center"><strong>Uid</strong></td>
<td align="center"><strong>User Name</strong></td>
<td align="center"><strong>Password</strong></td>
<td align="center"><strong>Email</strong></td>
</tr>
<tr>
<td> </td>
<td align="center">
<input name="uid" type="text" id="uid" value="<? echo $rows['uid']; ?>">
</td>
<td align="center">
<input name="user" type="text" id="user" value="<? echo $rows['user']; ?>">
</td>
<td align="center">
<input name="password" type="text" id="password" value="<? echo $rows['password']; ?>" size="15">
</td>
<td>
<input name="email" type="text" id="email" value="<? echo $rows['email']; ?>" size="15">
</td>
</tr>
<tr>
<td> </td>
<td>
<input name="uid" type="hidden" id="uid" value="<? echo $rows['uid']; ?>">
</td>
<td align="center">
<input type="submit" name="Submit" value="Submit">
</td>
<td> </td>
</tr>
</table>
</td>
</form>
</tr>
</table>
<?php
// close connection
mysql_close();
?>
TEST-EXEC.php
<?php
$host="localhost"; // Host name
$username="username"; // Mysql username
$password="password"; // Mysql password
$db_name="db2"; // Database name
$tbl_name="table2"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// update data in mysql database
$sql="UPDATE $tbl_name SET uname='$user', password='$password', email='$email' WHERE uid='$uid'";
$result=mysql_query($sql);
// if successfully updated.
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='TEST-DISPLAY2.php'>View result</a>";
}
else {
echo "ERROR";
}
?><!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
</head>
<body>
</body>
</html>
TEST-DISPLAY2.php is the same as TEST-DISPLAY.php with the difference that it displays results for the second table.
Eugenie, in TEST-EDIT.PHP you created a form in the script TEST-EXEC.PHP by the name of "form1" which uses the POST method. Inside of form1 you are seeding the variables username, uid, password and email. When you submit form1 it TEST-EXEC.PHP needs to capture the variables that you seeded. You do this by:
$username = $_POST['username'];
$uid = $_POST['uid'];
$password = $_POST['password'];
$email = $_POST['email'];
$_POST is a predefined variable that gets the values from a form sent with method="post". Put these at the top of TEST-EXEC.PHP. You can also use $user = mysql_real_escape_string($_POST['user']);
like another person mentioned so the values in variable user are escaped.
I am going to echo others and tell you that you have some serious security issues happening with this code because you are not paying attention to your strings. If you do not research the security dangers people are mentioning then if you use this code in production you are opening up the entire database and webserver to malicious code. And it's just not worth it.
Here's something you need, it is a function that validates email addresses to make sure they do not contain bad things.
function validEmail($email)
{
$isValid = true;
$atIndex = strrpos($email, "#");
if (is_bool($atIndex) && !$atIndex)
{
$isValid = false;
}
else
{
$domain = substr($email, $atIndex+1);
$local = substr($email, 0, $atIndex);
$localLen = strlen($local);
$domainLen = strlen($domain);
if ($localLen < 1 || $localLen > 64)
{
// local part length exceeded
$isValid = false;
}
else if ($domainLen < 1 || $domainLen > 255)
{
// domain part length exceeded
$isValid = false;
}
else if ($local[0] == '.' || $local[$localLen-1] == '.')
{
// local part starts or ends with '.'
$isValid = false;
}
else if (preg_match('/\\.\\./', $local))
{
// local part has two consecutive dots
$isValid = false;
}
else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain))
{
// character not valid in domain part
$isValid = false;
}
else if (preg_match('/\\.\\./', $domain))
{
// domain part has two consecutive dots
$isValid = false;
}
else if
(!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/',
str_replace("\\\\","",$local)))
{
// character not valid in local part unless
// local part is quoted
if (!preg_match('/^"(\\\\"|[^"])+"$/',
str_replace("\\\\","",$local)))
{
$isValid = false;
}
}
// if ($isValid && !(checkdnsrr($domain,"MX") || ?checkdnsrr($domain,"A")))
if ($isValid && !(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A")))
{
// domain not found in DNS
$isValid = false;
}
}
return $isValid;
}
You're never setting the variables $user, $password, $email, or $uid in TEST-EXEC.php. You need:
$uid = mysql_real_escape_string($_GET['uid']);
$user = mysql_real_escape_string($_POST['user']);
$password = mysql_real_escape_string($_POST['password']);
$email = mysql_real_escape_string($_POST['email']);
Notice that you use $_GET for parameters from the URL, $_POST for inputs from the form (unless the form uses method="get").
can someone tell me were I wrote wrong ? this code shows data from database but when I press submit it just reload the page and none of those field's update in database ...
shall I change variables in update query to for example : $_POST['name']
<?php
$host="localhost"; // Host name
$username="username"; // Mysql username
$password="password"; // Mysql password
$db_name="db"; // Database name
$tbl_name="test"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
// Count table rows
$count=mysql_num_rows($result);
?>
<table width="500" border="0" cellspacing="1" cellpadding="0">
<form name="form1" method="POST" action="">
<tr>
<td>
<table width="500" border="0" cellspacing="1" cellpadding="0">
<tr>
<td align="center"><strong>Id</strong></td>
<td align="center"><strong>Name</strong></td>
<td align="center"><strong>Lastname</strong></td>
<td align="center"><strong>mid</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
$id[]=$rows['id'];
?>
<tr>
<td align="center">
<input name="id[]" type="text" id="id" value="<? echo $rows['id']; ?>">
</td>
<td align="center">
<input name="name[]" type="text" id="name" value="<? echo $rows['name']; ?>">
</td>
<td align="center">
<input name="lastname[]" type="text" id="lastname" value="<? echo $rows['lastname']; ?>">
</td>
<td align="center">
<input name="midmark[]" type="text" id="midmark" value="<? echo $rows['midmark']; ?>">
</td>
</tr>
<?
}
?>
<tr>
<td colspan="4" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</td>
</tr>
</form>
</table>
<?php
if(isset($_POST['submit'])){
for($i=0;$i<$count;$i++){
$sql1=mysql_query("UPDATE $tbl_name SET name='$name[$i]', lastname='$lastname[$i]', midmark='$midmark[$i]' WHERE id='$id[$i]' ");
$result1=mysql_query($sql1);
}
}
if(isset($result1)){
header("location:multiple.php");
}
mysql_close();
?>
Try this, hope it'll help you.
if(isset($_POST['Submit']))
{
for($i=0;$i<$count;$i++)
{
$sql1=mysql_query("UPDATE `".$tbl_name."` SET name='".magic($_REQUEST['name'][$i])."', lastname='".magic($_REQUEST['lastname'][$i])."', midmark='".magic($_REQUEST['midmark'][$i])."' WHERE id='".magic($_REQUEST['id'][$i])."' ");
$result1=mysql_query($sql1);
}
}
function magic($value)
{
if( get_magic_quotes_gpc() )
$value = stripslashes( $value );
if( function_exists( "mysql_real_escape_string" ) )
$value = mysql_real_escape_string( $value );
else
$value = addslashes( $value );
return $value;
}
Try putting a '.' between the variables in your SQL string and the text and using single quotes - it may be trying to evaluate both parts of $name[$i]
, e.g.
'UPDATE '.$tbl_name.' SET name=\''.$name[$i].' ...etc
Also, always echo the SQL string before the query when testing to see what it will do
display and input using:-
<tr>
<td align="center">
<input name="data[<? echo $rows['id']; ?>][name]" type="text" id="name" value="<? echo $rows['name']; ?>">
</td>
<td align="center">
<input name="data[<? echo $rows['id']; ?>][lastname]" type="text" id="lastname" value="<? echo $rows['lastname']; ?>">
</td>
<td align="center">
<input name="data[<? echo $rows['id']; ?>][midmark]" type="text" id="midmark" value="<? echo $rows['midmark']; ?>">
</td>
</tr>
then process using:-
<?php
if(isset($_POST['submit']))
{
while(list($index,$record)=each($_POST['data']))
{
$sql=mysql_query("UPDATE $tbl_name SET name='".mysql_real_escape_string($record['name'])."', lastname='".mysql_real_escape_string($record['lastname'])."', midmark='".mysql_real_escape_string($record['nmidmarkame'])." WHERE id=".intval($index));
$result1=mysql_query($sql1);
}
}
?>
Your post variables are set wrong.
first of all your if statements, post['submit'] is lowercase, it needs to be the same as in the form:
isset($_POST['Submit'])
Then you need to read the rest of the post values and store them in variables or at least reference them properly:
$sql1=mysql_query("UPDATE $tbl_name SET name='$name[$i]', lastname='$lastname[$i]', midmark='$midmark[$i]' WHERE id='$id[$i]' ");
Should become:
$name = $_POST['name[$i]'];
$lastname = $_POST['lastname[$i]'];
$sql1=mysql_query("UPDATE $tbl_name SET name='$name', lastname='$lastname', ...
i want to get value from database..for exmaple,in the name field, it show the name that stored in the database. i want to show the value in the respective field.but it cannot retrieve the value..plz guys..help me
<?php
session_start();
$username = $_SESSION["username"];
$department = $_SESSION["department"];
?>
<html>
<head>
<title>Change Password</title>
</head>
<form method="post" action="changepassprocess.php">
<?php
$db = mysql_connect('localhost','root')
or die ("unable to connect");
mysql_select_db('fyp',$db) or die ("able to select");
$sql_select = "SELECT * FROM access WHERE username ='".$username."' ";
?>
<font face= "arial" size="2" font color="black">
<center>
<h3 align=center> Change Password </h3>
<table width="500" height="100" border="0" cellspacing="0" cellpadding="2">
<tr>
<tr>
<td align="left">User ID</td>
<td>: <input name="username" type="text" id="username" value="<? {echo "$username"; } ?>" size="20" maxlength="10" readonly='username'></td>
</tr>
<tr>
<td align="left">Name </td>
<td>: <input name="name" type="text" id="name" value="<? {echo "$name"; } ?>" size="50" readonly="name"></td>
</tr>
<tr>
<td align="left">Department </td>
<td>: <?php echo $row['department']; ?> </td>
</tr>
<tr>
<td align="left">New Password </td>
<td>:<input name="newpassword" type="password" id="newpassword" size="20" ></td>
</tr>
</table><br>
<align = center><input type="submit" name="send" value="Change Password">
</form>
</body>
</html>
Well, you forgot to run your query to the database. The $sql_select variable holds the query text, but you need to pass it to the database and retrieve the answer from it. Read http://php.net/manual/en/function.mysql-query.php and examples there.
You are missing:
$result = mysql_query($sql_select);
$row = mysql_fetch_array($result);
These will execute the query you've prepared and get the results as an array $row.
You might want to see how get fetch a value from Mysql DB using php from:
W3school: Select Data From a Database Table.
<?php
session_start();
include("../connect.php");
$user=$_SESSION['user'];
if(empty($user))
{
header("location:index.php");
}
else{
$query_display="SELECT * FROM user_login WHERE user_id_no='$user_id_no'";
$result=mysqli_query($bd,$query_display);
while($arr=mysqli_fetch_array($result))
{
$first_name=$arr['first_name'];
$last_name=$arr['last_name'];
$address=$arr['address'];
}
echo $first_name;
echo $last_name;
echo $address;
}
?>
connect.php
<?php
$mysql_hostname = "localhost";
$mysql_user = "root";
$mysql_password = "";
$mysql_database = "";
$bd=mysqli_connect($mysql_hostname,$mysql_user,$mysql_password,$mysql_database);
?>