Jump to content

New to PHP - Help regarding edit and delete post. please help.


arjun98854

Recommended Posts

Hi All

 

I was watching a tutorial on how to make a php powered web forum from a website called nettuts. They provided the source code to dowload and try them on my local machine.

My question is I see forums that have edit and delete options on their posts, I wanted to add something alike but I’m so confused to where and how to start the code as I am new to PHP. I am actually basing it on the create_topic.php to make like edit_topic.php and delete_topic.php but i was not succesfull.

Any help would be greatly appreciated.

I am trying to edit the create_topic.php code and trying to make it for edit and delete post. The code for create_topic.php is below. Kindly let me know if i have to provide the complete code of all pages so that it would give a better understanding for all to help me.

 

Thanks a lot..

 

//create_topic.php

 

<?php

//create_topic.php

include 'connect.php';

include 'header.php';

 

echo '<h2>Create a topic</h2>';

if($_SESSION['signed_in'] == false)

{

//the user is not signed in

echo 'Sorry, you have to be <a href="/forum/signin.php">signed in</a> to create a topic.';

}

else

{

//the user is signed in

if($_SERVER['REQUEST_METHOD'] != 'POST')

{

//the form hasn't been posted yet, display it

//this to retrieve the categories from the database

$sql = "SELECT

cat_id,

cat_name,

cat_description

FROM

categories";

 

$result = mysql_query($sql);

 

if(!$result)

{

//the query failed,

echo 'Error while selecting from database. Please try again later.';

}

else

{

if(mysql_num_rows($result) == 0)

{

//there are no categories, so a topic can't be posted

if($_SESSION['user_level'] == 1)

{

echo 'You have not created categories yet.';

}

else

{

echo 'Before you can post a topic, you must wait for an admin to create some categories.';

}

}

else

{

 

echo '<form method="post" action="">

Subject: <input type="text" name="topic_subject" /><br />

Category:';

 

echo '<select name="topic_cat">';

while($row = mysql_fetch_assoc($result))

{

echo '<option value="' . $row['cat_id'] . '">' . $row['cat_name'] . '</option>';

}

echo '</select><br />';

 

echo 'Message: <br /><textarea name="post_content" /></textarea><br /><br />

<input type="submit" value="Create topic" />

</form>';

}

}

}

else

{

//start the transaction

$query  = "BEGIN WORK;";

$result = mysql_query($query);

 

if(!$result)

{

// the query failed and to  quit

echo 'An error occured while creating your topic. Please try again later.';

}

else

{

 

//the form has been posted, so save it

//insert the topic into the topics table first, then save the post into the posts table

$sql = "INSERT INTO

topics(topic_subject,

  topic_date,

  topic_cat,

  topic_by)

  VALUES('" . mysql_real_escape_string($_POST['topic_subject']) . "',

  NOW(),

  " . mysql_real_escape_string($_POST['topic_cat']) . ",

  " . $_SESSION['user_id'] . "

  )";

 

$result = mysql_query($sql);

if(!$result)

{

//something wrong, display the error

echo 'An error occured while inserting your data. Please try again later.<br /><br />' . mysql_error();

$sql = "ROLLBACK;";

$result = mysql_query($sql);

}

else

{

//retrieve the id of the freshly created topic for usage in the posts query

$topicid = mysql_insert_id();

 

$sql = "INSERT INTO

posts(post_content,

  post_date,

  post_topic,

  post_by)

VALUES

('" . mysql_real_escape_string($_POST['post_content']) . "',

  NOW(),

  " . $topicid . ",

  " . $_SESSION['user_id'] . "

)";

$result = mysql_query($sql);

 

if(!$result)

{

//something went wrong, display the error

echo 'An error occured while inserting your post. Please try again later.<br /><br />' . mysql_error();

$sql = "ROLLBACK;";

$result = mysql_query($sql);

}

else

{

$sql = "COMMIT;";

$result = mysql_query($sql);

 

 

echo 'You have succesfully created <a href="topic.php?id='. $topicid . '">your new topic</a>.';

}

}

}

}

}

 

include 'footer.php';

?>

 

[attachment deleted by admin]

it's a bit harder looking at this script cos using edit/delete is different.

 

Basically for the edit you need to create a select query using an id in the where clause and echo the results into a form (probably the same form as creating a topic) then when the user submits the form you need to do an update query using the same id in the where clause.

 

As for the delete, if you have a link/button, you would execute a query to delete from database using the same id in the where clause.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.