I have searched for this and keep getting errors no matter what I try.
The main error I get is:
mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, string given
I have created a page showing all records. The user then selects one record to edit and the id is passed on to this page which would show all the fields in a form/table. a few of the fields are readonly so can not be modified.
What I am trying to achieve is to show the results in a table so that they can be edited and then sent to the database and updated. I was thinking of sending it to another file to update and then send them back to view the results page.
<form action = 'modify2.php' method = 'post'>
<table border="1">
<tr style="border-bottom:1px solid #000000;">
<strong>User ID</strong>
<strong>Username</strong>
<strong>Password</strong>
<strong>Email</strong>
<strong>Name</strong>
<strong>Submitted By</strong>
<strong>Memeber Since</strong>
</tr>
<?PHP
//error check to see what results are obtained
echo '<br />sql code = '.$sql.'<br/>'; // this works bot the $result
shows nothing
//die();
//fetch object array and put into a row
//while($row = mysqli_fetch_assoc($result))
while($row = mysqli_fetch_assoc($sql))
{
?>
<tr><td><label>User ID :</label></td>
<td><input type = 'text' name = 'id' id = 'id' value='<?PHP echo $row["id"];?>' readonly/></input><br /></td></tr>
<tr><td><strong>Username</strong></td>
<td><input type = 'text' name = 'username' id = 'username' value='<?PHP echo $row["username"];?>'/></input><br /></td></tr>
<tr><td><label>password :</label></td>
<td><input type = 'text' name = 'password' id = 'password' value='<?PHP echo $row["password"];?>'/></input><br /></td></tr>
<tr><td><label>email :</label></td>
<td><input type = 'text' name = 'email' id = 'email' value='<?PHP echo $row["email"];?>'/></input><br /></td></tr>
<tr><td><label>Real Name :</label></td>
<td><input type = 'text' name = 'Name' id = 'Name' value='<?PHP echo $row["Name"];?>'/></input><br /></td></tr>
<tr><td><label>submitted by :</label></td>
<td><input type = 'text' name = 'submittedby' id = 'submittedby' value='<?PHP echo $row["submittedby"];?>' readonly/></input><br /></td></tr>
<tr><td><label>trn_date :</label></td>
<td><input type = 'text' name = 'trn_date' id = 'trn_date' value='<?PHP echo $row["trn_date"];?>' readonly/></input></td></tr>
<?PHP
}
//close table here
?>
<tr><td colspan='2'><div align='center'><input type = 'submit' value ='Modify Record' name = 'submit'/></td></tr>
</form></div>
</table>
You need to put $result=mysqli_query($con,$sql); first to you can use
// Associative array
$row=mysqli_fetch_assoc($result);
Related
I'm trying to create a form where you can type in information and choose several options. Based on what is chosen i would like to take id and create record in database.
<form method = "post" action = "http://localhost:81/servisai/knygynas/insert_book.php">
<input type = "submit" value = "Pridėti knygą">
<tbody>
<?php while($row = $resultb->fetch_assoc()) : ?>
<tr>
<td style="display:none"><input type = "text" name = "Autoriaus_id"></td>
<td><input type = "text" name = "Autoriaus_vardas" class="form-control" value = "<?php echo $row['Vardas']; ?>"></td>
<td><input type = "text" name = "Autoriaus_pavarde" class="form-control" value = "<?php echo $row['Pavarde']; ?>"></td>
<td><input type = "checkbox" name = "myCheckbox" ></td>
</tr>
<?php endwhile ?>
</tbody>
</table>
</form>
I'm not sure how to choose which records to send to insert_book.php upon submitting
I think it's supposed to do something with if (isset($_POST['myCheckbox'])) but dont know where to go from here
You can do it by following
<input type = "hidden" name = "Autoriaus_id[]" value="<?php echo $row['id']?>">
foreach($_POST['Autoriaus_id'] as $id){
//do your stuffs here
}
pls i'm trying to insert variable values from a php file into this html file using ajax but i don't know how to start here is the form
<form>
<table>
<tr>
<td>FULL NAME</td>
<td><input type = 'text' name = 'fullname' id = 'fullname'></td>
<td>CUID</td>
<td><input type = 'text' name = 'idno' ></td>
</tr>
<tr>
<td>SURNAME</td>
<td><input type = 'text' name = 'sname' id = 'sname'></td>
<td>TITLE</td>
<td>
<select name='title'>
<option>Select Title</option>
<?php
$conn = db();
$stmt=$conn->prepare('select TITLE from titles order by TITLE');
$stmt->execute();
while($data = $stmt->fetch()){
echo "<option>".$data['TITLE']."</option>";
}
?>
</select>
</td>
</tr>
<tr>
<td>FIRST NAME</td>
<td><input type = 'text' name = 'fname' id = 'fname'></td>
</tr>
<tr>
<td>MIDDLE NAME</td>
<td><input type = 'text' name = 'mname' id = 'mname'></td>
</tr>
<tr>
<td>DESIGNATION</td>
<td>
<select name='post' id ='post'>
<option>Select Post</option>
<?php
$conn = db();
$stmt=$conn->prepare('select post from posts order by post');
$stmt->execute();
while($data = $stmt->fetch()){
echo "<option>".$data['post']."</option>";
}
?>
</select>
</td>
</tr>
<tr>
<td>GENDER</td>
<td>
<select name='gender'>
<option>Select Gender</option>
<?php
$conn = db();
$stmt=$conn->prepare('select SEX from sx order by SEX');
$stmt->execute();
while($data = $stmt->fetch()){
echo "<option>".$data['SEX']."</option>";
}
?>
</select>
</td>
<td>DATE OF RESUMPTION</td>
<td><input type = 'date' name = 'dresume' id = 'dresume'></td>
<td>CATEGORY</td>
<td>
<select onchange='bn(this.value);' name='category'>
<option>Select Category</option>
<?php
$conn = db();
$stmt=$conn->prepare('select CATEGORY from categories order by CATEGORY');
$stmt->execute();
while($data = $stmt->fetch()){
echo "<option>".$data['CATEGORY']."</option>";
}
?>
</select>
</td>
</tr>
<tr>
<td>DEPARTMENT</td>
<td>
<select name='unit'>
<option>Select Department</option>
<?php
$conn = db();
$stmt=$conn->prepare('select DEPARTMENT from units order by DEPARTMENT');
$stmt->execute();
while($data = $stmt->fetch()){
echo "<option>".$data['DEPARTMENT']."</option>";
}
?>
</select>
</td>
<td>UNIT</td>
<td><input type = 'text' name = 'unit' id = 'unit'></td>
<td>KOL</td>
<td><input type = 'text' name = 'kol' id = 'kol'></td>
</tr>
<tr>
<td>LEVEL</td>
<td>
<select name='level' >
<option>Select Level</option>
<?php
$conn = db();
$stmt=$conn->prepare('select level from levels');
$stmt->execute();
print_r ($stmt);
while($data = $stmt->fetch()){
echo "<option>".$data['level']."</option>";
}
?>
</select>
</td>
<td>STEP</td>
<td>
<select name='step'>
<option>Select Step</option>
<?php
$conn = db();
$stmt=$conn->prepare('select step from steps');
$stmt->execute();
print_r ($stmt);
while($data = $stmt->fetch()){
echo "<option>".$data['step']."</option>";
}
?>
</select>
</td>
<td>SALARY SCALE</td>
<td><input type = 'text' name = 'sscale' id = 'sscale'></td>
</tr>
<tr>
<td>RESPONSIBLITY/DUTY POSY</td>
<td><input type = 'text' id = 'dutypost' name = 'dutypost'></td>
<td>RESPONSIBLITY ALOWANCE</td>
<td><input type = 'text' id = 'rallowance' name = 'rallowance'></td>
</tr>
<tr>
<td>SPECIAL PAYMENT</td>
<td><input type = 'text' id = 'spayment' name = 'spayment'></td>
<td>PAY PERCENTAGE</td>
<td><input type = 'text' id = 'ppercent' name = 'ppercent'></td>
</tr>
<tr>
<td>HOUSE DEDUCTION</td>
<td><input type = 'text' id = 'hded' name = 'hded'></td>
<td>RENT DEDUCTION</td>
<td><input type = 'text' id = 'rded' name = 'rded'></td>
<td>FURNITURE DEDUCTION</td>
<td><input type = 'text' id = 'fded' name = 'fded'></td>
</tr>
<tr>
<td>NHF</td>
<td><input type = 'text' id = 'nhf' name = 'nhf'></td>
<td>NHF_NO</td>
<td><input type = 'text' id = 'nhfno' name = 'nhfno'></td>
</tr>
<tr>
<td>PFA</td>
<td><input type = 'text' id = 'pfa' name = 'pfa'></td>
<td>PFA_NO</td>
<td><input type = 'text' id = 'pfano' name = 'pfano'></td>
</tr>
</table>
</form>
when an edit button is clicked these varibale will automatically appear in the form pls i'll need a head start at this still new at it thanks
here is the php file
if(isset($_POST['edit'])) {
$dresume = $data['DRESUME'];
$kol = $data['KOL'];
$level = $data['LEVEL'];
$step = $data['STEP'];
$dpost = $data['DutyPost'];
$dpay = $data['DutyPay'];
$spay = $data['Special'];
$ppercent = $data['Paypercent'];
$rentd = $data['HDEDP'];
$house = $data['HOUSE'];
$furnided = $data['FURNIDED'];
$nhf = $data['NHF'];
$nhfno = $data['NHF_NO'];
$pfa = $data['PFA'];
$pfano = $data['PFA_NO'];
}
The basic thing you should understand is that PHP executes only in server and you cannot execute your php script in html once the page is loaded.
Now you have a doubt that how did I get php written inside html worked?
Answer your file got parsed in the server and all php script written in between the "" executed and corresponding html is generated, See the source of your html in browser.
Once the page is served to browser your php code will not execute. To do any modification in the html loaded in browser, either you need to reload the page or you need some client script that executes in browser. Javascript can be used as a client script.
Ajax is a method of communication between your html loaded in browser and your server. ie, if you want to send some data to or from your html loaded in browser, you can make use of ajax. https://webdesignerhut.com/pass-data-with-ajax-to-a-php-file/ is a good example
You can find good and better documentations before you start
Basically, a form HTML send parameters to an other PHP page.
If you want do it in ajax, you need to send the form anyway
or put the ajax on the form page
form.html
<form method="post" action="success.php">
<input type="text" name="id" />
</form>
success.php
<?php echo $_POST['id']; ?>
That's the ajax way (on the same page)
form.html
<form method="post" id="formAjax" action="success.php">
<input type="text" id="inputId" name="id" />
</form>
<script>
$('#formAjax')on('submit', function() {
var id = $('#inputId').val()
alert('input id:'+id);
}
</script
HI I NEED YOUR HELP..
I have this VIEW which show all items according to its company
<?php foreach($item_list as $item2):?>
<tr>
<input type = "hidden" name = "status[]" value ="5" >
<td><input type = "text" name = "item_id[]" value ="<?php echo $item2->item_id ?>" ></td>
<td><?php echo $item2->item_desc?></td>
<td><?php echo $item2->serial_num ?></td>
<td><input type = "checkbox" name = "JobStatus[]" value = "quotation" ></td>
<td><input type = "checkbox" name = "JobStatus[]" value = "job order" ></td>
</tr>
<?php endforeach; ?>
---------------------------------------- html output ---------------------------------------
<tr>
<input type = "hidden" name = "status[]" value ="5" >
<td><input type = "text" name = "item_id[]" value ="146" ></td>
<td>sample item 01</td>
<td>123</td>
<td><input type = "radio" name = "JobStatus[]" value = "quotation" ></td>
<td><input type = "radio" name = "JobStatus[]" value = "job order" ></td>
</tr>
<tr>
<input type = "hidden" name = "status[]" value ="5" >
<td><input type = "text" name = "item_id[]" value ="147" ></td>
<td>sample item 02</td>
<td>21344</td>
<td><input type = "radio" name = "JobStatus[]" value = "quotation" ></td>
<td><input type = "radio" name = "JobStatus[]" value = "job order" ></td>
</tr>
My problem is that I want to insert this to new value (status, item_id, item_desc, serial_num, JobStatus) to quotation table in mysql. If Click I on radio item_id 146 row it will insert all values under item_id 146 likewise to item_id 147 it will insert all item values under 147 item_id
So you want the radio button to be checked according to it's job status?????
If so, try something along these lines ( i don't know how your your database is set up so you will have to alter the following):
<?php foreach($query as $item):?>
<td><input type = "text" name = "item_id" value ="<?php echo $item->item_id ?>" ></td>
<td><?php echo $item->item_desc?></td>
<td><?php echo $item->serial_num ?></td>
<td><input type="radio" name="JobStatus" value="quotation" <?php $item->job_status == 'quotation' ? echo "checked" : null; ?> ></td>
<td><input type="radio" name="JobStatus" value="job order" <?php $item->job_status == 'job order' ? echo "checked" : null; ?> ></td>
</tr>
<?php endforeach; ?>
Enclose the whole loop inside a form tag with action="index.php/controller/method"
In your controller,
public function method() {
$items = $this->input->post('item_id', TRUE);
$job_status = $this->input->post('JobStatus', TRUE);
$stati = $this->input->post('status', TRUE);
foreach($items as $key => $row) {
$this->your_model->insert_new($row, $job_status[$key], $stati[$key]);
}
}
In your_model, write the database query to insert these data wherever you need. Of course you need to change the names - method, controller and your_model to suit your application.
Hi I'm extremely new to php programming and i'm trying to create a table which shows multiple users data, but will also allow the data within the table to be edited. Then once any changes to the data has been made I want the user to able to click an 'update' button and the new updated changes will be stored in the database. I came up with this:
echo "<form action=UpdateInfo.php method=post>
<p>Edit the table below and click the update button in order to update the information.</p>";
echo "<div id = 'table_heading'>
<table class = 'heading'>
<tr>
<th class = 'odd'> Title </th>
<th> UniID </th>
<th class = 'odd'> First Name </th>
<th> Surname </th>
<th class = 'odd'> House Number </th>
<th> Address Line One </th>
<th class = 'address_line_two'> Address Line Two </th>
<th> City </th>
<th class = 'odd'> Post Code </th>
<th> County </th>
<th class = 'odd'> Phone Number </th>
<th> Email </th>
<th class = 'odd'> Username </th>
<th class = 'far_right'> Password </th>
</tr>
";
while ($return = mysql_fetch_assoc($result)) {
$phonenumber = "$return[PhoneNumber]";
$number = str_pad($phonenumber, 11, "0", STR_PAD_LEFT);
echo "
<tr class='data'>
<td class = 'title'><input class = 'title' type='text' name='title' value= '".$return['Title']."' /></td>
<td class = 'uniid'><input class = 'uniid' type='text' name='uniid' value= '".$return['UniID']."' /></td>
<td class = 'first_name'><input class = 'first_name' type='text' name='firstname' value= '".$return['FirstName']."' /></td>
<td class = 'surname'><input class = 'surname' type='text' name='surname' value= '".$return['Surname']."' /></td>
<td class = 'house_number'><input class = 'house_number' type='text' name='housenumber' value= '".$return['HouseNumber']."' /></td>
<td class = 'address_line_one'><input class = 'address_line_one' type='text' name='addresslineone' value= '".$return['AddressLineOne']."' /></td>
<td class = 'address_line_two'><input class = 'address_line_two' type='text' name='addresslinetwo' value= '".$return['AddressLineTwo']."' /></td>
<td class = 'city'><input class = 'city' type='text' name='city' value= '".$return['City']."' /></td>
<td class = 'postcode'><input class = 'postcode' type='text' name='postcode' value= '".$return['PostCode']."' /></td>
<td class = 'county'><input class = 'county' type='text' name='county' value= '".$return['County']."' /></td>
<td class = 'phonenumber'><input class = 'phonenumber' type='text' name='phonenumber' value= '" .$number. "' /></td>
<td class = 'email'><input class = 'email' type='text' name='email' value= '".$return['Email']."' /></td>
<td class = 'username'><input class = 'username' type='text' name='username' value= '".$return['Username']."' /></td>
<td class = 'password'><input class = 'password' type='text' name='password' value= '".$return['Password']."' /></td>
</tr>";
}
echo "</table>
<input class='submit' type='submit' value='Update Info' />
</form>
</div>";
This creates the table as I had hoped and allows the data within to be edited. However when the update button is clicked and the user is taken to the 'updateinfo.php' page the following query is performed:
$sql = "UPDATE completeinfo SET Title='".$_POST['title']."',
FirstName='".$_POST['firstname']."',
Surname='".$_POST['surname']."',
AddressLineOne='".$_POST['addressone']."',
AddressLineTwo='".$_POST['addresstwo']."',
County='".$_POST['county']."',
City='".$_POST['city']."',
PostCode='".$_POST['postcode']."',
Email='".$_POST['email']."',
Username='".$_POST['username']."',
Password='".$_POST['password']."',
UserType='".$_POST['usertype']."'";
This query here updates all users in the database to have the same values as the last user to be printed out in the loop, rather than just applying the specific changes I make to each users data. I know why this is happening but I have no idea how I can achieve what am I after. Does anyone know how I can possibly have a table which holds multiple users information but each users data can be edited and updated on an individual bases rather than having all users given the same values.
Sorry for any lack of clarity and thanks in advance for any help.
This assumes that UniID is a unique identifier (primary key). If it isn't then just replace UniID with whatever the primary key is for that table.
echo "
<tr class='data'>
<td class = 'title'><input class = 'title' type='text' name='title[".$return['UniID']."]' value= '".$return['Title']."' /></td>
<td class = 'uniid'><input class = 'uniid' type='text' name='uniid[".$return['UniID']."]' value= '".$return['UniID']."' /></td>
<td class = 'first_name'><input class = 'first_name' type='text' name='firstname[".$return['UniID']."]' value= '".$return['FirstName']."' /></td>
<td class = 'surname'><input class = 'surname' type='text' name='surname[".$return['UniID']."]' value= '".$return['Surname']."' /></td>
<td class = 'house_number'><input class = 'house_number' type='text' name='housenumber[".$return['UniID']."]' value= '".$return['HouseNumber']."' /></td>
<td class = 'address_line_one'><input class = 'address_line_one' type='text' name='addresslineone[".$return['UniID']."]' value= '".$return['AddressLineOne']."' /></td>
<td class = 'address_line_two'><input class = 'address_line_two' type='text' name='addresslinetwo[".$return['UniID']."]' value= '".$return['AddressLineTwo']."' /></td>
<td class = 'city'><input class = 'city' type='text' name='city[".$return['UniID']."]' value= '".$return['City']."' /></td>
<td class = 'postcode'><input class = 'postcode' type='text' name='postcode[".$return['UniID']."]' value= '".$return['PostCode']."' /></td>
<td class = 'county'><input class = 'county' type='text' name='county[".$return['UniID']."]' value= '".$return['County']."' /></td>
<td class = 'phonenumber'><input class = 'phonenumber' type='text' name='phonenumber[".$return['UniID']."]' value= '" .$number. "' /></td>
<td class = 'email'><input class = 'email' type='text' name='email[".$return['UniID']."]' value= '".$return['Email']."' /></td>
<td class = 'username'><input class = 'username' type='text' name='username[".$return['UniID']."]' value= '".$return['Username']."' /></td>
<td class = 'password'><input class = 'password' type='text' name='password[".$return['UniID']."]' value= '".$return['Password']."' /></td>
</tr>";
<?php
foreach($_POST['uniid'] as $userID)
{
$sql = "UPDATE completeinfo
SET Title='".$_POST['title'][$userID]."',
FirstName='".$_POST['firstname'][$userID]."',
Surname='".$_POST['surname'][$userID]."',
AddressLineOne='".$_POST['addressone'][$userID]."',
AddressLineTwo='".$_POST['addresstwo'][$userID]."',
County='".$_POST['county'][$userID]."',
City='".$_POST['city'][$userID]."',
PostCode='".$_POST['postcode'][$userID]."',
Email='".$_POST['email'][$userID]."',
Username='".$_POST['username'][$userID]."',
Password='".$_POST['password'][$userID]."',
UserType='".$_POST['usertype'][$userID]."'
WHERE UniID = '".$_POST['uniid'][$userID]."'";
// Execute your $sql here
}
?>
Now, that being said, it is absolutely terrible practice to inject form data into SQL statements like this. You should cleanse/sanitize/escape the values first, and it would also be good to parameterize them using prepared statements.
The problem is you need to have a WHERE clause in the UPDATE statement. Without a where clause, the update statement always updates all the rows. Similar to SELECT selecting all rows if you do not use a WHERE clause.
If usernames are unique, you can do the update like this:
<input type='hidden' name='username_orig' value= '".$return['username']."' />
and the sql like this:
$sql="UPDATE completeinfo SET
Title='".$_POST['title']."',
FirstName='".$_POST['firstname']."',
Surname='".$_POST['surname']."',
AddressLineOne='".$_POST['addressone']."',
AddressLineTwo='".$_POST['addresstwo']."',
County='".$_POST['county']."',
City='".$_POST['city']."',
PostCode='".$_POST['postcode']."',
Email='".$_POST['email']."',
Username='".$_POST['username']."',
Password='".$_POST['password']."',
UserType='".$_POST['usertype']."'"
. "WHERE username = " $_POST['username_orig'];
username_orig would be a hidden field in your form, value set to username before the update. Or, you can not include username as an updatable field, but make it a hidden field in the form.
Do the records in the table have keys, beside the username? After trying a lot of approaches, I can recommend always having a primary key field in every table set to autoincrement. That way, it always is an integer and reliably available to identify the record to be updated. Again, you would have the record key in the form as a hidden field, like this:
<input type='hidden' name='recordKey' value= '".$return['recordKey']."' />
And the SQL like this:
$sql="UPDATE completeinfo SET
Title='".$_POST['title']."',
FirstName='".$_POST['firstname']."',
Surname='".$_POST['surname']."',
AddressLineOne='".$_POST['addressone']."',
AddressLineTwo='".$_POST['addresstwo']."',
County='".$_POST['county']."',
City='".$_POST['city']."',
PostCode='".$_POST['postcode']."',
Email='".$_POST['email']."',
Username='".$_POST['username']."',
Password='".$_POST['password']."',
UserType='".$_POST['usertype']."'"
. "WHERE recordKey = " $_POST['recordKey'];
fyi, the form tag needs quotation marks around post and action, like this
<form action='updateInfo.php' method='post'>
Hope that helps.
I'm not sure if this is want you want but I think you need to use a unique field from your table to specify the record you want to update. For that you need to modify your update query to include a where clause. For eg. say we assume that Username is a unique field, your update query will be something like this:
$sql="UPDATE completeinfo SET Title='".$_POST['title']."', FirstName='".$_POST['firstname']."',
Surname='".$_POST['surname']."', AddressLineOne='".$_POST['addressone']."',
AddressLineTwo='".$_POST['addresstwo']."', County='".$_POST['county']."', City='".$_POST['city']."',
PostCode='".$_POST['postcode']."', Email='".$_POST['email']."', Username='".$_POST['username']."',
Password='".$_POST['password']."', UserType='".$_POST['usertype']."' WHERE Username='".$_POST['username']."'";
You need a WHERE clause in your MySQL query.
Here's a good tutorial:
http://www.w3schools.com/php/php_mysql_where.asp
I am trying to update whatever content in the textbox that has been edited and post to database. However, only the second record is update but the first record is not. I think should be the while loop problem but I don't what is the mistake.
Here's my edit page code:
viewadmindb.php
<?php
session_start();
include('adminconfig.php');
$sql = "SELECT * FROM admin ORDER BY ID";
$result = mysql_query($sql);
?>
<body>
<div id="wrap">
<div id="status"></div>
<form method="POST" action="adminsave.php" onSubmit="return validate(this);">
<table class="viewdb" contentEditable="true">
<tr><td id='fcolor' style='border:2px solid black' align=center> ID </td>
<td id='fcolor' style='border:2px solid black' align=center> Name </td>
<td id='fcolor' style='border:2px solid black' align=center> Password </td>
<td id='fcolor' style='border:2px solid black; width:auto;' align=center>
Department</td>
<td id='fcolor' style='border:2px solid black' align=center> Email </td></tr>
<div id="content">
<?php
while($row = mysql_fetch_array($result)){ ?>
<tr>
<td style='border:2px solid black; width:auto' align=center><?php echo $row[] =
$row['ID'] ?></td>
<td style='border:2px solid black' align=center> <?php echo $row[]
= $row['name'] ?> </td>
<td style='border:2px solid black' align=center> <?php echo $row[] =
$row['password'] ?> </td>
<td style='border:2px solid black; width:200px' align=center> <?php echo $row[] =
$row['department'] ?> </td>
<td style='border:2px solid black' align=center> <?php echo $row[] = $row['email']
?> </td>
<tr>
<td><input id='edit' type = 'text' name="ID[]" value='<?php echo $row['ID'] ?>'
maxlength="50"></td>
<td><input id='edit' type = 'text' name="name[]" value='<?php echo $row['name']
?>'
maxlength="50"></td>
<td><input id='edit' type = 'text' name="password[]" value='<?php echo
$row['password'] ?>' maxlength=50"></td>
<td><input id='edit' type = 'text' name="department[]" value='<?php echo
$row['department'] ?>' maxlength="50"></td>
<td><input id='edit' type = 'text' name="email[]" value='<?php echo
$row['email']?>'
style='width:300px' " maxlength="50"></td>
<?php } ?>
<td><input id='edit' type='submit' name='<?php $row['ID'] ?>' value='Submit'/>
</td></tr>
</table>
</form>
<?php
$ID=$row['ID'];
$name=$row['name'];
$password=$row['password'];
$department=$row['department'];
$email=$row['email'];
?>
adminsave.php
<?php
session_start();
include('adminconfig.php');
$ids=$_POST['ID'];
$name_arr=$_POST['name'];
$password_arr=$_POST['password'];
$department_arr=$_POST['department'];
$email_arr=$_POST['email'];
foreach(($ids as $key=>$id) {
$name = $name_arr[$key];
$password = $password_arr[$key];
$department = $department_arr[$key];
$email = $email_arr[$key];
$sql = "UPDATE admin SET name = '$name',password = '$password',
department ='$department',email = '$email' WHERE ID = '$id'";
}
$result = mysql_query($sql);
if(!$result){
die('invalid query:'.mysql_error());
}
else
echo ("<tr><td>" . "Data updated succesfully..." . "</td></tr>");
header('Refresh:5; url=viewadmindb.php');
die;
?>
You really should look up into how ID's are supposed to work in html. The basic things is that ID must be unique. You should not have two or more elements with same ID. But in your case it's the name-attribute that is the issue.
If you have a loop like this...
while($row = mysql_fetch_array($result)){ ?>
<tr>
<td><input id='edit' type = 'text' name="ID" value='<?php echo $row['ID'] ?>'
maxlength="50"></td>
</tr>
}?>
...and you have two rows from the $result-recordset, you will echo out html something like this:
<tr>
<td><input id='edit' type = 'text' name="ID" value='1'
maxlength="50"></td>
</tr>
<tr>
<td><input id='edit' type = 'text' name="ID" value='2'
maxlength="50"></td>
</tr>
Your then saving values into the database based on a element with name ID. But the problem is that PHP doesn't know which of the rows above it should use (How could PHP know?). When refering to an element that has a duplicate the last element in the DOM is used. Therefore only this row is take into account:
<tr>
<td><input id='edit' type = 'text' name="ID" value='2'
maxlength="50"></td>
</tr>
There are no loop in adminsave.php that indicates you want to save several values. It just tells that you want to save content into database with a specific ID.
$sql = "UPDATE admin SET name = '$name',password = '$password',
department ='$department',email = '$email' WHERE ID = '$ID'";
and because the last row in the DOM is used, the update-statement would be:
$sql = "UPDATE admin SET name = '$name',password = '$password',
department ='$department',email = '$email' WHERE ID = '2'";
You can solve this by making the name-element an array by adding brackets to name-elements: (Also make edit a class instead of an id because it's ok to have duplicate classes but not duplicate ids)
<tr>
<td><input class='edit' type = 'text' name="ID[]" value='<?php echo $row['ID'] ?>'
maxlength="50"></td>
</tr>
But then you would also have to loop through the array
<?php
$ids = $_POST['ID']; //Get array from form
$name_arr = $_POST['name'];
$password_arr = $_POST['password'];
$department_arr = $_POST['department'];
$email_arr = $_POST['email'];
foreach($ids as $key=>$id) {
//Get specific element in each array
$name = $name_arr[$key];
$password = $password_arr[$key];
$department = $department_arr[$key];
$email = $email_arr[$key];
//Create sql and execute
$sql = "UPDATE admin SET name = '$name',password = '$password',
department ='$department',email = '$email' WHERE ID = '$id'";
$result = mysql_query($sql);
}
The row:
$sql = "SELECT * FROM admin WHERE $ID = '$ID'";
is pointless because the variable $sql is overritten on the next row.
Note that above is just for demonstrating how the basic concepts of ids, names and arrays works when handling forms. You should really not just mysql_* functions, but instead read up on PDO or mysqli instead. You should sanitize (make sure unwanted data is not injected into db) before updating.
The whole Logic is wrong.
Just pass in the query string from main page to another php page ex from:admin_detail.php to edit_admin.php
Then query db for data based on passed query string
echo them in desired textbox.
then call update statement.
viewadmindb.php
The var $row you didnot set. Just ad this $row = mysql_fetch_array($reslult); before you access to table values.
What is this $row[] = $row['name'] ? You refill $row, and after you cannot access the original value from database. Use ony labels, no vars like <td> E-mail: </td>
adminsave.php
You rewrited the $sql var. The line $sql = "SELECT * FROM admin WHERE $ID = '$ID'; you donot need to use.
Good tip: use the css syntax ` and border the varchars with {$var}:
"UPDATE `admin` SET `name` = '{$name}', `password` = '{$password}', `department` = '{$department}', `email` = '{$email}' WHERE `ID` = '{$ID}'"
It seems you are new to php.
Your code is not well formated and not really readable.
Don't do $_POST['...'] and write this value directly into database (security issue => mysql injection) So please insert mysql_real_escape_string($value) before you insert into database.
What the hack is that? echo $row[] = $row['password'] don't do that! only echo is enough.
Solution of your answer:
It's normal that your code update only the last iteration of the while loop, because only the last value will be stored into the $_POST array.
If you wanna fix that you have to make the form as array like:
<input id='edit' type = 'text' name="name[]" value='<?php echo $row['name'] ?>'
maxlength="50">
Then in your viewadmindb.php you have to iterate over this values again and make for each value an extra update query which updates the value in the database.
UPDATE:
The foreach loop should look like this in adminsave.php:
$arrIds = array();
$arrNames = array();
$arrDepartments = array();
$arrPasswords = array();
// ... add all necessary vars you wan a fetch from the post request
$arrIds[] = $_POST['name'][];
$arrNames[] = $_POST['name'][];
$arrDepartments[] = $_POST['department'][];
$arrResults = array(); // To store result data if necessary
foreach($arrIds as $key => $item) {
// Build sql query
$sql = "UPDATE admin SET name = '". $arrNames[$key] . "',password = '". $arrPasswords[$key] . "',
department ='". $arrDepartments[$key] . "',email = '". $arrEmailss[$key] . "' WHERE ID = '$item'";
// Execute query!
$arrResults[] = mysql_query($sql);
}
So now you should be able to get it running...