/    Sign up×
Community /Pin to ProfileBookmark

Editing data from the data base

Hi I am trying to retrieve data from MySQL database using PHP and and display it to the user who would be able to edit it I can display information from the database but its not editable how can I do that

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@stephan_gerlachSep 13.2006 — you need to connect to the database server
[code=php]
mysql_connect('localhost','username','password');
[/code]


Select the desired database
[code=php]
mysql_select_db('db_name');
[/code]


then run the query like
[code=php]
$sql = 'SELECT * FROM user WHERE id=2 LIMIT 1';
$query = mysql_query($sql);
if (mysql_num_rows($query)==1)
{
$result=mysql_fetch_assoc($query);
}
[/code]


create a form
[code=html]
<form action="update.php" method="post">
<input type="hidden" name="id" value="<?php echo $result['id'];?>" />
Username: <input type="text" name="username" value="<?php echo $result['username'];?>" /><br />
Email: <input type="text" name="email" value="<?php echo $result['email'];?>" /><br />
<input type="submit" value="Update" name="update"/>
</form>
[/code]


and then update the entry
[code=php]
if ( isset ($_POST['update']) )
{

$sql = 'UPDATE user SET username="'.$_POST['username'].'" AND email="'.$_POST['email'].'" WHERE id="'.$_POST['id'].'" LIMIT 1';

mysql_query($sql);
}

[/code]



THESE ARE THE BASICS AND NOT SECURE. CODE IS NOT TESTED. IF YOU USE IT LIKE THAT WITHOUT EDITING ITS YOUR OWN FAULT IF YOU GET HACKED.
Copy linkTweet thisAlerts:
@pcthugSep 13.2006 — Peruse the [url=http://www.php.net/manual/en/ref.mysql.php]MySQL Function set[/url] for tutorials and examples on how to implement a MySQL database with a PHP application. For further insight regarding the MySQL language syntax and datatypes, see the [url=http://dev.mysql.com/doc/]MySQL Online Documentation[/url].
×

Success!

Help @dikats 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,
)...