trouble in updating multiple rows in mysql db - php

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', ...

Related

mysql / php: Update multiple rows

I try to use the below code to update multiple rows, the below code can view the results of rows but it can not be updated, where is wrong place? How to modify it ?
<?php
$host="localhost"; // Host name
$username="abc"; // Mysql username
$password="abc123"; // Mysql password
$db_name="abc"; // Database name
$tbl_name="BRAddress"; // 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 Database");
$sql="SELECT * FROM $tbl_name WHERE br_no='62779457'";
$result=mysql_query($sql);
// Count table rows
$count=mysql_num_rows($result);
?>
<table width="100%" border="0" cellspacing="1" cellpadding="0">
<form name="form1" method="post" action="">
<tr>
<td>
<table width="100%" border="0" cellspacing="1" cellpadding="0">
<tr>
<td align="center"><strong>BR No.</strong></td>
<td align="center"><strong>Date of Register</strong></td>
<td align="center"><strong>Address</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center">
<? $br_no[]=$rows['br_no']; ?><? echo $rows['br_no']; ?>
</td>
<td align="center">
<input name="br_date_of_register[]" type="date" id="br_date_of_register" value="<? echo $rows['br_date_of_register']; ?>">
</td>
<td align="center">
<input name="br_address[]" type="text" size="60" id="br_address" value="<? echo $rows['br_address']; ?>">
</td>
</tr>
<?php
}
?>
<tr>
<td colspan="3" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</td>
</tr>
</form>
</table>
<?php
// Check if button name "Submit" is active, do this
if($Submit){
for($i=0;$i<$count;$i++){
$sql1="UPDATE $tbl_name SET
br_date_of_register='$br_date_of_register[$i]',
br_address='$br_address[$i]'
WHERE br_no='$br_no[$i]'";
$result1=mysql_query($sql1);
}
}
if($result1){
header("location:update_sample.php");
}
mysql_close();
?>
Thank you very much for your help & support !
I think that you need to change this part
if($Submit){
to
if($_POST('Submit')){
I haven't run the whole or looked at entire code, but you have nothing that defines $Submit variable though from what I see.
Or you can put in
$Submit = $_POST('Submit');
before the if statement.
Let me know how you go.
Cheers
Your POST variable are empty. $br_date_of_register has no value. You must use this like following
$br_date_of_register = $_POST[br_date_of_register];
$br_address = $_POST[br_address];
for($i=0;$i<$count;$i++){
$sql1="UPDATE $tbl_name SET
br_date_of_register='$br_date_of_register[$i]',
br_address='$br_address[$i]'
WHERE br_no='$br_no[$i]'";
$result1=mysql_query($sql1);
}
Edit
if($Submit)
To
if($_SERVER['REQUEST_METHOD'] == "POST")
Considering your Submit check,
you can use this,
if(isset($_POST["Submit"]))
{
}
Further in your SQL statement, do this,
$sql1='UPDATE ' . $tbl_name . ' SET
br_date_of_register = ' . $br_date_of_register[$i] .
' , br_address = ' . $br_address[$i] .
' WHERE br_no = ' . $br_no[$i];

php mysql simple update: cannot read values

probably stupid question but...
I have a sample for PHP and MySQL, which doesn't work...
there are 3 php files:
list_records.php
update.php
update_ac.php
the problem is that 3rd one doesn't read variables from the 2nd. What am I doing wrong?
Here are my files/code:
list_records.php
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="test_mysql"; // 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>Name</strong></td>
<td align="center"><strong>Lastname</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><?php echo $rows['name']; ?></td>
<td><?php echo $rows['lastname']; ?></td>
<td><?php echo $rows['email']; ?></td>
<td align="center">update</td>
</tr>
<?php
}
?>
</table>
</td>
</tr>
</table>
<?php
mysql_close();
?>
update.php
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="test_mysql"; // 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'];
// 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> </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
mysql_close();
?>
update_ac.php
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="test_mysql"; // 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='TEST', lastname='$lastname', email='$email' WHERE id='$id'";
$result=mysql_query($sql);
// if successfully updated.
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='list_records.php'>View result</a>";
}
else {
echo "ERROR";
}
?>
u need to get the value in the server side from client side before inserting or updating them into the database.for this we have superglobal variables in php like $_GET,$_POST,$_REQUEST etc.
so include thses in your update_ac.php:
$lastname=$_POST[lastname];
$email=$_POST[email];
$id=$_POST[id];`
you can use $_REQUEST method if you are not sure by which method u passed the values from the form.but try to avoid it as it is less secure.
You need to get them from POST in the update_ac.php file like
$sql="UPDATE $tbl_name
SET name='TEST',
lastname='".$_POST['lastname']."',
email='".$_POST['email']."'
WHERE id=".$_POST['id'];
This is what you need in your update query
......lastname='".$_POST['lastname']."', email='".$_POST['email']."' WHERE id=".$_POST['id']
Whenever a form posts/submits a value to a file mentioned in an action, the file must access the values of form's field's name in $_POST array as $_POST['lastname'], $_POST['email']. etc. (if the method used is POST in form tag)
There are three ways of access submitted values:
GET http://php.net/manual/en/reserved.variables.get.php
POST http://php.net/manual/en/reserved.variables.post.php
REQUEST http://php.net/manual/en/reserved.variables.request.php

capture the row after search with id [closed]

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.

Update multiple rows in mysql with php

Here is my code below. The problem is when I try to update info it instead clears all records and does not update. How can I get this script to update and not clear. Also, I have used this before and it worked fine but all the sudden it doesn't.. I might have removed something important.
<strong>Update multiple rows in mysql</strong><br>
<?php
$mysql_host = "mysql.com";
$mysql_user = "username";
$mysql_pass = "password";
$mysql_database = "dbname";
$tbl_name="test_mysql"; // Table name
// Connect to server and select databse.
mysql_connect("$mysql_host", "$mysql_user", "$mysql_pass")or die("cannot connect");
mysql_select_db("$mysql_database")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
// Count table rows
$count=mysql_num_rows($result);
$id = array();
?>
<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>Email</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center"><? $id[]=$rows['id']; ?><? 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="email[]" type="text" id="email" value="<? echo $rows['email']; ?>"></td>
</tr>
<?php
}
?>
<tr>
<td colspan="4" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</td>
</tr>
</form>
</table>
<?php
// Check if button name "Submit" is active, do this
if(isset($_POST['Submit'])){
for($i=0;$i<$count;$i++){
$sql1="UPDATE $tbl_name SET name='$name[$i]', lastname='$lastname[$i]', email='$email[$i]' WHERE id='$id[$i]'";
$result1=mysql_query($sql1);
}
}
if($result1){
echo "Good";
////header("location:update_multiple.php");
}
mysql_close();
?>
You have using wrong set of variables,
try
$name[$i] <-- access local variable, an array called $name
$_POST["name"][$i] <-- access $_POST, the form name instead
I would suggest you make use $row["id"] as index key (name[$row["id"]]),
instead of using sequential indexed (key (0, 1, 2...)
None of your variables are defined in your SQL ($name, $lastname, $email, $id).
In your for loop, use $_POST['name'][$i] and so forth.
Also, it seems like you have forgotten to put your id in some form (hidden) field?
try this:
<?php require_once('Connections/tlsc_conn.php');
mysql_select_db($database_tlsc_conn, $tlsc_conn);
$query_Recordset1 = "SELECT * FROM tbl_name";
$Recordset1 = mysql_query($query_Recordset1, $tlsc_conn) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
if(isset($_POST['submit'])) {
// $count = count($_POST['id']);
// $count=mysql_num_rows($Recordset1);
$submit = $_GET['submit'];
$i = ($_POST['count']);
$name = ($_POST['name']);
$lastname = ($_POST['lastname']);
$email = ($_POST['email']);
$id = ($_POST['id']);
for($i=0;$i<$count;$i++){
$sql1="UPDATE $tbl_name SET name='{$_POST['name'][$i]}',
lastname='{$_POST['lastname'][$i]}',
email='{$_POST['email'][$i]}'
WHERE id='{$_POST['id'][$i]}'";
$row_Recordset1=mysql_query($sql1);
}
if($row_Recordset1){
header("location:lulu.php");
exit;
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org /TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<form name="form2" method="post" action="">
<table width="634" border="1">
<tr>
<td>id</td>
<td>name</td>
<td>lastname</td>
<td>email</td>
</tr>
<?php do { ?>
<tr>
<td><?php $id[]=$row_Recordset1['id']; ?><?php echo $row_Recordset1['id']; ?>
<input name="id[]" type="hidden" value="<?php echo $row_Recordset1['id']; ?>" /></td>
<td><input name="name[]" type="text" value="<?php echo $row_Recordset1['name']; ?>"></td>
<td><input name="lastname[]" type="text" value="<?php echo $row_Recordset1['lastname']; ?>"></td>
<td><input name="email[]" type="text" value="<?php echo $row_Recordset1['email']; ?>"> </td>
</tr>
<?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
</table>
<p>
<input type="submit" name="submit" value="Submit" />
</p>
</form>
<p>
</p>
</body>
</html>
Used this command to change multiple entries in the database:
$sql = "UPDATE users SET name = ?, lastname = ?, email = ? WHERE id = '{$_SESSION['id']}'";

how to get value from the database

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);
?>

Categories