/    Sign up×
Community /Pin to ProfileBookmark

Edit database

So I want to edit a row from the database through a HTML-form. This is my code so far, but I’m stuck.
I get ofcourse “Undefined variable: newMessage”, but I’m not sure how to solve it.

[code=php]
<?php
if(isset($_POST[‘submit’]))
{
if(isset($_SESSION[’email’])) {
$name = $_SESSION[’email’];
$id = intval($_GET[‘id’]);
$newMessage = $_POST[‘newMessage’];

$sql = “UPDATE posts SET message=’$newMessage’ WHERE id = $id AND name = ‘$name'”;
$result = mysqli_query($db, $sql) or die(‘SQL-error’);

}
}

?>

<form action=”.” method=”POST”>
Message:
<input type=”text” name=”newMessage” value=”<?php echo $newMessage; ?>”>
<input type=”submit” name=”submit” value=”Update”>
</form>[/code]

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@ginerjmNov 16.2017 — I am assuming that you do have error checking enabled and that is where this message is coming from. What line number did it point to? Perhaps you could point it out?

PS - I've not seen your action usage before. Do you really need that dot in the action attribute of the form tag? When people don't specify an exact script name there I usually see it left blank or completely out.
Copy linkTweet thisAlerts:
@NifelauthorNov 16.2017 — I am assuming that you do have error checking enabled and that is where this message is coming from. What line number did it point to? Perhaps you could point it out?

PS - I've not seen your action usage before. Do you really need that dot in the action attribute of the form tag? When people don't specify an exact script name there I usually see it left blank or completely out.[/QUOTE]


The error refers to <input type="text" name="newMessage" value="<?php echo $newMessage; ?>">

And yes, you're right about the form action. Not sure why there is a dot there.
Copy linkTweet thisAlerts:
@ginerjmNov 16.2017 — Silly me!

I should have seen it. Ok - think about what your script is doing. You wisely check right off the bat whether or not you DO have a form submission. Many people don't do that and wonder why things are going screwy. And I noticed that. So now - think about what you script does next. It recognizes that there is no form data to process and then proceeds to do the form output for the user to provide input, correct? But - you are attempting to send a value to the client which you clearly have not yet defined. Initialize that value when there is no input to process so that it exists to send the blank form to the client.
Copy linkTweet thisAlerts:
@NogDogNov 16.2017 — Yeah, simplest fix might be to just initialize that and any other variables to whatever default value (including "") you want if the conditions aren't met.
[code=php]
<?php
$name = '';
$id = '';
$newmessage = '';
if(isset($_POST['submit']))
{
[/code]
Copy linkTweet thisAlerts:
@benanamenNov 16.2017 — You wisely check right off the bat whether or not you DO have a form submission.[/QUOTE]

Except that he is doing it wrong. Depending on a button to be submitted in order for the script to work will completely fail in certain cases.

OP, you need to check the Request Method
[code=php]if ($_SERVER['REQUEST_METHOD'] == 'POST')[/code]

Do not create variables for nothing.

Never ever put variables in your query. You need to use prepared statements. I recommend you use PDO

Completely remove the form action
×

Success!

Help @Nifel spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...