I am actually starting my newest codes with HTML/PHP .
I am searching for retrieving data (list of persons) from mysql Data Base, displaying it into html table, then when I will clik on button "edit" it will show me in another page the details of the selected person like this :
It works fine for all the rows expects the firt row of the table.
Any help please !!!
there is my code :
<table border = 1>
<caption> Liste des personnes </caption>
<tr>
<th>id </th>
<th>nom</th>
<th>prenom</th>
<th>date Naissance</th>
<th>sexe</th>
<th>ville</th>
<th>comptence</th>
<th>photo</th>
</tr>
<?php while ($obj = mysqli_fetch_object($result)){ ?>
<tr>
<td> <?= $obj->id ?> </td>
<td><?= $obj->nom?></td>
<td><?= $obj->prenom?></td>
<td><?= $obj->dateNaissance?></td>
<td><?= $obj->sexe?></td>
<td><?= $obj->ville?></td>
<td><?= $obj->competence?></td>
<?php if (isset($obj->photo)) {?>
<td><img src="uploads/<?= $obj->photo?>" width =20 height = 20 >
<?php } ?>
<td>
<form name="editPerson" action="edit.php" method="POST">
<input type="hidden" name="id" value="<?= $obj->id ?>">
<input type="submit" name="editer" value="Edit">
</form>
</td>
</tr>
<?php } ?>
</table>
Explainations
You can't have an action='edit.php' as you will edit a specific user, not all of them. You need to specify in your action what user you want to edit. And it is mostly done with the ID of the user. So you action will look like this action='edit.php?id=1. And so, your method would be GET.
In your edit.php, you will have a $_GET['id'] variable that will contain the ID of the user to be edited. So you will have to first create a new query to search for this specific user.
You can then proceed on preparing the query.
$query = $connexion->prepare('SELECT * FROM users WHERE id = :id');
And then, executing the query with the id from the URI.
$query->execute(['id' => $_GET['id']]);
And cast the result to a variable to get the user.
Then, all you have to do in your page is a little bit of refactoring from your previous page. Meaning that there will be now a big <form> tag surrounding your <table> tag and the <td> tag will now contain <input value='<?php $user->id; ?>'> tag for example for the ID. And your edit buttons will now be a save button.
The method of the <form> in your edit?id=1 would be a POST method ot itself. So in the same page, you will be able to update the user. You can also cast the form to another page, like saveUser.php. Just be consistent from one solution to another in all your project.
<form method='POST' action='<?php echo $_SERVER['PHP_SELF']; ?>'>
Use this Code
<input type="submit" name="editer" value="Edit">
Instead of
<form name="editPerson" action="edit.php" method="POST">
<input type="hidden" name="id" value="<?= $obj->id ?>">
<input type="submit" name="editer" value="Edit">
</form>
And get the id value on edit.php file by using $_GET['id'].
Example for edit.php file:
$id = $_GET['id']
Related
I have a dynamic table which is set inside a foreach, so for each item of the array fetched create a new row. I have in the last column a button for each row. When clicking that submit button I am suppose to receive the id of that in PHP. Submission is being done correctly, but I am receiving the wrong id in PHP. Its basically taking the last id of the array when submitting. Any idea why?
Here is the table:
<form method="post" id="frm-example" action="<?php echo $_SERVER["PHP_SELF"] . '?' . e(http_build_query($_GET)); ?>">
<table id="example" class="display compact">
<thead>
<th>Device</th>
<th>Sales date</th>
<th>Client comments</th>
<th>Breakage count</th>
</thead>
<tbody>
<?php foreach ($arr_cases_devices as $cases) { ?>
<tr>
<td>
<?php echo $cases['name']; ?>
</td>
<td>
<?php echo $cases["sales_date"]; ?>
</td>
<td>
<?php echo $cases["dev_comment"]; ?>
</td>
<td>
<input type="hidden" name="device_id_breakage" value="<?php echo $cases["Dev_Id"]; ?>" />
<button type="submit" name="see_rma">See RMA</button>
</td>
</tr>
<?php } ?>
</tbody>
</table>
</form>
When clicking on see_rma this is what I receive in PHP:
if (isset($_POST['see_rma'])) {
$selected_dev = e($_POST['device_id_breakage']);
print_r($selected_dev); // prints the "Dev_Id" of the last row, not of the row clicked
}
If I try printing $cases["Dev_Id"]; inside loop in the table, it prints perfectly fine, so it prints the Dev_Id of each row correctly. So, that means there is nothing wrong with the array or data. I don't why is this happening but it's for sure the first time I am having this issue.
I do this in many other tables but for some reasons in this one its not working properly.
You have multiple <input> elements with the same name within your form, and all of them are going to be submitted when you submit the form, but PHP can only get one of them. That's why you end up with only the last one in $_POST.
It looks like you should be able to fix this by just moving some attributes from the hidden input into the button (replacing the hidden input).
<button type="submit" name="device_id_breakage" value="<?php echo $cases["Dev_Id"]; ?>">
See RMA
</button>
Only the button that was clicked will be submitted. Note that after changing the name of the button, you won't have see_rma in $_POST any more, so if you have any code that depends on that you'll need to change it to look for the other name instead.
So I've searched through some posts, and I've seen that I can't use a HTML form within another HTML form.
Like:
<form method="post" action="x.php">
<input type="..."/>
<form method="post" action="x.php">
<input type="..."/>
</form>
</form>
Ok, but my problem is that I want to make a different page, which contains HTML code like this:
<?php
if(isset($_GET['vote']) && $_GET['vote']=='yes'){
echo 'vote successfully inserted';
}
# gets the email value from MAIN form
$email = isset($_POST['email'] : $_POST['email'] : NULL;
#grab the infos from bd for the user with that email,
$stmt = $db->prepare('SELECT name,email,vote FROM tbl WHERE email=:e');
$stmt->execute(array(':e'=>$email));
while($row = $stmt->fetch(PDO::FETCH_OBJ)){
if($row->vote == 'no'){ # IF THE USER DIDN'T VOTED, THEN
if(isset($_POST['vote'])){ # IF THE <a> IS PRESSED, UPDATE DB
$sql = "UPDATE tbl SET vote='yes' WHERE email=:e";
$s = $db->prepare($sql);
$s->execute(array(':e'=>$email));
}
}
?>
<table>
<tr>
<td>Name</td>
<td>Email</td>
<td>Address</td>
<td>Vote</td>
</tr>
<tr>
<td><?php echo $row->name;?></td>
<td><?php echo $row->email;?></td>
<td><?php echo $row->address;?></td>
<td>
<form method="POST" action="" id="SECOND">
VOTE!
</form>
</td>
</tr>
</table>
<?php } // end while() ?>
Then, under this <table>, I have another form:
<form action="" method="POST" id="MAIN>
<input type="text" name="email" placeholder="email"><br/>
<input type="submit" value="Login" name="submit"/>
</form>
The project is about a electoral campaign, where a user can 'login' with this email address, and submit his vote.
So,
when the user requests the page, the MAIN form will pop-up, he will fill in his email, and will press submit.
he is redirected to the same page (I'm hiding the MAIN form), and the table will pop-up.
now, the user can select his favorite candidate, and press on the <a> link - his vote will be stored in db, updating the vote field from, initially 'no' to 'yes'.
Now, the prob is that when the <a> link is pressed, the update in the db doesn't take place.
The reason the link doesn't update the database is because the form is not being submitted. Change the to an
<input type="submit" value="VOTE!">
I know I am a beginner, but I have an issue I can't figure out. I've searched everywhere. Please don't be mean:) I'm trying to learn!:)
SO I have a while loop that is making an HTML table for me, and two of the three row[] echoes work every time, but the third echoes only the ID of the last entry in the table.
My code:
<?php
$searchsql = "SELECT * FROM `students` WHERE `fname` LIKE '%" . $searchvalue1 . "%' LIMIT 0, 10 ";
$search1result = mysql_query($searchsql);
while($search1row=mysql_fetch_array($search1result)){?>
<h3>
<table align="center">
<thead>
<tr>
<td>First Name</td>
<td>Last Name</td>
<td>Select</td>
</tr>
</thead>
<tbody>
<tr>
<td><?php echo $search1row['fname'];?></td>
<td><?php echo $search1row['lname'];?></td>
<td>
<form method="post" action="4.php">
<input type="hidden" id="voteid" name="voteid" class="inputbutton" value="<?php echo $search1row['studentid'];?>">
<input type="submit" class="inputbutton" value="SELECT">
</td>
</tr>
</tbody>
</table>
</h3>
<?php }?>
So if the table returned is (see picture)
The select button from within that form always posts the studentid from the LAST row... Every select button posts Jane's ID number. So John, Jason, Jane, and their last names are echoed correctly, but their corresponding ID numbers are not... if that makes any sense.
I have to keep it a form because of the way my site works (not a link with get variables).
Any ideas on how to get the ID to echo inside of that form?
You're not closing the form at the end of each loop. So each time the voteid input is getting overwritten which is why it always posts the id for the last row.
Add a closing form tag:
...
<form method="post" action="4.php">
<input type="hidden" id="voteid" name="voteid" class="inputbutton" value="<?php echo $search1row['studentid'];?>">
<input type="submit" class="inputbutton" value="SELECT">
</form>
...
Sorry I'm a bit of a noob when it comes to PHP but I just wondered if someone had an idea on how I could solve this PHP/SQL problem.
I have a PDO statement that gets all users from a database.
With the array of users from the database I create a foreach loop to display all of the users in a table which I want to use to select a specific user, enter a number in the row of the user I select, then click submit and store the users name and also the number. I will use this information to populate another database later.
My question is, I cant seem to reference the user or the number in the table to extract the user and number I enter. When I try and request the numbered entered in the index.php, it will only ever display a number if I enter a number for a the final user in the table. When I try and view the FullName it never works and I get 'Undefined index: FullName' error.
I also specified to 'POST in the form but it doesnt seem to be doing that.
Does anyone have any ideas?
Thanks
//function.php
function getName($tableName, $conn)
{
try {
$result = $conn->query("SELECT * FROM $tableName");
return ( $result->rowCount() > 0)
? $result
: false;
} catch(Exception $e) {
return false;
}
}
//form.php
<form action "index.php" method "POST" name='form1'>
<table border="1" style="width:600px">
<tr>
<th>Name</th>
<th>Number Entered</th>
<tr/>
<tr>
<?php foreach($users as $user) : ?>
<td width="30%" name="FullName">
<?php echo $user['FullName']; ?>
</td>
<td width="30%">
<input type="int" name="NumberedEntered">
</td>
</tr>
<?php endforeach; ?>
</table>
<input type="submit" value="submit"></td>
</form>
//index.php
$users = getName('users', $conn);
if ( $_REQUEST['NumberedEntered']) {
echo $_REQUEST['NumberedEntered'];
echo $_REQUEST['FullName'];
}
The variable FullName isn't transmitted by your form to index.php. Only values of form elemnts are sent. You can add a hidden form field, that contains FullName like this:
<input type="hidden" name="FullName" value="<?php echo $user['FullName']">
But your second problem is, that your foreach loop will create several input fields with the exact same name. You won't be able to recieve any of the entered numbers, except the last one. have a look at this question for possible solutions.
Update
Putting each row in individual form tags should solve your problem:
<?php foreach($users as $user) : ?>
<form action="index.php" method="POST">
<tr>
<td align="center" width="40%" >
<?php echo $user['FullName']; ?>
<input type="hidden" name="FullName" value="<?php echo $user['FullName']; ?>" />
</td>
<td width="30%">
<input name="NumberedEntered"/>
</td>
<td>
<input type="submit" value="submit"/>
</td>
</tr>
</form>
<?php endforeach; ?>
I have an html table I've updated to use checkboxes to be able to delete multiple files:
<table>
<thead>
<tr>
<th>Camera Name</th>
<th>Date Created</th>
<th>Video Size</th>
<th>Video Length</th>
<th>
<button type="submit" class="deletebutton" name="delete_video" value="Delete" title="Delete the selected videos" onClick="return confirm('Are you sure you want to delete?')">Delete</button><br>
<input type="checkbox" name="radioselectall" title="Select All" />
</th>
</tr>
</thead>
<tbody>
<?php
for($i=0;$i<$num_videos;$i++)
{
//do stuff
//Note: I'm looping here to build the table from the server
?>
<tr >
<td onclick="DoNav('<?php echo $url; ?>');">
<?php echo $result_videos[$i]["camera_name"]; ?>
</td>
<td onclick="DoNav('<?php echo $url; ?>');">
<?php echo setlocalTime($result_videos[$i]["video_datetime"]); ?>
</td>
<td onclick="DoNav('<?php echo $url; ?>');">
<?php echo ByteSize($result_videos[$i]["video_size"]); ?>
</td>
<td onclick="DoNav('<?php echo $url; ?>');">
<?php echo strTime($result_videos[$i]["video_length"]); ?>
</td>
<td>
<form name="myform" action="<?php echo htmlentities($_SERVER['REQUEST_URI']); ?>" method="POST">
<input type="checkbox" name="radioselect" title="Mark this video for deletion"/>
<input type="hidden" name="video_name" value="<?php echo $result_videos[$i]["video_name"]; ?>" />
</form>
</td>
</tr>
I started with first creating some jquery code to create a select all/deselect all button in the table heading and just a test to show I can find which boxes are checked. That all works:
//selectall checkboxes - select all or deselect all if top checkbox is marked in table header
$("input[name='radioselectall']").change(function()
{
if( $(this).is(':checked') )
{
$("input[type='checkbox']","td").attr('checked',true);
}
else
{
$("input[type='checkbox']","td").attr('checked',false);
}
});
//process checkboxes - which ones are on
$(".deletebutton").click(function() {
$("input[name='radioselect']:checked").each(function(i){
alert(this.value);
});
});
So the part where I'm stuck is I don't know where to go from here. I need to pass all the video_names of all the videos selected (with the checkboxes). video_name is part of a hidden field in my form. So I need to pass that to my php function when the delete button is selected. Not really sure how to tackle this. Hope this makes sense.
Simple solution maybe:
Change your checkboxes to have a value of 1 and a name of $result_videos[$i]["video_name"]; in the table rows, make the form encapsulate the whole table.
Then on submit you can do something like:
foreach ($_POST as $key => $value) {
//Delete $key (the name) where $value == 1
}
Your approach is fine if you want to access the hidden fields through jQuery at a given point but once you submit the form and lose the DOM, the PHP processing page will have no way to relate the selected checkboxes with the hidden fields as there is no consistent naming scheme.
One approach would be to use the loop counter to suffix the two in order to pair them up:
<input type="checkbox" id="radioselect_<?= $i ?>" title="Mark this video for deletion" value="1" />
<input type="hidden" id="video_name_<?= $i ?>" value="<?php echo $result_videos[$i]["video_name"]; ?>" />
This would be good if you want to relate more than the two fields. Otherwise, you could just use the video_name as the value of the checkbox, as Ing suggested.