pezzie Posted August 20, 2006 Share Posted August 20, 2006 Hii want to get data from mySQL and wish to delete or update it.The delete is fine but when it comes to update, nothing happens at all.i want the data to be present in the textboxes so that the user does not have to type the whole thing in again. [color=red]PLEASE PLEASE help. i need it by tomorrow.[/color]Thank you.this is my code:[color=red]edit.php[/color]<?// Connect database include("dbConfig.php");// Get all records in all columns from table and put it in $result.$result=mysql_query("select * from articles ORDER BY id");/*Split records in $result by table rows and put them in $row. Make it looping by while statement. */while($row=mysql_fetch_assoc($result)){// Output echo "ID : {$row['id']} <br/>"; echo "Title : {$row['title']} <br/>";echo "Content : {$row['content']} <br/>";echo "Filename : {$row['filename']} <hr>";// Add a link with a parameter(id) and it's value.echo '<a href="update.php?id='.$row['id'].'">Update</a>';echo "<HR>";}mysql_close();?> //------------------------------------------------------------------------------------------[color=red]update.php[/color]<?// Connect database. include("dbConfig.php");if($Submit){// Get parameters from form. $id=$_POST['id'];$title=$_POST['title'];$content=$_POST['content'];$filename=$_POST['filename'];mysql_query("update articles set title='$title', content='$content', filename='$filename' where id='$id'");header("location:edit.php");// Get id parameter (GET method) from edit.php $id=$_GET['id'];// Get records in all columns from table where column id equal in $id and put it in $result.//$result=mysql_query("select * from articles where id='$id'");// Split records in $result by table rows and put them in $row. $row=mysql_fetch_assoc($result);}?><html><body><form id="form1" name="form1" method="post" action="<? echo $PHP_SELF; ?>"><p>Name : <input name="name" type="text" id="title" value="<? echo $row['title']; ?>"/><br />Email : <input name="email" type="text" id="content" value="<? echo $row['content']; ?>"/><br />Tel : <input name="tel" type="text" id="filename" value="<? echo $row['filename']; ?>"/></p><p><input type="submit" name="Submit" value="Submit" /></p></form> </body></html> Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted August 20, 2006 Share Posted August 20, 2006 You are not exiting from the if statement in the correct place. Change:[code]<?// Connect database.include("dbConfig.php");if($Submit){// Get parameters from form.$id=$_POST['id'];$title=$_POST['title'];$content=$_POST['content'];$filename=$_POST['filename'];mysql_query("update articles set title='$title', content='$content', filename='$filename' where id='$id'");header("location:edit.php");// Get id parameter (GET method) from edit.php$id=$_GET['id'];// Get records in all columns from table where column id equal in $id and put it in $result.//$result=mysql_query("select * from articles where id='$id'");// Split records in $result by table rows and put them in $row.$row=mysql_fetch_assoc($result);}?>[/code]to:[code]<?php// Connect database.include("dbConfig.php");if($Submit){ // Get parameters from form. $id=$_POST['id']; $title=$_POST['title']; $content=$_POST['content']; $filename=$_POST['filename']; mysql_query("update articles set title='$title', content='$content', filename='$filename' where id='$id'"); header("location:edit.php"); exit;}// Get id parameter (GET method) from edit.php$id=$_GET['id'];// Get records in all columns from table where column id equal in $id and put it in $result.//$result=mysql_query("select * from articles where id='$id'");// Split records in $result by table rows and put them in $row.$row=mysql_fetch_assoc($result);?>[/code] Quote Link to comment Share on other sites More sharing options...
pezzie Posted August 20, 2006 Author Share Posted August 20, 2006 Thank for the reply.it is now giving me an error.Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /var/www/vhosts/acreinternet.co.uk/subdomains/stage2/httpdocs/new/update.php on line 23 Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted August 20, 2006 Share Posted August 20, 2006 That means your query is failing. Probably because you commented out your query:[code]// Get records in all columns from table where column id equal in $id and put it in $result.//$result=mysql_query("select * from articles where id='$id'");[/code] Quote Link to comment Share on other sites More sharing options...
448191 Posted August 20, 2006 Share Posted August 20, 2006 You can't just ASSUME when you toss a query, it will be executed ok.[code]<?phpmysql_query($query) or die('Query Failed: '.$query.' MySQL returned: '.mysql_error());?>[/code] Quote Link to comment Share on other sites More sharing options...
pezzie Posted August 20, 2006 Author Share Posted August 20, 2006 Thank you for your replies.everything is fine now and the text boxes appear with the data inside them BUT once i click on submit, nothing happens .EXAMPLE: if i change name from DAVID to JOHN and click on update, the page refreshes and the name changes to DAVID again. Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted August 20, 2006 Share Posted August 20, 2006 If register_globals is not on, then you need to change this:[code]if($Submit){[/code]to[code]if($_POST['Submit']){[/code]Which you should probably do anyway. Quote Link to comment Share on other sites More sharing options...
pezzie Posted August 20, 2006 Author Share Posted August 20, 2006 now it goes to page edit, BUT the data is the same. Quote Link to comment Share on other sites More sharing options...
pezzie Posted August 20, 2006 Author Share Posted August 20, 2006 Anyone knows why? :-[ Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted August 20, 2006 Share Posted August 20, 2006 Echo out your query, then place it into phpMyAdmin or MySQL Query Browser and check to make sure that it is executing correctly. Quote Link to comment Share on other sites More sharing options...
pezzie Posted August 20, 2006 Author Share Posted August 20, 2006 When i echo out my $result, the output is [color=red]Resource id #4[/color] Quote Link to comment Share on other sites More sharing options...
pezzie Posted August 20, 2006 Author Share Posted August 20, 2006 please help Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted August 20, 2006 Share Posted August 20, 2006 you need to test the string...change thsi bit of ode so that the query string is in a var that you can print to screen...[code]<?phpif($Submit){ // Get parameters from form. $id=$_POST['id']; $title=$_POST['title']; $content=$_POST['content']; $filename=$_POST['filename']; $qry = "update articles set title='$title', content='$content', filename='$filename' where id='$id'"; echo $qry; mysql_query($qry); //header("location:edit.php"); comment this to make sure your qry is echoed... //exit;}?>[/code] Quote Link to comment Share on other sites More sharing options...
pezzie Posted August 20, 2006 Author Share Posted August 20, 2006 after i changed the code to the above reply, i got this.[color=red]Parse error: parse error, unexpected $ in /var/www/vhosts/acreinternet.co.uk/subdomains/stage2/httpdocs/new/update.php on line 65[/color]line 65 is the HTML TAG </body> Quote Link to comment Share on other sites More sharing options...
kenwvs Posted August 20, 2006 Share Posted August 20, 2006 Quite often when I get this error, it means I have an open bracket either a "() or {} or quotation marks " or ' not matched up........have one side open and not closed later on.I have also found this error if i have forgot to close the my php with ?>Ken Quote Link to comment Share on other sites More sharing options...
pezzie Posted August 20, 2006 Author Share Posted August 20, 2006 checked everything and no luck. This is a joke man. no one knows what the hell is wrong with that code. the code is getting on my nerves. >:( . Quote Link to comment Share on other sites More sharing options...
448191 Posted August 20, 2006 Share Posted August 20, 2006 Yup, that's a bracket error.Gimme everyting your got and I'll put it in good old Zend DE and fix it for you... Quote Link to comment Share on other sites More sharing options...
pezzie Posted August 20, 2006 Author Share Posted August 20, 2006 Thanks for that.[color=red]index.php[/color]<?phpsession_start();if (!$_SESSION["valid_user"]){// User not logged in, redirect to login pageHeader("Location: login.php");}// Display Member informationecho "<p>User ID: " . $_SESSION["valid_id"];echo "<p>Username: " . $_SESSION["valid_user"];echo "<p>Logged in: " . date("m/d/Y", $_SESSION["valid_time"]);// Display Delete, Edit and add article linksecho "<p><a href=\"articles.php\">VIEW all Articles</a></p>";echo "<p><a href=\"add.php\">Add New Article</a></p>";echo"<br>";// Display logout linkecho "<p><a href=\"logout.php\">Click here to logout!</a></p>";?>[color=red]edit.php[/color]<?// Connect database include("dbConfig.php");// Get all records in all columns from table and put it in $result.$result=mysql_query("select * from articles ORDER BY id");/*Split records in $result by table rows and put them in $row. Make it looping by while statement. */while($row=mysql_fetch_assoc($result)){// Output echo "ID : {$row['id']} <br/>"; echo "Title : {$row['title']} <br/>";echo "Content : {$row['content']} <br/>";echo "Filename : {$row['filename']} <hr>";// Add a link with a parameter(id) and it's value.echo '<a href="update.php?id='.$row['id'].'">Update</a>';echo "<HR>";}mysql_close();?> [color=red]update.php[/color]<?php// Connect database.include("dbConfig.php");if($_POST['Submit']){// Get parameters from form.$id=$_POST['id'];$title=$_POST['title'];$content=$_POST['content'];$filename=$_POST['filename'];//mysql_query("update articles set title='$title', content='$content', filename='$filename' where id='$id'");$q="UPDATE `articles` SET title='$title', content='$content', filename='$filename' where id='$id'";$r = mysql_query($q);if ( !$r ) { echo "ERROR Please try again";}else{header("location:edit.php");exit;}}// Get id parameter (GET method) from edit.php$id=$_GET['id'];// Get records in all columns from table where column id equal in $id and put it in $result.$result=mysql_query("select * from articles where id='$id'");// Split records in $result by table rows and put them in $row.$row=mysql_fetch_assoc($result);echo $id;?><html><body><form id="form1" name="form1" method="post" action="<? echo $PHP_SELF; ?>"><p>Name : <input name="name" type="text" id="title" value="<? echo $row['title']; ?>"/><br />Email : <input name="email" type="text" id="content" value="<? echo $row['content']; ?>"/><br />Tel : <input name="tel" type="text" id="filename" value="<? echo $row['filename']; ?>"/></p><p><input type="submit" name="Submit" value="Submit" /></p></form> </body></html> Quote Link to comment Share on other sites More sharing options...
448191 Posted August 20, 2006 Share Posted August 20, 2006 I take it back, it's not a bracket error. :PGet rid of the shorthand tags ("[color=red][b]<?[/b][/color]") and replace them with proper tags ("[color=red][b]<?php[/b][/color]"). Quote Link to comment Share on other sites More sharing options...
pezzie Posted August 21, 2006 Author Share Posted August 21, 2006 any help. i still have not figured out what is wrong with that dam code of mine. Quote Link to comment Share on other sites More sharing options...
AndyB Posted August 21, 2006 Share Posted August 21, 2006 I assume the problem relates to update.php not updating records?Update.php appears to expect to get the information to be updated from a POST array ... but I don't see any POSTed form action anywhere in the code. I do see a link equivalent to update.php?id=something (which isn't going to place any information in a POSTed array). All edit.php does is display the information.Perhaps it would be helpful to post the current version of update.php and the current version of 'whatever it is' that is linked to update.php so we can focus on complete scripts rather than attempt to follow the various script changes suggested piecemeal in the thread. Quote Link to comment Share on other sites More sharing options...
AndyB Posted August 21, 2006 Share Posted August 21, 2006 hmm. I suspect the update query is doing nothing because the value of id does not exist for the UPDATE query.Add an extra line to the form in update.php so that the record ID gets POSTed:[code]<input type="hidden" name="id" value="<?php echo $id;?>"/>[/code] Quote Link to comment Share on other sites More sharing options...
pezzie Posted August 21, 2006 Author Share Posted August 21, 2006 You are god. Thank you very much.i dnt know how to thank you mate.thank you Quote Link to comment Share on other sites More sharing options...
AndyB Posted August 21, 2006 Share Posted August 21, 2006 The not-so-secret secret methods of debugging queries that "refuse to work" is to know exactly what the query is - which is frequently not what you [i]think[/i] it is - and what the specific error is.[code]$query = " .... whatever the query is ...";echo $query; // nice to know what's happening[/code]Also, learn to make better use of error trapping with code like this:[code]$query = " .... whatever the query is ...";$result = mysql_query($query) or die("Error: ". mysql_error(). " with query ". $query);[/code] 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.