Getting specific table row based on id mysql DB - php

I have a database where I view all the records, the last column is the table ID, I click it and I want to be able to edit that rows data only, here's what I got but it doesn't waork after updating phpmyadmin:::
<?php
include "db.inc.php";
$id=$_GET['id'];
$order = "SELECT * FROM ircb where id='$id'";
$result = mysql_query($order);
$row = mysql_fetch_array($result);
?>
<form method="post" action="update.php">
<input type="hidden" name="id" value="<? echo "$row[id]"?>">
<tr>
<td>Date</td>
<td>
<input type="text" name="cdate"
value="<? echo "$row[cdate]"?>" size="30" style="color: black;background-color:#FFFF11">
</td>
</tr>
<tr>
<td>Item</td>
<td>
<input type="text" name="item"
value="<? echo "$row[item]"?>" size="30" style="color: black;background-color:#FFFF11">
</td>
</tr>
I end up getting no results returned, when I hover over the link it does display the correct table row id but when I click the link I get empty boxes containing some of the code like <? echo in the fields..no true values though..and the page header after clicking the link does show ::: ...../edit_form.php?id=8 for row 8 so I assume something in my query is not quite right.
thanks

It is because you try to GET a name "id" from a form which is POSTed ;) And also embed PHP in html more clearly. For example, value="<?php echo $row['id']; ?>" is more "true" rather than value="<? echo "$row[id]"?>" ;)

Related

Create multiple forms in a html table

I've got a MySQL database and a table called courses. I would like to show the records of this table within an html table, but I would also like to edit them with a submit button at the end.
I know this has been asked before, but I'm still struggling with how I should properly proceed.
I currently have:
<tbody>
<?php
foreach ($db->getResultSet() as $row) {
extract($row);
?>
<tr>
<form id="myTable" name="editrecord" action="course.php" method="post">
<input type="hidden" name="code" value="<?php echo $code; ?>" /><!--PrimaryKey-->
<td><?php echo $code; ?></td>
<td><input type="text" name="name" value="<?php echo $name; ?>" /></td>
<td><input type="text" name="desc" value="<?php echo $desc; ?>" /></td>
<td><input type="submit" name="Edit" value="Edit!" /></td>
</form>
</tr>
<?php
}
?>
</tbody>
Now this seems to work fine. However I'm aware that <form> should not be inside <tr> or anywhere else other that <td>
I'm aware I could use various divs and set display properties to table layout etc. however I'd like to avoid this as I am using pre-made javascript that can sort and filter data which requires me to use <table>
I also tried a for loop outside the table to create 1 form per record and use the primary key as the form's id. Then proceed with echo "<td><input form='$code' .... But this doesn't work in Explorer
What is the correct way to implement something like this?

PHP POST value from a looped table

Please help me out of this.....
I am designing a table which is inside a form.
the table is generated based on while loop.
In each row there is a download button.
when i click download the POST value should get the same row information.
But my POST variable is giving me the last row information only.
I tried using input-type as hidden... But it did not work
Here is the code for your reference
enter code here
<form name="simpleform" method="post" action="insert.php">
<?php
$data = "environment";
$user_name = $_SESSION['username'];
$serch = mysql_query("SELECT * FROM data WHERE (data_category = '" . $data . "') ");
while ($record=mysql_fetch_assoc($serch))
{?>
<tr class="warning">
<td >
<input type="text" value=<?php echo $record['data_ID'];?> readonly="readonly" >
<input type="hidden" value=<?php echo $record['data_ID'];?> name="dataid" />
</td>
<td >
<input type="text" value=<?php echo $record['data_name'];?> readonly="readonly" >
<input type="hidden" value=<?php echo $record['data_name'];?> name="dataname" />
</td>
<td >
<input type="text" value=<?php echo $record['data_downloads'];?> readonly="readonly">
<input type="hidden" value=<?php echo $record['data_downloads'];?> name="datadown" />
</td>
<td >
<input type="text" value="" >
<input type="hidden" value="" name="datause" />
</td>
<td>
<input type="submit" name="simplesubmit" value="Go to download" />
</td>
</tr>
<?php }
exit;
?>
</tbody>
</form>
The problem is that you are using the same name attribute for all your controls. Thus, when PHP receives the form, they get overwritten, and you only see the last value of the form.
The simplest way to avoid that is just appending [] to the end of your names -- eg name=dataid[]. This will make PHP take all arguments as an array, so you don't lose data.
The second problem, is that your submit button also has the same name - you should diversify it by using some row-specific data in its name, such as 'name="submit-'.$record['data_name'].'"'
For more info, more code from you is needed, such as what are the data you are printing like.
Every post button can have its name and value, so if you change your code to produce a traceable post button name you can just do whatever you want with that.
<table>
<tr>
<td>...</td>
<td>...</td>
<td><input type="submit" name="submit[1]" value="OK" />
</tr>
<tr>
<td>...</td>
<td>...</td>
<td><input type="submit" name="submit[2]" value="OK" />
</tr>
</table>
When the form is posted its very easy to capture which button is clicked;
if ($_POST["submit"]) {
$id = key($_POST["submit"]);
}
Thanks for info... and good response. As you said , i replaced the same and saw the post value is giving me all arguments as array. My purpose is to let the client download file that he clicks. so if the client click the first row button in the table, the post value should get only that Data_name. So that i can run a query to get the URL of that data_name and download

PHP post two values in one field, one of which is hidden

Is there a way that I can post two values at the same time from a single field in a table but hide one from the user?
I would like the following form to post the values ID and reason_name when it is submitted but have the user only be able to see (and edit in the text box) the reason_name.
<form name="add_positioning" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<table border="1" class="autoTable_pos">
<tr>
<td>Positioning</td><td> </td></tr>
<?
$sql= "SELECT * FROM gradeReason WHERE reason_userID = $user_id AND category = 'positioning' AND current = 1";
$result = mysqli_query($mysqli,$sql) or die(mysqli_error($mysqli));
while($row = mysqli_fetch_array($result)){?>
<tr>
<td>
<input type="text" name="reason[]" size="25" value="<? echo $row['reason_name']; ?>"/>
</td>
<td>
<input type="button" value="Delete" class="delRow_pos"/>
</td>
</tr>
<?
}
?>
<tr>
<td>
<input type="text" name="reason[]" size="25"/>
</td>
<td>
<input type="button" value="Delete" class="delRow_pos"/>
</td>
</tr>
<tr>
<td colspan="2" align="right">
<input type="submit" value="Save" name="save_reasons"/>
</td>
</tr>
</table>
</form>
The form POST action so far (basic echo at the moment for my own sanity to check that it posts the values correctly, which it does...)
if(isset($_POST['save_reasons'])) {
foreach($_POST['reason'] as $item) {
echo $item.'<br/>';
}
}
This table displays the values that are held in a database but enables the user to add more values by dynamically adding a new row (using JQUERY I haven't included) to the table when they type in an empty one at the bottom and also allows them to edit or delete existing values.
For each value posted I intend to check if the ID value is empty, if it is it means that it is a new value and enter a new record into the database, if it isn't update the existing record in the database with the corresponding ID. I don't have a problem writing that bit, I just can't think how to get the ID value posted as well as the reason_name while keeping ID hidden from the user.
Add the ID to the name attribute of the Text boxes of the reasons loaded from the DB. Leave the other Text boxes added using JQ without the ID.
E.g.
Text box which shows the existing reason loaded from the DB
<input type="text" name="reason[][ID]" size="25"/>
Text box added with JQ
<input type="text" name="reason[]" size="25"/>
Then once you submit the form you get you will get the following array.
array
'reason' =>
array
0 =>
array
14 => string 'VAL1' (length=4)
1 => string 'VAL2' (length=5)
By checking for an array in the each element in the "reason" array, you can differentiate two type of Text Boxes.
Here is the complete code I have tested.
<?PHP
//var_dump($_POST);
foreach($_POST['reason'] as $item=>$value)
{
if(is_array($value)){
foreach($value as $ID=>$reason)
{
echo "Existing Reason : ".$ID." - ".$reason."<br>";
}
}
else{
echo "New Reason : ".$item." - ".$value.'<br/>';
}
}
?>
<form action="" method="POST">
<input type="text" name="reason[][14]" size="25" value="aaaa"/>
<input type="text" name="reason[]" size="25" value="bbbbb"/>
<input name="" type="submit">
</form>
Assuming the id is at $row['reason_id'] I think you want:
$i = 0;
while($row = mysqli_fetch_array($result)){?>
<tr>
<td>
<input type="hidden" name="reason_id[<? echo $i; ?>]" size="25" value="<? echo $row['reason_id']; ?>"/>
<input type="text" name="reason[<? echo $i; ?>]" size="25" value="<? echo $row['reason_name']; ?>"/>
</td>
<td>
<input type="button" value="Delete" class="delRow_pos"/></td></tr>
<? $i++ } ?>
This way you can later
if(isset($_POST['save_reasons'])) {
foreach($_POST['reason'] as $key => $item) {
$id = $_POST['reason_id'][$key];
echo $id . " " . $item.'<br/>';
}
}

Can't get data from the database to show in the fields to update through a form

I have a table in my database called agendas which is linked with another table called meetings. I would like to edit the agendas table through the form but I want the current information in the agendas fields to appear on the web form.
<?php
include 'library/connect.php';
$agenda_id = $_GET['agenda_id'];
$result = mysql_query("SELECT agenda.*, meetings.meeting_id FROM agenda INNER JOIN meetings ON agenda.meetings = meetings.meeting_id WHERE agenda_id = '$agenda_id'");
$row = mysql_fetch_array($result);
$meeting_id = $row['meeting_id'];
?>
<form id="form1" name="form1" method="post" action="secretary_agendaSuccesful.php?agenda_id=<?php echo $agenda_id; ?>">
<table width="666" border="1">
<tr>
<td width="91">Subject:</td>
<td width="559"><span id="sprytextarea1">
<label for="subject"></label>
<textarea name="subject" id="subject" cols="45" rows="5" value="<? echo $row['subject'] ?>"></textarea>
<span class="textareaRequiredMsg">A subject is required.</span></span></td>
</tr>
<tr>
<td>Duration:</td>
<td><span id="sprytextfield1">
<label for="duration"></label>
<input type="text" name="duration" id="duration" value="<? echo $row['duration'] ?>"/>
<span class="textfieldRequiredMsg">duration in hours</span><span class="textfieldInvalidFormatMsg">Enter duration in hours</span></span></td>
</tr>
<td> </td>
<td><input type="submit" name="submitbtn" id="submitbtn" value="Submit" /></td>
</tr>
</table>
</form>
Is this the correct way to get information from a database into the fields?
this is almost correct way
only you need is to use htmlspecialchars() function on the displayed values.
there can be another problem - with the query itself, which prevents your data from displaying.
and there is also obvious SQL injection
Make your code this way
$agenda_id = intval($_GET['agenda_id']);
$query = "SELECT agenda.*, meetings.meeting_id FROM agenda
INNER JOIN meetings ON agenda.meetings = meetings.meeting_id
WHERE agenda_id = $agenda_id";
$result = mysql_query($query) or trigger_error(mysql_error()." ".$query);
and see if it will show any errors
Edit
just noticed that you are using value parameter for the <textarea> tag. This tag has different usage from others.

display database field in textbox

I am trying to display a database input in a textbox for editing. I cant seem to get it working?!
The page links from a forum post edit button. Firstly, I don't know how to get the database info for that particular field to be displayed and also I don't understand how to make sure the post that the user clicks to edit is the post that is displayed.
My code looks like this
<?php
#data preparation for the query
$id=$_GET['id'];
# selects title and description fields from database
$sql = "SELECT a_answer FROM $tbl_name WHERE question_id='$id'";
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);
?>
<h3>Edit</h3> <form action="save_edit.php" enctype="multipart/form-data" method="post" name="myForm" />
<table>
<tr>
<td><b>Answer</b></td>
<td><textarea cols="80%" rows="10" name="a_answer"><?php echo $row['$a_answer']; ?></textarea></td>
</tr>
</table> <input name="id" type="hidden" value="<? echo $id; ?>">
<input name="enter" type="submit" value="Edit"> </form>
<?php mysql_close(); ?>
confirm if the question id is integer or varchar in your database. if it is integer then remove the single quotes. ( just a hint may it helps you. ).
One more thing you do not need to put the multipart in your form if you not uploading any files another tip form me.
Two errors in your code: Firstly, you are using row instead of rows. The second is the dollar sign you 're using when accessing the field. The code below should work, provided your database query is correct.
<?php
#data preparation for the query
$id=$_GET['id'];
# selects title and description fields from database
$sql = "SELECT a_answer FROM $tbl_name WHERE question_id='$id'";
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);
?>
<h3>Edit</h3> <form action="save_edit.php" enctype="multipart/form-data" method="post" name="myForm" />
<table>
<tr>
<td><b>Answer</b></td>
<td><textarea cols="80%" rows="10" name="a_answer"><?php echo $rows['a_answer']; ?></textarea></td>
</tr>
</table> <input name="id" type="hidden" value="<? echo $id; ?>">
<input name="enter" type="submit" value="Edit"> </form>
<?php mysql_close(); ?>
Another way to go about things would be to use prepared statements, and let PHP and MySQL quess the correct type. Here can be found a simple example.

Categories