Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP/Mysql Create thread then redirect to forum category
    primarykey
    data
    text
    <p>Basically I have edited the "new_topic" page in my generic forum so that the user can select the category of the post from a drop down list, rather than goto the category first and going to new topic, what I am trying to do now is when the thread is created, rather than jumping to the standard "your new thread was created here" it redirect to the category the topic was made under.</p> <p>I tried using</p> <pre><code>header( "Location: category.php?id='. $topic_cat . '" ); </code></pre> <p>But header was already being used elsewhere. So I found a meta refresh that works to an extent..</p> <pre><code>echo "&lt;META HTTP-EQUIV='Refresh' Content='0; URL=/category.php?id='. $topic_cat .'&gt;"; </code></pre> <p>But it redirects to "category.phpid=" and outputs the following message.. <strong>The category could not be displayed, please try again later.You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 8</strong></p> <p>Is there anyway I can make the page jump to the category that is only being defined in the submit function itself? Thanks for any help.</p> <p>EDIT: would using "if" functions work to define the redirect? ie. if topic_cat 2 is selected in the form redirect to category.phpid=2?? Sorry I am very new to PHP and still trying to get my head around it all :( Here is the topic creation page in it's entirety</p> <pre><code>&lt;?php //create_topic.php include 'connect.php'; if($_SESSION['signed_in'] == false) { //the user is not signed in echo 'Sorry, you have to be &lt;a href="/forum/signin.php"&gt;signed in&lt;/a&gt; to create a topic.'; } else { //the user is signed in if($_SERVER['REQUEST_METHOD'] != 'POST') { //the form hasn't been posted yet, display it //retrieve the categories from the database for use in the dropdown $sql = "SELECT cat_id, cat_name, cat_description FROM categories"; $result = mysql_query($sql); if(!$result) { //the query failed, uh-oh :-( 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 '&lt;form method="post" action=""&gt;'; echo '&lt;select name="topic_cat"&gt;'; while($row = mysql_fetch_assoc($result)) { echo '&lt;option value="' . $row['cat_id'] . '"&gt;' . $row['cat_name'] . '&lt;/option&gt;'; } echo '&lt;/select&gt;&lt;br /&gt;'; echo '&lt;textarea name="post_content" /&gt;&lt;/textarea&gt;&lt;br /&gt;&lt;br /&gt; &lt;input type="submit" value="Create topic" /&gt; &lt;/form&gt;'; } } } else { //start the transaction $query = "BEGIN WORK;"; $result = mysql_query($query); if(!$result) { //Damn! the query failed, 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 we'll 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['post_content']) . "', NOW(), " . mysql_real_escape_string($_POST['topic_cat']) . ", " . $_SESSION['user_id'] . " )"; $result = mysql_query($sql); if(!$result) { //something went wrong, display the error echo 'An error occured while inserting your data. Please try again later.&lt;br /&gt;&lt;br /&gt;' . mysql_error(); $sql = "ROLLBACK;"; $result = mysql_query($sql); } else { //the first query worked, now start the second, posts query //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.&lt;br /&gt;&lt;br /&gt;' . mysql_error(); $sql = "ROLLBACK;"; $result = mysql_query($sql); } else { $sql = "COMMIT;"; $result = mysql_query($sql); echo "&lt;META HTTP-EQUIV='Refresh' Content='0; URL=/category.php?id=". $topic_cat ."'&gt;"; } } } } } ; ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload