How to edit a database row value from a html table - php

so i have a form with like 6 fields(FirstName,LastName,etc),when the users submit the form,the values are saved in a database.In another page i have a table which shows those values from database,with same 6 cols + another one which contains 2 buttons(delete and edit),the delete one works perfect,but the edit buttons has some issues
The buttons code is this:
echo '<td>';
echo '<button>Modifica!</button>';
echo '<button>Sterge!</button>';
echo '</td>';
As you can see i have assigned rand id(this is the primary key for databse rows) to the var idang.
$idang=$_GET['idang'];
if(isset($idang)) {
$CerereSQL="SELECT * FROM `angajati` WHERE id='$idang'";
$rezultat=mysqli_query($con,$CerereSQL);
$rand=mysqli_fetch_assoc($rezultat);
I've made another variable with the global var $_GET['idang'],and then i have SELECTED the table angajati from database where the primary key from my db its equal with the primary key from the table row,which i've pushed the edit button.This method works for the delete button,and probably for the edit one too,but the real problem is that when i want to edit that row,it will link me to another page containing another form,with original values in the fields,and when i modify those fields with other values,it must change only that row,not the entire table,or the column.But i have no idea how to do that,except that it can be done with the UPDATE statement
The rest of the code,containing the form is:
echo '<table border=0 align=center cellspacing=10>';
echo '<form action="" method=post>';
echo '<tr>';
echo '<th>Nume:</th><td><input type="text" name=nume value="';
echo $num;
echo '"></td>';
echo '<td style=color:red>';
echo $err_nume;
echo $err_nume1;
echo '</td>';
echo '</tr>';
echo '<tr>';
echo '<th>Prenume:</th><td><input type="text" name=prenume value="';
for($value=1;$value<count($nume);$value++) {
$c=$c."".$nume[$value];
}
echo $c;
echo '"></td>';
echo '<td style=color:red>';
echo $err_prenume;
echo $err_prenume1;
echo '</td>';
echo '</tr>';
echo '<tr>';
echo '<th>CNP:</th><td><input type="text" name=cnp value="';
echo $cnp;
echo '"></td>';
echo '<td style=color:red>';
echo $err_cnp;
echo $err_cnp1;
echo $err_cnp2;
echo '</td>';
echo '</tr>';
echo '<tr>';
echo '<th>Data angajare:</th><td><input type="text" name=data value="';
echo $rand['data'];
echo '"></td>';
echo '<td style=color:red>';
echo $err_data;
echo '</td>';
echo '</tr>';
$companii=array("S.C. 2K Telecom S.R.L.", "S.C. Accessnet International S.R.L.", "S.C. Ad Net Market Media S.A.", "S.C. Idilis S.R.L.", "S.C. Infratel Net S.R.L.");
echo '<tr>';
echo '<th>Compania:</th>';
echo '<td>';
echo '<select name=companie>';
//echo '<option value="alege">Alege compania</option>';
//if(!isset($_POST['companie'])){
echo '<option value='.$companie.'>'.$companie.'</option>';
$key=array_search($companie, $companii);
unset($companii[$key]);
//}
foreach($companii as $firme) {
echo '<option value="'.$rand['companie'].'"';
if(isset($_POST['companie']) && $_POST['companie'] == $firme) {echo " selected";}
echo ">$firme</option>";
}
echo '</select>';
echo '</td>';
echo '<td style=color:red>';
echo $err_companie;
echo '</td>';
echo '</tr>';
echo '<tr>';
echo '<th>Functia:</th><td><input type="text" name=functie value="';
echo $rand['functie'];
echo '"></td>';
echo '<td style=color:red>';
echo $err_functie;
echo '</td>';
echo '</tr>';
echo '<tr>';
echo '<td align=center><a href=tabel.php><input type=button name=button value=Inapoi!></a></td>';
echo '<td align=center><input type=submit name=submit value=Introducere! ></td>';
echo '<td align=center><input type=reset name=reset value=Resetare! ></td>';
echo '</tr>';
echo '<tr>';
echo '<td colspan="2" align=center>'.$up.'</td>';
echo '</tr>';
echo '</form>';
echo '</table>';
So this form contains the old values from the table row i've pressed the button,and i want to cahnge with new values.Any idea?

Here is basic example to update name column of your table. Get other fields and add in the query.
if(isset($_POST['submit'])) {
//echo "<pre>";print_r($_POST);exit; // to check your post data
$name = $_POST['nume'];
$query = "UPDATE table_name set name='$name' where id=$idang";
mysqli_query($query); //run the query
}

echo '<form action="abc.php" method=post>'; // add action
abc.php
If($_POST)
{
// fetch values from post
// update query
}
in else you can do if $_GET then insert.
I think from this you will get an idea to solve.

Related

Generate multiple checkboxes and evaluate them

I have a table that displays every unapproved leave, it looks like this:
For each unapproved there will be a new row, so another checkbox, but the name of the checkbox will be the same or slightly different(name="approve1"). Personally I don't think that's good practice...
Also the evaluation isn't really good, because it has to go into the database.
<form role="form" method="post" action="" id="form">
<h2><?php echo $title ?></h2>
<?php
$page = date('F');
$page = lcfirst($page);
echo "<p><a href='$page.php'>Back</a></p>";
?>
<hr>
<?php
if($i == 0){
echo "<h3>No unapproved leaves.</h3>";
}else{
echo '<table class="table table-striped table-hover">';
echo '<thead>';
echo '<th>';
echo 'Start';
echo '</th>';
echo '<th>';
echo 'End';
echo '</th>';
echo '<th>';
echo 'Employee';
echo '</th>';
echo '<th>';
echo 'Approve';
echo '</th>';
echo '</thead>';
echo '<tbody>';
for($j =0; $j < $i; $j++){
echo '<tr>';
echo '<td>';
echo $unapproved[$j]['start'];
echo '</td>';
echo '<td>';
echo $unapproved[$j]['end'];
echo '</td>';
echo '<td>';
$id = $unapproved[$j]['employee_FK'];
$result = mysql_query("select name, surname from employee where employee_ID = $id");
while ($row = mysql_fetch_assoc($result)) {
$employee[] = $row;
}
echo $employee[0]['name'], " ", $employee[0]['surname'];
echo '</td>';
echo '<td>';
echo '<input type="checkbox" name="approve" value="1" id="approve">';
echo '</td>';
echo '</tr>';
}
echo '</tbody>';
echo '</table>';
}
?>
<input type="submit" name="submit" value="Submit" class="btn" >
</div>
</div>
</form>
Can anyone think of a better, easier solution?
The practice for tabular data in most frameworks is to name them Model[id][field], so in your example it would look like "<input type='checkbox' name='leave[$j][approved]' value='1' id='leave_$j_approved'>".
Then you can easily access your data by iterating the post data like so:
foreach($_POST['leave'] as $id => $values) {
}
use checkbox name like this
<input type="checkbox" name="approve[]" value="<?php $your_row_id; ?>" id="approve">
here $your_row_id means unique id of a row in database.
And in post you will get an array named approve with all the selected values.
Parse that array using foreach.
You can generate checkboxes using a array notation in the "name" attribute.
<input type="checkbox" name="approve[]" value="1" id="approve">
After on send, you can recover values using array notation $_GET['approve'][0], ...
You can transfer arrays via HTTP :
echo '<input type="checkbox" name="approve[]" value="', $id, ']" id="approve">';
Now $_GET['approve'] will be an array which only contains values of checked boxes.
You can also specify the key associated with the value:
echo '<input type="checkbox" name="approve[', $id, ']" value="', $id, ']" id="approve">';

Is there a more efficient way to do this PHP code [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
This code works and is fine, but I was wondering if there was an easier way to code something like this?
<?php
while($rows=mysql_fetch_array($result)){
$pid=$rows['id'];
$d = $rows['date'];
echo '<style>tr, td {border:0;} #dat{text-align:right;} #co {background-color:#33B533;} </style>';
echo '<div id="postd">';
echo '<table id="mdata">';
echo '<tr>';
echo '<td>';
echo '<a href="profile.php?name=';
echo $rows['name'];
echo '">' . $rows["name"] . '</a>';
echo '</td>';
echo '<td id="dat">';
echo $result7 = nicetime($d);
echo '</td>';
echo '</tr>';
echo '<tr>';
echo '<td>';
echo $rows['post'];
echo '</td>';
echo '<td id="dat">';
echo '<form method="post" action="delete.php">';
echo '<input type="hidden" value="';
echo $rows['id'] . '" name="id" />';
echo '<input id="sub" type="submit" name="delete" value="Delete" />';
echo '</form>';
echo '<form method="post" action="edit.php">';
echo '<input type="hidden" value="';
echo $rows['id'] . '" name="id" />';
echo '<input id="sub" type="submit" name="edit" value="Edit" />';
echo '</form>';
echo '</td>';
echo '</tr>';
echo '<tr>';
echo '<td>';
$sql9 = "SELECT * FROM `like` WHERE postid='$pid' AND userid='$userid'";
$result9=mysql_query($sql9);
if($result9){
echo '<form method="post" action="like.php">';
echo '<input type="hidden" value="';
echo $rows['id'] . '" name="id" />';
echo '<input type="hidden" value="';
echo $userid . '" name="nid" />';
echo '<input id="sub" type="submit" name="like" value="Like" />';
echo '</form>';
}
echo '</td>';
echo '<td id="dat">';
if ($rows['like'] == 1) {
echo $rows['like'] . " Like";
} else if ($rows['like'] == 0) {
echo "";
} else {
echo $rows['like'] . " Likes";
}
echo '</td>';
echo '</tr>';
echo '</table>';
echo '</div>';
$sql2="SELECT * FROM comments WHERE postid='$pid' ORDER BY date ASC";
$result2=mysql_query($sql2);
while($rows2=mysql_fetch_array($result2)){
$c = $rows2['date'];
echo '<table id="co" border="1px solid black" width="250px" align="center">';
echo '<tr>';
echo '<td>';
echo $rows2['name'];
echo '</td>';
echo '<td id="dat">';
echo $result4 = nicetime($c);
echo '</td>';
echo '</tr>';
echo '<tr>';
echo '<td>';
echo $rows2['comment'];
echo '</td>';
echo '</tr>';
echo '</table>';
}
echo '<form action="addcomment.php" method="post">';
echo '<table id="co" border="1px solid black" width="250px" align="center">';
echo '<tr>';
echo '<td>';
echo '<textarea id="target" type="text" name="com" placeholder="Comment"></textarea>';
echo '</td>';
echo '<td>';
echo '<input type="submit" name="submit" value="Post">';
echo '</td>';
echo '</tr>';
echo '</table>';
echo '<input type="hidden" value="';
echo $rows['id'] . '" name="id" />';
echo '<input type="hidden" name="name" value="Jon" />';
echo '</form>';
echo '<br/>';
}
?>
Basically I've created a Facebook type system. This spools the posts down in the order they are posted in the database. Check it out at http://bockhorst.comeze.com/Wall/wall.php Its still a work in progress. Also does anyone know a php compressor?
The more efficient way to do this is to use a proper development framework that provides some structure and cohesion to your code. What you have here is a stew of SQL, HTML, and code. It's extremely hard to maintain an application written like this.
Additionally you're using the woefully out of date mysql_query interface. Please, don't. It's terrible. You really should be using an ORM to handle your routine database interfacing, something like Doctrine or Propel. These make it easier to compose the queries correctly and deal with the data instead of being stuck trying to render all your application logic directly in SQL.
If you're dead-set on doing SQL by hand, PDO is the way to go.

How to give the checkbox a value for each data that called from database and how to post it

I don't know if this can be done in php. I have an equipment list that is call from database. User can select any equipment that they want to borrow by selecting the checkbox. I have checkbox named 'list'. The question is how to put checkbox value and post the checkbox value?? because I want to save the selected item(checked) into database.
<?php
//include("connect.php");
$sql = mysql_query("SELECT * FROM equipment WHERE equip_name_desc='$search'");
$row = mysql_num_rows($sql);
if ($row >= 1){
echo '<table border="1"><tr>';
echo '<td align="center" width="40">NO</td>';
echo '<td width="200">DESCRIPTION</td>';
echo '<td width="120">SERIAL NUM.</td>';
echo '<td width="120">REF. NUM.</td>';
echo '<td width="120">PRICE</td>';
echo '<td width="120">STATUS</td>';
echo '<td width="80">BORROW</td>';
echo '</tr>';
$index=1;
while($a = mysql_fetch_array($sql))
{
echo '<tr>';
echo '<td align="center">'.$index.'</td>';
echo '<td>'.$a['equip_desc'].'</td>';
echo '<td>'.$a['equip_sn'].'</td>';
echo '<td>'.$a['equip_ref'].'</td>';
echo '<td>'.$a['equip_unit_price'].'</td>';
echo '<td>'.$a['equip_status'].'</td>';
echo '<td>'?><input type="checkbox" name="list" /><?php
echo '</tr>';
$index++;
}
echo '</table>';
}
?>
Well, pretty weird thing, but ill try to understand. I suppose the serialnumber is the unique primary. So you could use that as a reference for all the items in the array:
echo '<td><input type="checkbox" name="list" value="$a[\'equip_sn\']></td>';
this is how you put the checkbox value
echo '<td>'?><input type="checkbox" name="list" value="'.$a['your field'].'" />
to post the check box value
assign it to a variable
$variablename=$_POST['list']=="$a[0]" //take the values stored in the array

I need to upload an image with PHP and insert file names in database

I have a PHP form that updates records in a database. It looks something like this.
//update a record
$query1 = 'UPDATE mytable SET name="'.$name.'", description="'.$desc.'",
img="'.$img.'" WHERE id="'.$id.'" ';
mysqli_query($con,$query);
//get record set
$query2 = 'SELECT * FROM mytable WHERE id="'$id'"';
$result = mysqli_query($con,$query2);
echo '<form action="my-update-page.php" method="post">';
//table heading row
echo '<table width="1000" border="1" cellspacing="0" cellpadding="1">';
echo '<tr>';
echo '<td>ID</td>';
echo '<td>NAME</td>';
echo '<td>description</td>';
echo '<td>Image</td>';
echo '</tr>';
//display data
while($row = mysqli_fetch_array($result))
{
echo '<input type="hidden" name="id" value="' . $row['id'] . '" />';
echo '<tr>';
echo '<td>'. $row['id'] . '</td>';
echo '<td><input type="text" name="name" value="'. $row['name'].'" /></td>';
echo '<td><textarea name="description">'.$row['description'].'</textarea></td>';
echo '<td><input type="text" size="3" name="img" value="'. $row['img'].'"/>;
echo 'Upload Image</td>';
echo '</tr>';
}
//closing tag for table
echo '</table>';
echo '<br /><input type="submit" value="submit" /></form>';
I want my upload.php page to open in a popup where the user can upload the image. I'm pretty sure I can manage doing that. Where I get stuck is after the file is uploaded, I want the popup to close and file name to show in the form input.
Modify to reflect your names, but window.opener is the link to the other window. At that point, access any elements the same way.
window.opener.forms['myform'].elements['fileinput'].value = nameOfFile;
window.close();
As the comment above says, use AJAX to do the file post.

Can not get PHP to post info to database

I've written a script to post information into my database for I can't get it to post and need help pointing out what I'm missing.
my php post for looks like this:
$query = "SELECT * from departments";
$res = mysql_query($query);
echo '<div id="department" >';
echo '<form action="depedit.php" method="post">';
echo '<input type="text" placeholder="Search departments">';
echo '<br>';
echo '</form>';
echo '</div>';
echo '<div id="depadd">';
echo '<form>';
echo '<table width="0" border="0">';
echo '<tr>';
echo '<td>Name:</td>';
echo '</tr>';
echo '<tr>';
echo '<td>';
echo "<select name='depid'>";
while ($row = mysql_fetch_array($res)) {
echo "<option value='".$row['id']."'>".$row['depname']."</option>";
}
echo "</select>";
echo '</td>';
echo ' </tr>';
echo '<tr>';
echo '<td> </td>';
echo '</tr>';
echo ' <tr>';
echo '<td><label class="limit">Select Limit for active courses in Learning Locker:</label></td>';
echo ' </tr>';
echo ' <tr>';
echo '<td>';
echo "<select name='courselimit'>";
echo "<option value='1'>1</option>";
echo "<option value='2'>2</option>";
echo "<option value='3'>3</option>";
echo "<option value='4'>4</option>";
echo "<option value='5'>5</option>";
echo "<option value='0'>Unlimited</option>";
echo "</select>";
echo '</td>';
echo '</tr>';
echo '<tr>';
echo '<td> </td>';
echo '</tr>';
echo '<tr>';
echo '<td><input type="radio" id="1" name="senabled" value="1"/><label for="c1" class="required">Required<br>(Study Shredder Feature Enabled)</br></label></td>';
echo '<td><input type="radio" id="0" name="senabled" value="0"/><label for="c1" class="optional">Optional</label></td>';
echo '</tr>';
echo '<tr>';
echo '<td><input type="hidden" name="orgid" value="'.$adminorgid.'"></td>';
echo '</tr>';
echo '<tr>';
echo '<td><input type="hidden" name="createdby" value="'.$userid.'"></td>';
echo '</tr>';
echo '<tr>';
echo '<td><input type="hidden" name="timecreated" value="'.time(now).'"></td>';
echo '</tr>';
echo '<tr>';
echo '<td> </td>';
echo '</tr>';
echo ' <tr>';
echo ' <td><button type="submit" class="btn">Submit</button></td>';
echo ' </tr>';
echo '</table>';
echo "</form>";
echo '</div>';
depedit.php looks like this:
$adddep = "INSERT INTO organization_dep (orgid,depid,courselimit,senabled,createdby,timecreated) VALUES ('".$_POST["orgid"].",".$_POST["depid"].",".$_POST["courselimit"].",".$_POST["senabled"].",".$_POST["createdby"].",".$_POST["timecreated"]."')";
$res = mysql_query($adddep);
if ($res === TRUE) {
echo "Department added successfully";
} else{
printf("Could not create department");
}
the correct information seems like its getting passed as the url displays the following information in it:
/index.php?depid=6&courselimit=3&senabled=1&orgid=9&createdby=1129&timecreated=1364005206
any help with this would be greatly appreciated as I'm sure its something simple that I'm just over looking.
You want to get the values from the $_POST
('".$_POST["orgid"].",".$_POST["depid"].",".$_POST["courselimit"].",".$_POST["senabled"].",".$_POST["createdby"].",".$_POST["timecreated"]."')";
but your saying it is passed through the URL, this looks like you need to use $_GET to get the values
--- and one small tip you can use echo to print multiple lines it works. as long as you close it, in the end
e.g
echo " <html>
<body>
</body>
</html>
";
I think you have a bunch of things to get right before this will be working:
Only the first form, which seems to be for searching departments, have method and action attributes
Since the second form does not have the action attribute, it is not submitting to depedit.php but to the same page that has the form.
Since the second form does not have the method attribute, it defaults to GET, and you are trying to read out POST variables in your PHP. If it was not using GET, you would not see those params in the resulting URL.
In your SQL insert statement, you must have single quotes around every single text value but not around int values. Now you have one single quote before the first value and one before the last which makes no sense.
First: this portion will not be POSTed "depedit.php" because there is no submit
echo '<form action="depedit.php" method="post">';
echo '<input type="text" placeholder="Search departments">';
echo '<br>';
echo '</form>';
Second: this will never be POSTed to depedit.php, since your <form> does not have an action specifying "depedit.php"
echo '<form>';
echo '<table width="0" border="0">';
echo '<tr>';
//other codes
echo "</form>";
maybe you mean to remove the first echo '</form>'(line 7) and the second <form>(line 10)

Categories