Update mySQL from update link - php

I have the following link on index page
<td align="center">update</td>
and this is the page it goes to ... Update.php
and here is the code
$id=$_GET['keyword'];
<tr><td> Keyword: </td><td> <input type="text" name="keyword" id="keyword" value="<? echo $id['keyword']; ?>">
I want the keyword to be automatically inserted into the form so that the rest of info can be updated. How can i accomplish this? For some reason this is not working. It is putting the keywork in the URL ?id=KEYWORD on the update page but not displaying in the form. Prob something stupid im sure

You need to either use $_GET['id'] or change the link href to ../athena/admin/update.php?keyword=. Currently, you are not looking for the id parameter, which contains the keyword.

You need to change:
<? echo $id['keyword']; ?>
to:
<?php echo htmlspecialchars($id); ?>
Note:
I used normal php tags instead of short tags, just in case;
I used htmlspecialchars to avoid errors and javascript nastiness; always prepare your data for the medium you are outputting to.

Please check your variable name again.
Here, you pass .php?id= as your GET parameter:
<td align="center">update</td>
In the Update.php, I think you were just misnamed it, it was supposed to be:
$id = $_GET["id"]; //not $_GET["keyword"]
The code below will output all the GET parameter, usually useful to debug in a simple PHP app:
<?php
echo '<pre>';
print_r($_GET);
echo '</pre>';
?>
Hope it helped.
Edit: there is no println in php. Use print_r() or var_dump()

$id=$_GET['keyword'];
<tr><td> Keyword: </td><td> <input type="text" name="keyword" id="keyword" value="<? echo $id; ?>">
May be like this?

Related

Can I echo php value into form?

Is it possible to echo php value into form label? Currently I can only echo into textbox which is editable for the user but I want it to be unable to edit like label, any way I can do so?
Below is for textbox which is able to work
<input style="color:#000000" type="text" value="<?php echo $account['Username']; ?>" name="name" required/>
I tried to echo in label but nothing shown in my web
<label for="fullname"><?php echo $account['Fullname']; ?></label>
Use the echo statement. Like this:
<label><?php echo ("this is a label");?></label>
Of course it is possible.
You can use the echo function like you used it before.
Make sure:
you are using PHP file (*.php)
index in array is not empty
you are using < ? php tags
Try this:
// index.php
<?php
$text = "privet";
?>
<label for="fullname"><?php echo $text; ?></label>
// output is <label for="fullname">privet</label>
read this http://php.net/manual/en/function.echo.php
be careful and read also this http://php.net/manual/en/function.htmlspecialchars.php
BTW: if it still does not working... just use dump to get the variable info
<label><?php echo $var; ?></label> // does not working
// ok -> then dump the var!
<?php var_dump($var); ?> // hmm.. lets see (maybe null or string(0)?) :-)

how remove <td>..</td> when echo-ing value inside text input?

I encountered a very strange issue, that I never seen before. I have a loop and I echo the loop's output without any problems using or other similar tags:
<p></php echo $values[1];?></p>
it works like a charm, but when I try to echo the same value inside a text input strange things start to happen. The output inside an input is wrapped in tags.
<input type="text" value="<?php echo $values[1]; ?>"/>
gives me in result (that's how it looks like in web inspector in Chrome):
<input type="text" value=" <td>2.62</td>">
What did I do wrong??
Based on your comments then replace:
<input type="text" value="<?php echo $values[1]; ?>"/>
.. with:
<input type="text" value="<?php echo trim(strip_tags($values[1])); ?>"/>
It really sounds like a mistake that there can be HTML tags inside your variables and if this should not happen then of course this should be fixed
$values[1] contains the data
<td>2.62</td>
If your example is from your actual code, take notice that your first variable is $values[0] and the input one is $values[1]

Why form displays <? print $_POST['domain']; ?> instead of the empty text field

I have this input field, which is part of a whois search script.
<input type="text" name="domain" value="<? print $_POST['domain']; ?>" />
On the form, instead of displaying an empty text field, it displays <? print $_POST['domain']; ?>.
Could someone please tell me what's going on here?
Thanx in advance.
<? print $_POST['domain']; ?>
Change to
<?php print $_POST['domain']; ?>
Please consider that this way of output is unsafe, if someone can change the variable then its a potential XSS threat.
To prevent this use:
<?php print htmlspecialchars($_POST['domain']); ?>

The php post is returning wrong value

I have wrote the code like this
<?php
echo "Add1:".$_POST['Address1'];
echo "<br>";
echo "Add2:".$_POST['Address2'];
?>
<FORM name="myForm" method='post'>
<table>
<tr>
<td>Address 1</td>
<td><input type="text" name="Address1"></td>
</tr>
<tr>
<td>Address2</td>
<td><input type="text" name="Address2"></td>
</tr>
</table>
<input type="submit">
</FORM>
When enter a value as some thing like this
Address1 = test <test1 and Address2=address2:
But in post i got only the Address1 value in both post variable like.
Add1:test
Add2:test1
Any one can help me.
Debug what you're getting via
<?php print_r($_POST);
and you'll see if it is PHP releated problem or the problem lays somewhere else
Based on your example, it doesn't look like it's not working (Add1 and Add2 are different). What happens if you do a var_dump($_POST)?
If $_POST['Address2'] wasn't filled through the form, it is empty, no output should be displayed.
Try var_dump($_POST); to check the whole POST-array and/or use tools like Firebug, to see which data was sent to the actually.
you can have exact idea by writing this code
<?php
echo "<pre>";print_r($_POST);echo "</pre>";
echo "Add1:".$_POST['Address1'];
echo "<br>";
echo "Add2:".$_POST['Address2'];
?>
that will emable you to check what exactly is coming in $_POST

Deleting a database record using $_POST with Codeigniter

I've been doing it all wrong, I used to take the value from the URI segment and didn't realize it wasn't the ideal way. So I changed my approach and now have everything via a $_POST. I'm not sure if I'm doing this correctly, could someone shed some light? My view contains tabular data listing items pulled from the DB. Each item has two links, "View" and "Delete." The code seems to work but was wondering if it could be coded better. I forgot that the form name wasn't unique, so when I went to go delete a record, it would always delete the newest record (the last hidden field was set).
myview.php (snippet)
<?php foreach($records as $record): ?>
<form method="POST" name="myform<?php echo $location->id;?>" action="/location/delete">
View Delete
<br />
<input type="hidden" name="location_id" value="<?php echo $location->id;?>">
</form>
<?php endforeach ?>
Viewing/Deleting via uri id is perfectly fine, I wouldn't venture to say that using $_POST is wrong, but creating a new unique form for every delete element is terribly messy, and weighed against what you are gaining (no exposed id i guess?), I believe it is more 'correct' to use the uri for delete functions.
If you only want certain people to be able to delete certain records, handle that programmatically in the delete function itself, don't depend on the fact that the request is only sent via $_POST. This is not dependable, anyone can generate a post request.
For anyone who comes across this later, here's how I solved my issue.
In my controller I have a method called delete that checks to see if the form field was submitted via a $_POST. If there's no variable, redirect them somewhere with an error message. If the field was passed, then go through the normal checks to make sure the record can be deleted.
if(!isset($_POST['item_id']))
{
$this->session->set_flashdata('message', 'item cannot be removed!');
redirect("/item");
}
if($this->input->post('item_id')) {
... code ....
... code ....
}
Your syntax error is with this line:
<?php foreach($records as $record): ?>
<form method="POST" name="myform<?php echo $location->id;?>" action="/location/delete">
View <a href="#" onclick="document.myform<?php echo
$location->id;?>.submit();">Delete</a>
<br />
<input type="hidden" name="location_id" value="<?php echo $location->id;?>">
</form>
<?php endforeach ?>
You can not do looping for a form. Instead, use the following code:
<form method="POST" name="myform<?php echo $location->id;?>" action="/location/delete">
<?php foreach($records as $record): ?>
a href="/location/view/<?php echo $location->id;?>">View</a> Delete
<br />
<input type="hidden" name="location_id" value="<?php echo $location->id;?>">
<?php endforeach ?>
</form>

Categories