Ippox Posted March 7, 2011 Share Posted March 7, 2011 Hi. I'm making a private webpage for my self where i can post news, games, music titles and movies. I'm made it so that i can post and delete them from my page. Also i wanted to have the option to edit them. So i made a action that shows me a list og the posts in each category. And then i want the option to hit UPDATE, and then it should open the form with the info's from MySQL. I've made those two options as the following: elseif ( $action == "EditNews" ) { // EDIT NEWS START if ( !$_GET['id'] ) { ?> <div id="content"> <?php $sql = mysql_query("SELECT * FROM news") or die(mysql_error()); while ( $row = mysql_fetch_assoc($sql) ) { echo "<a href=\"?page=Admin&action=UpdateNews&id=" . $row['news_id'] . "\">Update</a> | " . $row['subject'] . "<br>"; } echo "</div>"; } else { $type = $_GET['type']; $id = $_GET['id']; } // EDIT NEWS END } elseif ( $action == "UpdateNews" ) { // UPDATE NEWS START if ( !$_POST['submit'] ) { $id = $_GET['id']; $sql = mysql_query("SELECT * FROM news WHERE news_id='$id'") or die(mysql_error()); $user_id = $_SESSION['uid']; $subject = $_row['subject']; $body = nl2br($_row['body']); ?> <div id="content"> <form action="?page=Admin&action=EditNews" method="post"> <div id="">Subject</div> <div id=""><input id="input" type="text" name="subject"></div> <div id="">Message</div> <div id=""><textarea id="input" name='body' cols='15' rows='4'></textarea></div> <div id=""><input id="submit" class="input" type="submit" name="submit" value="Post News"></div> </form> </div> <?php } else { ?> <div id="content">Not Working!</div> <?php } // UPDATE NEWS END } - I know i miss something, but i've tried to get to the point, where i only have the form as blank, and need some help from there. I hope someone can help me with my problem. MOD EDIT: . . . tags added. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted March 7, 2011 Share Posted March 7, 2011 Well, we'd love to help you with your problem, but you haven't really told us what it actually is . . . Also, when posting code, please enclose it within the forum's . . . BBCode tags. Quote Link to comment Share on other sites More sharing options...
Ippox Posted March 7, 2011 Author Share Posted March 7, 2011 The problem is that i want to get the MySQL data from that specific post, into the form. And i know that i have to use <?php echo " .$row['subject'] . " ?> as value of the subject input. And <?php echo " .$row['body'] . " ?> in the textarea. But when i add it there, the page goes blank. and it doesn't give any errors at all either. So my problem is that i want my action "UpdatePost" to be able to let my form appear with the informations from the posted informations from MySQL. But i can't seems to figure out what i need to do. Quote Link to comment Share on other sites More sharing options...
flolam Posted March 7, 2011 Share Posted March 7, 2011 before you can use $row["column"] you have to use mysql_fetch_assoc(result of mysql_query), which will give you an array of your result. You are already doing this in the while loop you use to display the link, but not for the form. Thus, you should insert $row_to_update = mysql_fetch_assoc($sql); after this $sql = mysql_query("SELECT * FROM news WHERE news_id='$id'") or die(mysql_error()); and then fill the form like this echo $row_to_update[column_name]; Quote Link to comment Share on other sites More sharing options...
Ippox Posted March 7, 2011 Author Share Posted March 7, 2011 Just tried it. So my action for UpdateNews looks like this: elseif ( $action == "UpdateNews" ) { // UPDATE NEWS START if ( !$_POST['submit'] ) { $id = $_GET['id']; $row_to_update = mysql_fetch_assoc($sql); $sql = mysql_query("SELECT * FROM news WHERE news_id='$id'") or die(mysql_error()); ?> <div id="content"> <form action="?page=Admin&action=EditNews" method="post"> <div id="">Subject</div> <div id=""><input id="input" type="text" name="subject" value='<?php echo " $row_to_update[subject] "; ?>'></div> <div id="">Message</div> <div id=""><textarea id="input" name='body' cols='15' rows='4'></textarea><?php echo " $row_to_update[body] "; ?></div> <div id=""><input id="submit" class="input" type="submit" name="submit" value="Post News"></div> </form> </div> <?php } else { ?> <div id="content"> Hmm.. Noget fungere ikke som det skal! </div> <?php } // UPDATE NEWS END } And now it can find the form. But its still blank. Its like it can't get the information from my MySQL database. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted March 7, 2011 Share Posted March 7, 2011 When you say the page is blank, do you mean there is nothing at all displayed, or that the fields simply don't have values? In your php.ini file, do you have error reporting on? error_reporting = -1 display_errors = On Quote Link to comment Share on other sites More sharing options...
flolam Posted March 7, 2011 Share Posted March 7, 2011 look at my post: you need to insert mysql_fetch_assoc AFTER you do mysql_query Quote Link to comment Share on other sites More sharing options...
Ippox Posted March 7, 2011 Author Share Posted March 7, 2011 Ohh.. Changed it, and now it works Now i only have to check if i can "submit" the post successfully. Can you guys double check for me, if its correct? I will be able to use the "submit" function again.. right? Quote Link to comment Share on other sites More sharing options...
Ippox Posted March 7, 2011 Author Share Posted March 7, 2011 Okay.. After tried a bit, i keep getting this error when pressing on "Update Post": 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 '('1', 'Testing', 'Test', 'Now()')' at line 1 My following code is the following: elseif ( $action == "UpdateNews" ) { // UPDATE NEWS START if ( !$_POST['submit'] ) { $id = $_GET['id']; $sql = mysql_query("SELECT * FROM news WHERE news_id='$id'") or die(mysql_error()); $row_to_update = mysql_fetch_assoc($sql); ?> <div id="content"> <form action="?page=Admin&action=UpdateNews" method="post"> <div id="">Subject</div> <div id=""><input id="input" type="text" name="subject" value='<?php echo"$row_to_update[subject]"; ?>'></div> <div id="">Message</div> <div id=""><textarea id="input" name='body' cols='15' rows='4'><?php echo"$row_to_update[body]"; ?></textarea></div> <div id=""><input id="submit" class="input" type="submit" name="submit" value="Update News"></div> </form> </div> <?php } else { $user_id = $_SESSION['uid']; $subject = $_POST['subject']; $body = nl2br($_POST['body']); $errors = array(); if ( !$user_id ) { $errors[1] = "You are not logged in."; } if ( !$subject ) { $errors[2] = "You did not type a subject."; } if ( !$body ) { $errors[3] = "You did not type a message."; } if ( count($errors) > 0 ) { echo "<ul>"; foreach ( $errors as $error ) { echo "<li>"; echo $error; echo "</li>"; } echo "</ul>"; } else { ?> <div id="content"> <?php $sql = mysql_query("UPDATE news SET('$user_id', '$subject', '$body', 'Now()')") or die(mysql_error()); echo "You have updated the news successfully!"; echo "<br/>"; echo "<a href=\"?page=Admin&action=EditNews\">Update More</a>"; } echo "</div>"; ?> <?php } // UPDATE NEWS END } Can anyone see where my problem is? Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted March 7, 2011 Share Posted March 7, 2011 That's the wrong syntax for an UPDATE query. Quote Link to comment Share on other sites More sharing options...
Ippox Posted March 7, 2011 Author Share Posted March 7, 2011 I've changed it a bit. And it reacts to the Update Post botton. Except that it doesn't UPDATE the changes. What have i done wrong? elseif ( $action == "UpdateNews" ) { // UPDATE NEWS START if ( !$_POST['submit'] ) { $id = $_GET['id']; $sql = mysql_query("SELECT * FROM news WHERE news_id='$id'") or die(mysql_error()); $row = mysql_fetch_assoc($sql); ?> <div id="content"> <form action="?page=Admin&action=UpdateNews" method="post"> <div id="">Subject</div> <div id=""><input id="input" type="text" name="subject" value='<?php echo"$row[subject]"; ?>'></div> <div id="">Message</div> <div id=""><textarea id="input" name='body' cols='15' rows='4'><?php echo"$row[body]"; ?></textarea></div> <div id=""><input id="submit" class="input" type="submit" name="submit" value="Update News"></div> </form> </div> <?php } else { $user_id = $_SESSION['uid']; $subject = $_POST['subject']; $body = ($_POST['body']); $errors = array(); if ( !$user_id ) { $errors[1] = "You are not logged in."; } if ( !$subject ) { $errors[2] = "You did not type a subject."; } if ( !$body ) { $errors[3] = "You did not type a message."; } if ( count($errors) > 0 ) { echo "<ul>"; foreach ( $errors as $error ) { echo "<li>"; echo $error; echo "</li>"; } echo "</ul>"; } else { ?> <div id="content"> <?php $sql = mysql_query("UPDATE news SET user_id='$user_id', subject='$subject', body='$body', date='Now()' WHERE news_id='$id'") or die(mysql_error()); echo "You have updated the news successfully!"; echo "<br/>"; echo "<a href=\"?page=Admin&action=EditNews\">Update More</a>"; } echo "</div>"; ?> <?php } // UPDATE NEWS END } Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted March 7, 2011 Share Posted March 7, 2011 Remove the query string from the query execution and assign it to a variable, then use the variable in the execution. Echo the query string and mysql_error() if the query fails. $query = "UPDATE query here"; if( $result = mysql_query($query) ) { if( mysql_affected_rows() < 1 ) { echo 'Number of rows updated is 0!'; } } else { echo "<br>Query: $query<br>Failed with error: " . mysql_error() . '<br>'; } Quote Link to comment Share on other sites More sharing options...
Ippox Posted March 8, 2011 Author Share Posted March 8, 2011 You don't have a more simple answer on whats wrong? Can't you see anything that needs to be changed? Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted March 8, 2011 Share Posted March 8, 2011 If you can't be bothered to put in a bit of effort to at least get some error output for debugging, then I doubt anyone will bother with helping you. Quote Link to comment Share on other sites More sharing options...
Ippox Posted March 8, 2011 Author Share Posted March 8, 2011 Thanks for your reply.. Its working now. I tried to echo out the $sql and noticed that it didn't get the used news_id to update. So it couldn't figure out where to UPDATE the infos. Anyways, the problem was that no variable was set for the news_id as in the POST action.. So now its working smoothly. Thanks everyone for your help Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.