alen Posted November 29, 2006 Share Posted November 29, 2006 I started reading this tutorial about how to make a simple news management script with php and mysql.I'm new to all this php related stuff.. so could one of you guys help me with some errors?I have this file called edit_process.php where I'm supposed to modifiy the news that I added. So I open the page and there I find beautiful errors which I tried to fix, but failed :\Here's the error;[b]Notice: Undefined index: id in C:\Program Files\Server\public_html\nyheter\edit_process.php on line 7Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\Program Files\Server\public_html\nyheter\edit_process.php on line 9[/b]Here's the code;[b]<?phpinclude "config.php"; $db = mysql_connect($db_host,$db_user,$db_pass); mysql_select_db ($db_name) or die ("Cannot connect to database"); $query = "SELECT title, news FROM news WHERE id = $_GET[id]"; $result = mysql_query($query); while($r=mysql_fetch_array($result)) { $title=$r["title"]; $news=$r["news"]; echo "<form name='edit_process.php' method='post' action='edit_save.php?id=$_GET[id]'> <p>Title : <input type='text' name='title' value='$title'> </p> <p>News :</p> <p> <textarea name='news' cols='40' rows='6'>$news</textarea> </p> <p> <input type='submit' name='Submit' value='Save'> </p> </form>"; } mysql_close($db); ?> [/b]Sincerely,anigma Link to comment https://forums.phpfreaks.com/topic/28805-news-management/ Share on other sites More sharing options...
bqallover Posted November 29, 2006 Share Posted November 29, 2006 Are you sure $_GET[id] contains a value?Maybe it's not being passed into the script, e.g. edit_process.php?id=22, from the URL?Or is it being called with POST data from a form or script? In that case you'd need $_POST[id] instead. Link to comment https://forums.phpfreaks.com/topic/28805-news-management/#findComment-131883 Share on other sites More sharing options...
sw0o0sh Posted November 29, 2006 Share Posted November 29, 2006 [code]<?phpinclude('config.php');$db=mysql_connect($db_host,$db_user,$db_pass); mysql_select_db ($db_name) or die ("Cannot connect to database"); if($_GET){ echo "<!-- Your query was selected with a get method -->";$query=mysql_query("SELECT * FROM news WHERE id=$_GET[id]");}else if($_POST){ echo "<!-- Your query was selected with a post method -->";$query=mysql_query("SELECT * FROM news WHERE id=$_POST[id]");}while($r=mysql_fetch_array($result)) { echo "<form name='edit_process.php' method='post' action='edit_save.php?id=".$_GET[id]."'> <p>Title : <input type='text' name='title' value='".$r[title]."' /> </p> <p>News :</p> <p> <textarea name='news' cols='40' rows='6'>".$r[news]."</textarea> </p> <p> <input type='submit' name='Submit' value='Save' /> </p> </form>";}?>[/code]Try that. Notice I made the query a tad more dynamic. You can undo that if you want, but use it now to find out whether you are using a post or get method to find the query. Link to comment https://forums.phpfreaks.com/topic/28805-news-management/#findComment-131894 Share on other sites More sharing options...
alen Posted November 29, 2006 Author Share Posted November 29, 2006 I got this,[b]Notice: Undefined variable: result in C:\Program Files\Server\public_html\nyheter\edit_process.php on line 13Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\Program Files\Server\public_html\nyheter\edit_process.php on line 13[/b]Take a look at the whole script, http://www.oxyscripts.com/item-408.html. Link to comment https://forums.phpfreaks.com/topic/28805-news-management/#findComment-132055 Share on other sites More sharing options...
sws Posted November 29, 2006 Share Posted November 29, 2006 Set your error handling to this and you won't have those probs anymore.Place this snippet of code near the top of your page inside the opening <? tag[code]error_reporting (E_ALL & ~ E_NOTICE); # Don't show notices.[/code] Link to comment https://forums.phpfreaks.com/topic/28805-news-management/#findComment-132056 Share on other sites More sharing options...
alen Posted November 29, 2006 Author Share Posted November 29, 2006 ah.now I only have this error;[b]Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\Program Files\Server\public_html\nyheter\edit_process2.php on line 10[/b] Link to comment https://forums.phpfreaks.com/topic/28805-news-management/#findComment-132072 Share on other sites More sharing options...
sws Posted November 29, 2006 Share Posted November 29, 2006 Looks like a bunch of quotes are missingTry this :[code]<?phpinclude('config.php');$db=mysql_connect($db_host,$db_user,$db_pass); mysql_select_db ($db_name) or die ("Cannot connect to database"); if($_GET){ echo "<!-- Your query was selected with a get method -->";$query=mysql_query("SELECT * FROM news WHERE id=$_GET['id']");}else if($_POST){ echo "<!-- Your query was selected with a post method -->";$query=mysql_query("SELECT * FROM news WHERE id=$_POST['id']");}while($r=mysql_fetch_array($result)) { echo "<form name='edit_process.php' method='post' action='edit_save.php?id=".$_GET['id']."'> <p>Title : <input type='text' name='title' value='".$r['title']."' /> </p> <p>News :</p> <p> <textarea name='news' cols='40' rows='6'>".$r['news']."</textarea> </p> <p> <input type='submit' name='Submit' value='Save' /> </p> </form>";}?>[/code] Link to comment https://forums.phpfreaks.com/topic/28805-news-management/#findComment-132075 Share on other sites More sharing options...
alen Posted November 29, 2006 Author Share Posted November 29, 2006 Well, now I get this;Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\Program Files\Server\public_html\nyheter\edit_process.php on line 7 Link to comment https://forums.phpfreaks.com/topic/28805-news-management/#findComment-132130 Share on other sites More sharing options...
chriscloyd Posted November 29, 2006 Share Posted November 29, 2006 try this[CODE]<?phpinclude('config.php');$db=mysql_connect($db_host,$db_user,$db_pass); mysql_select_db ($db_name) or die ("Cannot connect to database"); if(isset($_GET['id'])) {$id = $_GET['id'];echo "<!-- Your query was selected with a get method -->";$query=mysql_query("SELECT * FROM news WHERE id='$id'") or die(mysql_error());}if(isset($_POST['id'])){$id = $_POST['id'];echo "<!-- Your query was selected with a post method -->";$query=mysql_query("SELECT * FROM news WHERE id='$id'") or die(mysql_error());}while($r=mysql_fetch_array($query)) {$nid = $r['id']$title = $r['title'];$news = $r['news'] echo "<form name='edit_process.php' method='post' action='edit_save.php?id=".$nid."'> <p>Title : <input type='text' name='title' value='".$title."' /> </p> <p>News :</p> <p> <textarea name='news' cols='40' rows='6'>".$news."</textarea> </p> <p> <input type='submit' name='Submit' value='Save' /> </p> </form>";}?>[/CODE] Link to comment https://forums.phpfreaks.com/topic/28805-news-management/#findComment-132157 Share on other sites More sharing options...
alen Posted November 29, 2006 Author Share Posted November 29, 2006 Now I get this,Parse error: parse error, unexpected T_VARIABLE in C:\Program Files\Server\public_html\nyheter\edit_process.php on line 18 Link to comment https://forums.phpfreaks.com/topic/28805-news-management/#findComment-132173 Share on other sites More sharing options...
chriscloyd Posted November 29, 2006 Share Posted November 29, 2006 ya i made a mistake heres the new code tlel me if it works[code]<?phpinclude('config.php');$db=mysql_connect($db_host,$db_user,$db_pass); mysql_select_db ($db_name) or die ("Cannot connect to database"); if(isset($_GET['id'])) {$id = $_GET['id'];echo "<!-- Your query was selected with a get method -->";$query=mysql_query("SELECT * FROM news WHERE id='$id'") or die(mysql_error());}if(isset($_POST['id'])){$id = $_POST['id'];echo "<!-- Your query was selected with a post method -->";$query=mysql_query("SELECT * FROM news WHERE id='$id'") or die(mysql_error());}while($r=mysql_fetch_array($query)) {$nid = $r['id']$title = $r['title'];$news = $r['news']; echo "<form name='edit_process.php' method='post' action='edit_save.php?id=".$nid."'> <p>Title : <input type='text' name='title' value='".$title."' /> </p> <p>News :</p> <p> <textarea name='news' cols='40' rows='6'>".$news."</textarea> </p> <p> <input type='submit' name='Submit' value='Save' /> </p> </form>";}?>[/code] Link to comment https://forums.phpfreaks.com/topic/28805-news-management/#findComment-132229 Share on other sites More sharing options...
alen Posted November 29, 2006 Author Share Posted November 29, 2006 I get the exact same error as before. Link to comment https://forums.phpfreaks.com/topic/28805-news-management/#findComment-132308 Share on other sites More sharing options...
bljepp69 Posted November 29, 2006 Share Posted November 29, 2006 You're missing a ';' on line 17...$nid = $r['id']; Link to comment https://forums.phpfreaks.com/topic/28805-news-management/#findComment-132314 Share on other sites More sharing options...
alen Posted November 29, 2006 Author Share Posted November 29, 2006 Now I get;Notice: Undefined variable: query in C:\Program Files\Server\public_html\nyheter\edit_process.php on line 16Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\Program Files\Server\public_html\nyheter\edit_process.php on line 16 Link to comment https://forums.phpfreaks.com/topic/28805-news-management/#findComment-132336 Share on other sites More sharing options...
alen Posted November 29, 2006 Author Share Posted November 29, 2006 I'm trying to figure it out, but I just can't read through the lines. Link to comment https://forums.phpfreaks.com/topic/28805-news-management/#findComment-132387 Share on other sites More sharing options...
bljepp69 Posted November 29, 2006 Share Posted November 29, 2006 That likely means that your query ($query=mysql_query("SELECT * FROM news WHERE id='$id'") returned nothing. No rows matched your selection criteria, so the resource ($query) is empty. Then the call to mysql_fetch_array is invalid.Try this:[code]<?php include('config.php'); $db=mysql_connect($db_host,$db_user,$db_pass); mysql_select_db ($db_name) or die ("Cannot connect to database"); if(isset($_GET['id'])) { $id = $_GET['id']; echo "<!-- Your query was selected with a get method -->"; $query=mysql_query("SELECT * FROM news WHERE id='$id'") or die(mysql_error()); } if(isset($_POST['id'])){ $id = $_POST['id']; echo "<!-- Your query was selected with a post method -->"; $query=mysql_query("SELECT * FROM news WHERE id='$id'") or die(mysql_error()); } if (mysql_num_rows($query)>0) { while ($r=mysql_fetch_array($query)) { $nid = $r['id'] $title = $r['title']; $news = $r['news']; echo "<form name='edit_process' method='post' action='edit_save.php?id=".$nid."'> <p>Title : <input type='text' name='title' value='".$title."' /> </p> <p>News :</p> <p> <textarea name='news' cols='40' rows='6'>".$news."</textarea> </p> <p> <input type='submit' name='Submit' value='Save' /> </p> </form>"; } } else { echo "<p>No news items.</p>"; }?>[/code]I also think you could get rid of the "if (isset($_POST['id']..." statement. You don't have any variable called 'id' in your form and you are only using a GET method in the query string. If you wanted to make the processing code such that it didn't matter, then change the GET to REQUEST and then it will grab both POST & GET variables. Link to comment https://forums.phpfreaks.com/topic/28805-news-management/#findComment-132434 Share on other sites More sharing options...
chriscloyd Posted November 29, 2006 Share Posted November 29, 2006 [CODE]<?phpinclude('config.php');$db=mysql_connect($db_host,$db_user,$db_pass); mysql_select_db ($db_name) or die ("Cannot connect to database"); if(isset($_GET['id'])) {$id = $_GET['id'];echo "<!-- Your query was selected with a get method -->";$query=mysql_query("SELECT * FROM news WHERE id='$id'") or die(mysql_error());}if(isset($_POST['id'])){$id = $_POST['id'];echo "<!-- Your query was selected with a post method -->";$query=mysql_query("SELECT * FROM news WHERE id='$id'") or die(mysql_error());}while($r=mysql_fetch_array($query)) {$nid = $r['id'];$title = $r['title'];$news = $r['news']; echo "<form name='edit_process.php' method='post' action='edit_save.php?id=".$nid."'> <p>Title : <input type='text' name='title' value='".$title."' /> </p> <p>News :</p> <p> <textarea name='news' cols='40' rows='6'>".$news."</textarea> </p> <p> <input type='submit' name='Submit' value='Save' /> </p> </form>";}?>[/CODE] Link to comment https://forums.phpfreaks.com/topic/28805-news-management/#findComment-132438 Share on other sites More sharing options...
alen Posted November 29, 2006 Author Share Posted November 29, 2006 I tried the code bljepp69 gave me, and it got me another error;[b]Notice:[/b] Undefined variable: query in [b]C:\Program Files\Server\public_html\news\edit_process.php[/b] on line [b]10[/b][b]Warning:[/b] mysql_fetch_array(): supplied argument is not a valid MySQL result resource in [b]C:\Program Files\Server\public_html\news\edit_process.php[/b] on line [b]10[/b] Link to comment https://forums.phpfreaks.com/topic/28805-news-management/#findComment-132447 Share on other sites More sharing options...
bljepp69 Posted November 30, 2006 Share Posted November 30, 2006 Now it's time to hunt down where the error is that's causing the query to not be valid. Try this code. I have added some echo statements to send output at various points in the code so we can see what's happening.[code]<?php include('config.php'); $db=mysql_connect($db_host,$db_user,$db_pass); mysql_select_db ($db_name) or die ("Cannot connect to database"); if(isset($_GET['id'])) { $id = $_GET['id']; echo "id = $id<br />"; //check the value of $id echo "<!-- Your query was selected with a get method -->"; $query_text = "SELECT * FROM news WHERE id='$id'"; //verify that the query actually picked up the variable $id echo "query = $query_text<br />"; $query=mysql_query($query_text) or die(mysql_error()); } if (mysql_num_rows($query)>0) { echo mysql_num_rows($query)." rows returned<br />"; //see how many rows are actually returned while ($r=mysql_fetch_array($query)) { $nid = $r['id'] $title = $r['title']; $news = $r['news']; echo "<form name='edit_process' method='post' action='edit_save.php?id=".$nid."'> <p>Title : <input type='text' name='title' value='".$title."' /> </p> <p>News :</p> <p> <textarea name='news' cols='40' rows='6'>".$news."</textarea> </p> <p> <input type='submit' name='Submit' value='Save' /> </p> </form>"; } } else { echo "<p>No news items.</p>"; }?>[/code]Also, you can get rid of the "Notice" by setting this at the top of your code...error_reporting(E_ALL ^ E_NOTICE); Link to comment https://forums.phpfreaks.com/topic/28805-news-management/#findComment-132457 Share on other sites More sharing options...
alen Posted November 30, 2006 Author Share Posted November 30, 2006 One last error;Parse error: parse error, unexpected T_VARIABLE in C:\Program Files\Server\public_html\news\edit_process.php on line 19 Link to comment https://forums.phpfreaks.com/topic/28805-news-management/#findComment-132472 Share on other sites More sharing options...
chriscloyd Posted November 30, 2006 Share Posted November 30, 2006 [CODE]<?php include('config.php'); $db=mysql_connect($db_host,$db_user,$db_pass); mysql_select_db ($db_name) or die ("Cannot connect to database"); if(isset($_GET['id'])) { $id = $_GET['id']; echo "id = $id<br />"; //check the value of $id echo "<!-- Your query was selected with a get method -->"; $query_text = "SELECT * FROM news WHERE id='$id'"; //verify that the query actually picked up the variable $id echo "query = $query_text<br />"; $query=mysql_query($query_text) or die(mysql_error()); } if (mysql_num_rows($query)>0) { echo mysql_num_rows($query)." rows returned<br />"; //see how many rows are actually returned while ($r=mysql_fetch_array($query)) { $nid = $r['id']; $title = $r['title']; $news = $r['news']; echo "<form name='edit_process' method='post' action='edit_save.php?id=".$nid."'> <p>Title : <input type='text' name='title' value='".$title."' /> </p> <p>News :</p> <p> <textarea name='news' cols='40' rows='6'>".$news."</textarea> </p> <p> <input type='submit' name='Submit' value='Save' /> </p> </form>"; } } else { echo "<p>No news items.</p>"; }?>[/CODE] Link to comment https://forums.phpfreaks.com/topic/28805-news-management/#findComment-132473 Share on other sites More sharing options...
alen Posted November 30, 2006 Author Share Posted November 30, 2006 I'm feeling we're making progress;Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\Program Files\Server\public_html\news\edit_process.php on line 15No news items. Link to comment https://forums.phpfreaks.com/topic/28805-news-management/#findComment-132618 Share on other sites More sharing options...
chriscloyd Posted November 30, 2006 Share Posted November 30, 2006 [CODE]<?phpinclude('config.php');$db=mysql_connect($db_host,$db_user,$db_pass); mysql_select_db ($db_name) or die ("Cannot connect to database"); if(isset($_GET['id'])) {$id = $_GET['id'];echo "<!-- Your query was selected with a get method -->";$query=mysql_query("SELECT * FROM news WHERE id='$id'") or die(mysql_error());}if(isset($_POST['id'])){$id = $_POST['id'];echo "<!-- Your query was selected with a post method -->";$query=mysql_query("SELECT * FROM news WHERE id='$id'") or die(mysql_error());}while($r=mysql_fetch_array($query)) {$nid = $r['id'];$title = $r['title'];$news = $r['news']; echo "<form name='edit_process.php' method='post' action='edit_save.php?id=".$nid."'> <p>Title : <input type='text' name='title' value='".$title."' /> </p> <p>News :</p> <p> <textarea name='news' cols='40' rows='6'>".$news."</textarea> </p> <p> <input type='submit' name='Submit' value='Save' /> </p> </form>";}?>[/CODE] Link to comment https://forums.phpfreaks.com/topic/28805-news-management/#findComment-132619 Share on other sites More sharing options...
alen Posted November 30, 2006 Author Share Posted November 30, 2006 the code gave me these errors;Notice: Undefined variable: query in C:\Program Files\Server\public_html\news\edit_process.php on line 16Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\Program Files\Server\public_html\news\edit_process.php on line 16 Link to comment https://forums.phpfreaks.com/topic/28805-news-management/#findComment-132653 Share on other sites More sharing options...
alen Posted November 30, 2006 Author Share Posted November 30, 2006 Nevermind the Notice error, cleared that one. Link to comment https://forums.phpfreaks.com/topic/28805-news-management/#findComment-132995 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.