Cory94bailly Posted May 23, 2008 Share Posted May 23, 2008 <?php require('includes/config.php'); // Make a MySQL Connection mysql_connect("$path", "$username", "$password") or die(mysql_error()); mysql_select_db("$database") or die(mysql_error()); if(isset($_POST['submit'])) { //Set the variables! $id = basename($_SERVER['PHP_SELF']); $title = mysql_real_escape_string($_POST['title']); $news = mysql_real_escape_string($_POST['news']); $dbtitle = mysql_query("SELECT * FROM news"); $dbtext = ""; $query1 = "UPDATE news SET title = '$title' WHERE id = '$id'"; $query2 = "UPDATE news SET news = '$news' WHERE id = '$id'"; //Update the title and text! mysql_query($query1) or die(mysql_error()); mysql_query($query2) or die(mysql_error()); } ?> <form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post"> <p>Title:<br> <textarea rows="1" cols="20" name="title"><? $dbtitle ?></textarea></p> <p>Text:<br> <textarea rows="10" cols="50" name="news"><? $dbtext ?></textarea></p> <input type="submit" name="submit" value="Submit"> </form> <? echo "ID = $id"; echo "<br>"; echo "TITLE = $title"; echo "<br>"; echo "NEWS = $news"; echo "<br>"; echo "DBTITLE = $dbtitle"; echo "<br>"; echo "DBTEXT = $dbtext"; ?> Well there's my WHOLE code.. I am having lots of problems.. 1.) I don't know how to get it to show "ID = " as something else.. Let me explain. If I am at http://xxx.com/page.php?id=4 I want it to only show the number 4 as ID = ... 2.) I don't know how to word it but maybe looking at my code will help. Quote Link to comment https://forums.phpfreaks.com/topic/107004-solved-confusing-me/ Share on other sites More sharing options...
pocobueno1388 Posted May 23, 2008 Share Posted May 23, 2008 Instead of this line: $id = basename($_SERVER['PHP_SELF']); I think your wanting this: $id = $_GET['id']; Quote Link to comment https://forums.phpfreaks.com/topic/107004-solved-confusing-me/#findComment-548495 Share on other sites More sharing options...
wildteen88 Posted May 23, 2008 Share Posted May 23, 2008 Just looking at your code why use two queries to update the news title/news content in two queries: $query1 = "UPDATE news SET title = '$title' WHERE id = '$id'"; $query2 = "UPDATE news SET news = '$news' WHERE id = '$id'"; //Update the title and text! mysql_query($query1) or die(mysql_error()); mysql_query($query2) or die(mysql_error()); Use just one: //Update the title and text! $query = "UPDATE news SET title = '$title', news = '$news' WHERE id = '$id'"; mysql_query($query) or die(mysql_error()); To get the id from the url use $_GET['id'] Quote Link to comment https://forums.phpfreaks.com/topic/107004-solved-confusing-me/#findComment-548500 Share on other sites More sharing options...
Cory94bailly Posted May 23, 2008 Author Share Posted May 23, 2008 I just went to http://mypage.com/update.php?id=76 and I clicked "Submit" and it just showed blank for ID... Quote Link to comment https://forums.phpfreaks.com/topic/107004-solved-confusing-me/#findComment-548508 Share on other sites More sharing options...
wildteen88 Posted May 23, 2008 Share Posted May 23, 2008 That is because it'll only work if you submit the form. YOu'll need to change: if(isset($_POST['submit'])) { //Set the variables! $id = basename($_SERVER['PHP_SELF']); to: $id = (isset($_GET['id']) && is_numeric($_GET['id'])) ? $_GET['id'] : null; if(isset($_POST['submit'])) { Quote Link to comment https://forums.phpfreaks.com/topic/107004-solved-confusing-me/#findComment-548513 Share on other sites More sharing options...
Cory94bailly Posted May 24, 2008 Author Share Posted May 24, 2008 <?php require('includes/config.php'); // Make a MySQL Connection mysql_connect("$path", "$username", "$password") or die(mysql_error()); mysql_select_db("$database") or die(mysql_error()); $id = (isset($_GET['id']) && is_numeric($_GET['id'])) ? $_GET['id'] : null; if(isset($_POST['submit'])) { $title = mysql_real_escape_string($_POST['title']); $news = mysql_real_escape_string($_POST['news']); $dbtitle = mysql_query("SELECT * FROM news"); $dbtext = ""; //Update it! $query = "UPDATE news SET title = '$title', news = '$news' WHERE id = '$id'"; mysql_query($query) or die(mysql_error()); } ?> <form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post"> <p>Title:<br> <textarea rows="1" cols="20" name="title"><? $dbtitle ?></textarea></p> <p>Text:<br> <textarea rows="10" cols="50" name="news"><? $dbtext ?></textarea></p> <input type="submit" name="submit" value="Submit"> </form> <? echo "ID = $id"; echo "<br>"; echo "TITLE = $title"; echo "<br>"; echo "NEWS = $news"; echo "<br>"; echo "DBTITLE = $dbtitle"; echo "<br>"; echo "DBTEXT = $dbtext"; ?> There's my code... How do I get it to show the title and text of a specific post? If I could know how to go to http://myside.com/file.php?id=63 and it would show the mysql's 63rd id field so I could freely edit it.. If someone could show me how to do that stuff, I would be pretty much done... Quote Link to comment https://forums.phpfreaks.com/topic/107004-solved-confusing-me/#findComment-548646 Share on other sites More sharing options...
wildteen88 Posted May 24, 2008 Share Posted May 24, 2008 <?php require 'includes/config.php'; // Make a MySQL Connection mysql_connect($path, $username, $password) or die(mysql_error()); mysql_select_db($database) or die(mysql_error()); if(isset($_POST['submit'])) { if(is_numeric($_POST['id'])) { $id = $_POST['id']; $title = mysql_real_escape_string($_POST['title']); $news = mysql_real_escape_string($_POST['news']); //Update it! $query = "UPDATE news SET title = '$title', news = '$news' WHERE id = '$id'"; mysql_query($query) or die(mysql_error()); } else { die( 'invalid action' ); } } elseif(isset($_GET['id']) && is_numeric($_GET['id'])) { $id = $_GET['id']; $query = "SELECT title, news FROM news WHERE id='$id'"; $result = mysql_query($query); list($news_title, $news_text) = mysql_fetch_row($result); ?> <form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post"> <p>Title:<br> <input type="text" name="title" value="<?php echo $news_title ?>" /> </p> <p>Text:<br> <textarea rows="10" cols="50" name="news"><?php echo $news_text ?></textarea> </p> <input type="hidden" name="id" value="<?php echo $id; ?>"> <input type="submit" name="submit" value="Submit"> </form> <?php } ?> Quote Link to comment https://forums.phpfreaks.com/topic/107004-solved-confusing-me/#findComment-548821 Share on other sites More sharing options...
Cory94bailly Posted May 24, 2008 Author Share Posted May 24, 2008 Yayyyyyy!!! Wow, that was a smaller code than I thought it would have to be... Now, how can I get it so if I go to http://mysite.com/file.php, it will display all news but with a button next to them saying something like "Delete" and if I click it, it goes to http://mysite.com/file.php?id=IDHERE automatically? Quote Link to comment https://forums.phpfreaks.com/topic/107004-solved-confusing-me/#findComment-548978 Share on other sites More sharing options...
Cory94bailly Posted May 24, 2008 Author Share Posted May 24, 2008 Yayyyyyy!!! Wow, that was a smaller code than I thought it would have to be... Now, how can I get it so if I go to http://mysite.com/file.php, it will display all news but with a button next to them saying something like "Delete" and if I click it, it goes to http://mysite.com/file.php?id=IDHERE automatically? Well here's what I have now to display news.. <?php require('includes/config.php'); // Make a MySQL Connection mysql_connect("$path", "$username", "$password") or die(mysql_error()); mysql_select_db("$database") or die(mysql_error()); $query1 = "SELECT * FROM news ORDER BY sticky, id ASC"; $result1 = mysql_query($query1) or die(mysql_error()); function printNews($result) { if ( mysql_num_rows($result) < 1 ) { echo "<h1>No news!</h1>"; } else { echo "<table>\n"; while($news = mysql_fetch_assoc($result)) { $sticky = ($news['sticky'] == 'y') ? "<font color='red'><u>Announcement:</u></font> " : ''; echo <<<HTML <tr> <th align="left">{$sticky}<b>{$news['title']}</b></th> </tr> <tr> <td>{$news['news']}</td> </tr> HTML; } echo "\n</table><br />"; } } printNews($result1); // Non-sticky News ?> I don't know how to get it to show a edit button next to it.. Also, I want to know how to (if possibly) how to put a delete button near each... Quote Link to comment https://forums.phpfreaks.com/topic/107004-solved-confusing-me/#findComment-549098 Share on other sites More sharing options...
wildteen88 Posted May 24, 2008 Share Posted May 24, 2008 try: <?php require 'includes/config.php'; // Make a MySQL Connection mysql_connect($path, $username, $password) or die(mysql_error()); mysql_select_db($database) or die(mysql_error()); $query1 = "SELECT * FROM news ORDER BY sticky, id ASC"; $result1 = mysql_query($query1) or die(mysql_error()); function printNews($result) { if ( mysql_num_rows($result) < 1 ) { echo "<h1>No news!</h1>"; } else { echo "<table cellspacing=\"2\" cellpadding=\"5\">\n"; while($news = mysql_fetch_assoc($result)) { $sticky = ($news['sticky'] == 'y') ? "<font color='red'><u>Announcement:</u></font> " : ''; echo <<<HTML <tr> <th align="left">{$sticky}<b>{$news['title']}</b></th> <td rowspan="2" valign="bottom" align="left"> <a href="news_edit.php?id={$news['id']}">Edit</a><br /> <a href="news_delete.php?id={$news['id']}">Delete</a> </td> </tr> <tr> <td>{$news['news']}</td> </tr> HTML; } echo "\n</table><br />"; } } printNews($result1); ?> Note: make sure you change news_edit.php and news_delete.php to the correct page which edits/deletes the news. Quote Link to comment https://forums.phpfreaks.com/topic/107004-solved-confusing-me/#findComment-549186 Share on other sites More sharing options...
Cory94bailly Posted May 24, 2008 Author Share Posted May 24, 2008 I don't yet have one that deletes... Mind helping me? xD Quote Link to comment https://forums.phpfreaks.com/topic/107004-solved-confusing-me/#findComment-549224 Share on other sites More sharing options...
wildteen88 Posted May 25, 2008 Share Posted May 25, 2008 Its basically the same as your add/edit pages. Accept this time you run a DELETE query, eg: DELETE FROM news where id=$id Quote Link to comment https://forums.phpfreaks.com/topic/107004-solved-confusing-me/#findComment-549462 Share on other sites More sharing options...
Cory94bailly Posted May 25, 2008 Author Share Posted May 25, 2008 Here's my code: <?php ob_start(); require 'config.php'; // Make a MySQL Connection mysql_connect($path, $username, $password) or die(mysql_error()); mysql_select_db($database) or die(mysql_error()); //Finds out the ID if(is_numeric($_POST['id'])) { //Set the variable! $id = $_POST['id']; //Delete it! $query = "DELETE FROM news where id=$id"; mysql_query($query) or die(mysql_error()); echo "<meta http-equiv='refresh' content='1;url=index.php'> ID $id deleted!"; } else { //Error message. die('Invalid action'); } //End it! ob_end_flush(); ?> And I am just getting "Invalid action." Quote Link to comment https://forums.phpfreaks.com/topic/107004-solved-confusing-me/#findComment-549695 Share on other sites More sharing options...
wildteen88 Posted May 25, 2008 Share Posted May 25, 2008 How is the id coming to the script? If its via the url then you'd use $_GET['id'] rather than $_POST['id'] $_POST is used when getting data from a form. $_GET returns data in the url. Quote Link to comment https://forums.phpfreaks.com/topic/107004-solved-confusing-me/#findComment-549750 Share on other sites More sharing options...
Cory94bailly Posted May 25, 2008 Author Share Posted May 25, 2008 How is the id coming to the script? If its via the url then you'd use $_GET['id'] rather than $_POST['id'] $_POST is used when getting data from a form. $_GET returns data in the url. Never mind I fixed it.. Quote Link to comment https://forums.phpfreaks.com/topic/107004-solved-confusing-me/#findComment-549777 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.