MnilinM
03-05-2007, 08:39 PM
Heads up, i did search the forums but never found the answer i was looking for.
I built a site for a friend. Got some scraps of php here and there and smashed them all together to get what i wanted.
Now i got it to the point where he can log in and add something to the site.
I (with help from these forums) got it to where he can input info into mysql and upload a file at the same time, even.
Now i want to make pages where he can edit those postings.
I thought i had a good idea of how this would work, but since it's not, i guess i don't.
I can get it to check he is who he is, i can get it to show the form i want shown, i can get the existing information from mysql to display in the correct fields...
What i can't do is get it to actually update the table.
When i hit submit, it refreshes the page back to the pre-existing information, before the changes were made.
<?php
// connect to db
mysql_connect("localhost","root","password");
mysql_select_db("database");
// start auth
if(isset($_COOKIE['ID_my_site'])) //if_00
{
$username = $_COOKIE['ID_my_site'];
$pass = $_COOKIE['Key_my_site'];
$check = mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error());
while($info = mysql_fetch_array( $check ))
{
// if the cookie has the wrong password, they are taken to the login page
if ($pass != $info['password']) // if_0
{ header("Location: login.php");
}
// otherwise they are shown the admin area
else
{
// if action shows up in the address
if(!isset($action)) //if_1
{
// show fields to edit
$result = mysql_query("select * from news order by news_id desc") or die(mysql_error());
$self = $_SERVER['PHP_SELF'];
echo "<table width=\"75%\" border=1>";
while($row = mysql_fetch_array($result))
{
echo "<tr><td>{$row['news_title']}</td><td>{$row['news_datee']}</td><td><a href=\"$self?action=edit&{$row['news_id']}\">Edit</a></td></tr>";
}
echo "</table>";
} //end if_1
// if action=edit is in the address
if($_GET['action']=="edit" || $_POST['action']=="edit") //if_2
{
// if the submit button hasnt been pressed, show the form
if (!isset($_POST['submit'])) //if_3
{
$id = $_GET['id'];
$sqlq = "SELECT * from news";
$result = mysql_query($sqlq) or die(mysql_error());
$wor = mysql_fetch_array($result);
?>
Admin Area - News - Edit<br><br>
<form action="n_e.php" method="post" enctype="multipart/form-data">
<input type=hidden name="id" id="id" value="<?php echo "{$wor['news_id']}"; ?>">
<table border="1" cellspacing="1" cellpadding="2">
<tr>
<td>Post Title</td>
<td><input name="title" type="text" size="100" id="title" value="<?php echo "{$wor['news_title']}"; ?>"></td>
</tr>
<tr>
<td>Post Image<br>Comment</td>
<td><input name="thcom" type="text" size="100" id="thcom" value="<?php echo "{$wor['news_th_com']}"; ?>"></td>
</tr>
<tr>
<td>Article Text</td>
<td><textarea name="story" rows="20" cols="100" type="text" id="story"><?php echo "{$wor['news_story']}"; ?></textarea></td>
</tr>
<tr>
<td>Name</td>
<td><input name="poster" type="text" size="100" id="poster" value="<?php echo "{$wor['news_poster']}"; ?>"></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
</table>
<input type=hidden name="action" value="edit">
<input type="submit" name="Submit" Value="Submit">
</form>
<?php } // end if_3 ?>
<?php
// when submit is hit, update the post
if ($_POST['submit']) //if_4
{
$id = $_POST['id'];
$title = $_POST['title'];
$thcom = $_POST['thcom'];
$story = $_POST['story'];
$poster = $_POST['poster'];
$sqlqq = "UPDATE news SET news_title='$title', news_th_com='$thcom', news_story='$story', news_poster='$poster' WHERE news_id='$id'";
mysql_query($sqlqq) or die(mysql_error());
mysql_close();
echo "Done";
} //end if_4
} //end_if_2
} //end auth
} //end if_0
} //end if_00
//if the cookie does not exist, they are taken to the login screen
else
{
header("Location: login.php");
}
?>
I threw in a lot of comments to make sure i knew what was going on.
If i have a misunderstanding of any of this, let me know please
I built a site for a friend. Got some scraps of php here and there and smashed them all together to get what i wanted.
Now i got it to the point where he can log in and add something to the site.
I (with help from these forums) got it to where he can input info into mysql and upload a file at the same time, even.
Now i want to make pages where he can edit those postings.
I thought i had a good idea of how this would work, but since it's not, i guess i don't.
I can get it to check he is who he is, i can get it to show the form i want shown, i can get the existing information from mysql to display in the correct fields...
What i can't do is get it to actually update the table.
When i hit submit, it refreshes the page back to the pre-existing information, before the changes were made.
<?php
// connect to db
mysql_connect("localhost","root","password");
mysql_select_db("database");
// start auth
if(isset($_COOKIE['ID_my_site'])) //if_00
{
$username = $_COOKIE['ID_my_site'];
$pass = $_COOKIE['Key_my_site'];
$check = mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error());
while($info = mysql_fetch_array( $check ))
{
// if the cookie has the wrong password, they are taken to the login page
if ($pass != $info['password']) // if_0
{ header("Location: login.php");
}
// otherwise they are shown the admin area
else
{
// if action shows up in the address
if(!isset($action)) //if_1
{
// show fields to edit
$result = mysql_query("select * from news order by news_id desc") or die(mysql_error());
$self = $_SERVER['PHP_SELF'];
echo "<table width=\"75%\" border=1>";
while($row = mysql_fetch_array($result))
{
echo "<tr><td>{$row['news_title']}</td><td>{$row['news_datee']}</td><td><a href=\"$self?action=edit&{$row['news_id']}\">Edit</a></td></tr>";
}
echo "</table>";
} //end if_1
// if action=edit is in the address
if($_GET['action']=="edit" || $_POST['action']=="edit") //if_2
{
// if the submit button hasnt been pressed, show the form
if (!isset($_POST['submit'])) //if_3
{
$id = $_GET['id'];
$sqlq = "SELECT * from news";
$result = mysql_query($sqlq) or die(mysql_error());
$wor = mysql_fetch_array($result);
?>
Admin Area - News - Edit<br><br>
<form action="n_e.php" method="post" enctype="multipart/form-data">
<input type=hidden name="id" id="id" value="<?php echo "{$wor['news_id']}"; ?>">
<table border="1" cellspacing="1" cellpadding="2">
<tr>
<td>Post Title</td>
<td><input name="title" type="text" size="100" id="title" value="<?php echo "{$wor['news_title']}"; ?>"></td>
</tr>
<tr>
<td>Post Image<br>Comment</td>
<td><input name="thcom" type="text" size="100" id="thcom" value="<?php echo "{$wor['news_th_com']}"; ?>"></td>
</tr>
<tr>
<td>Article Text</td>
<td><textarea name="story" rows="20" cols="100" type="text" id="story"><?php echo "{$wor['news_story']}"; ?></textarea></td>
</tr>
<tr>
<td>Name</td>
<td><input name="poster" type="text" size="100" id="poster" value="<?php echo "{$wor['news_poster']}"; ?>"></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
</table>
<input type=hidden name="action" value="edit">
<input type="submit" name="Submit" Value="Submit">
</form>
<?php } // end if_3 ?>
<?php
// when submit is hit, update the post
if ($_POST['submit']) //if_4
{
$id = $_POST['id'];
$title = $_POST['title'];
$thcom = $_POST['thcom'];
$story = $_POST['story'];
$poster = $_POST['poster'];
$sqlqq = "UPDATE news SET news_title='$title', news_th_com='$thcom', news_story='$story', news_poster='$poster' WHERE news_id='$id'";
mysql_query($sqlqq) or die(mysql_error());
mysql_close();
echo "Done";
} //end if_4
} //end_if_2
} //end auth
} //end if_0
} //end if_00
//if the cookie does not exist, they are taken to the login screen
else
{
header("Location: login.php");
}
?>
I threw in a lot of comments to make sure i knew what was going on.
If i have a misunderstanding of any of this, let me know please