/    Sign up×
Community /Pin to ProfileBookmark

editing mysql entries(news) using php..

well first of all i made a news system that has an admin page..and the page displayed and an archive page. I enter my news in the admin page, but currently i do not have a way to edit my news posts(only through phpmyadmin). I wanted to kmow how i would go about making a button in the corner of every post that when clicked would expand the table of the current post, and when expanded it would have an input box with the news in it .. then i could modify it and push another button to save it.

and a delete option.

any suggestions?

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@ShrineDesignsJan 31.2005 — <p id="news1">blah, blah,blah...
<div>[ <a href="edit.php?id=1&do=modify">edit</a> | <a href="edit.php?id=1&do=delete">delete</a> ]</div>
</p>
edit.php[code=php]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
$db = mysql_connect('localhost', 'root', '');
mysql_select_db('dbname', $db);

if(isset($_GET['id']) && isset($_GET['do']))
{
?>
<form action="<?php echo "{$_SERVER['PHP_SELF']}?id={$_GET['id']}&amp;do={$_GET['do']}"; ?>" method="post">
<?php
if($_GET['do'] == 'delete')
{
if($_POST && (isset($_POST['delete']) && $_POST['delete'] == 1))
{
$result = mysql_query("DELETE FROM table WHERE id = '{$_GET['id']}'");

if($result !== false)
{
exit("deletion successful");
}
}
?>
<table border="0" cellspacing="5" cellpadding="0">
<tr>
<td nowrap>are you sure you want to delete this?
<input name="delete" type="checkbox" id="delete" value="1"></td>
</tr>
<tr>
<td><input name="submit" type="submit" id="submit" value="delete"></td>
</tr>
</table>
<?php
}
if($_GET['do'] == 'modify')
{
if($_POST && (isset($_POST['news']) && !empty($_POST['news'])))
{
$result = mysql_query("UPDATE table SET news = '{$_POST['news']}' WHERE id = '$_GET['id']'");

if($result !== false)
{
exit("update successful");
}
}
$result = mysql_query("SELECT news FROM table WHERE id = '{$_GET['id']}'");
$news = mysql_result($result, 0);
$news = htmlspecialchars($news);
?>
<table border="0" cellspacing="5" cellpadding="0">
<tr>
<td>news:</td>
<td rowspan="2"><textarea name="news" rows="3" id="news"><?php echo $news; ?></textarea></td>
</tr>
<tr>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input name="submit" type="submit" id="submit" value="modify"></td>
</tr>
</table>
<?php
}
?>
</form>
<?php
}
?>
</body>
</html>
[/code]
Copy linkTweet thisAlerts:
@ilyaauthorJan 31.2005 — this is my post code..



[code=php]

<?php

$db = mysql_pconnect("localhost", "root", "pass");

@mysql_select_db(test) or die( "Unable to select database");

$result = mysql_query("SELECT * FROM plugin ORDER by id DESC LIMIT 10");

$num=mysql_numrows($result);

$i=0;

while ($i < $num) {

$address=mysql_result($result,$i,"address");

$site=mysql_result($result,$i,"site");

$id=mysql_result($result,$i,"id");

$i++;

}

?>
<?

$result = mysql_query("select * from plugin order by id desc limit 10");


while($r=mysql_fetch_array($result))
{

$site=$r["site"];
$address=$r["address"];
?>

<div class="rbroundbox">
<div class="rbtop"><div></div></div>
<div class="entries">


<p id="news1">Article by:<font

color="#CCCCCC"><b>USERNAME</b></font>&nbsp;[ <a

href="edit.php?id=1&do=modify">edit</a> | <a href="edit.php?id=1&do=delete">delete</a> ]

<br><br><? echo $site ?></p>


</div><!-- /rbcontent -->
<div class="rbbot"><div></div></div>
</div><!-- /rbroundbox -->

<br>
<? } ?>
<br>
<?php

$db = mysql_pconnect("localhost", "root", "pass");

@mysql_select_db(test) or die( "Unable to select database");

if(isset($_POST['submit'])) {

$site=$_POST['site'];

$address=$_POST['address'];

$sql = mysql_query("INSERT INTO plugin ( site , address) VALUES ('$site', '$address');

");

echo 'The Article has been sucessfully made.';

}

?>

<br> <form method="post" action="index.php"><br>
Article:<br>
<textarea name="site" height='100' cols="39"></textarea>
<br><br>
<input type="submit" name=submit value=Submit!>
</form>
<br>






<a href='index.php'>INDEX</a>[/code]





now would i have to put the id="news1" into



[code=php]<p id="news variable here?"">Article by:<font

color="#CCCCCC"><b>USERNAME</b></font>&nbsp;[ <a

href="edit.php?id=1&do=modify">edit</a> | <a href="edit.php?id=1&do=delete">delete</a> ]

<br><br><? echo $site ?></p>[/code]


but replace the number with the id variable..so every post doesnt get edited/deleted..

and i got my news field in my mysql db as 'site'..
Copy linkTweet thisAlerts:
@ShrineDesignsJan 31.2005 — [i]Originally posted by ShrineDesigns[/i]&lt;p id="news1"&gt;blah, blah,blah...
&lt;div&gt;[ &lt;a href="edit.php?id=1&amp;amp;do=modify"&gt;edit&lt;/a&gt; | &lt;a href="edit.php?id=1&amp;amp;do=delete"&gt;delete&lt;/a&gt; ]&lt;/div&gt;
&lt;/p&gt;
in the example i use, the urls use a number for the id in the query string
Copy linkTweet thisAlerts:
@ilyaauthorFeb 01.2005 — it gives me this error when i try to edit/delete a news post...

[CODE]
Parse error: parse error, expecting T_STRING' or T_VARIABLE' or `T_NUM_STRING' in C:WEB_ROOThostetedit.php on line 21[/CODE]


everytime i run edit.php.. am i missing sumthing? plz explain better if you can..im a newby
Copy linkTweet thisAlerts:
@ShrineDesignsFeb 01.2005 — i updated the script, i forgot to put {} around a variable inside a string
Copy linkTweet thisAlerts:
@ilyaauthorFeb 01.2005 — there were a few more..

and now when i click delete i get the delete screen but it dont do nothing..

and when i click edit it shows edit with the error bellow and i cannot modify..it just refreshes..



but now it gives me this error

[code=php]Warning: mysql_result(): supplied argument is not a valid MySQL result resource in C:WEB_ROOThostetedit.php on line 52[/code]



EDIT: nvm that was my flaw..forgot to hange the table...

but now if i have post5 and i click edit it edits post 1 ..and same for delete..no matter what post..it always does the first..

because i have

[code=php]<p id="news1">blah, blah,blah...
<div>[ <a href="edit.php?id=1&do=modify">edit</a> | <a href="edit.php?id=1&do=delete">delete</a> ]</div>
</p>[/code]
in my post script..so it posts every post with that link..but sumhow i need a variable from the mysql db to replace id=1 with the id variable from the mysql
Copy linkTweet thisAlerts:
@ShrineDesignsFeb 02.2005 — where you output the rows to display the news or whatever, include the links for editing and deleting in it, and use and put the id of the news item, replacing the 1
Copy linkTweet thisAlerts:
@ilyaauthorFeb 02.2005 — eh..alright.. ill find a way when i get better at php
×

Success!

Help @ilya 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 5.19,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...