ballhogjoni Posted July 17, 2007 Share Posted July 17, 2007 I am trying to update or insert the data in a specific field in my db, but its not working. I think it has something to do with passing the variable. Can someone review the code below and tell me what I am missing? <?php $index_page_title = $_POST['indexpagetitle']; if (isset($index_page_title)) { $username="xxxx"; $password="xxxxx"; $database="xxxx"; mysql_connect('localhost',$username,$password); mysql_select_db($database) or die( "Unable to select database"); $result = mysql_query("SELECT * FROM Title") or die(mysql_error()); $num_rows = mysql_num_rows($result) or die(mysql_error()); if ($num_rows >= 1) { mysql_query("UPDATE Title SET newTitle = '".$index_page_title."'") or die(mysql_error()); echo 'you updated you title to '.$index_page_title; echo '<FORM><INPUT TYPE="button" VALUE="Go Back" onClick="history.go(-1)"></FORM>'; } else { mysql_query("INSERT INTO Title (newTitle) VALUES ('$index_page_title')") or die(mysql_error()); echo 'you Added your title to '.$index_page_title; echo '<FORM><INPUT TYPE="button" VALUE="Go Back" onClick="history.go(-1)"></FORM>'; } } else { ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Admin Only</title> </head> <body> Change home page title <br /> <form action="" method="POST"> <table> <tr> <td align="right"> Title: </td> <td> <input type="text" name="indexpagetitle"> </td> </tr> <tr> <td colspan="2" align="center"> <input type="submit" value="Change Home Page Title"/><?php echo $index_page_title; ?> </td> </tr> </table> </form> </body> </html> <?php } ?> Link to comment https://forums.phpfreaks.com/topic/60426-solved-whats-wrong-with-my-code/ Share on other sites More sharing options...
The Little Guy Posted July 17, 2007 Share Posted July 17, 2007 You may want to add a WHERE clause to this statement: mysql_query("UPDATE Title SET newTitle = '".$index_page_title."'") or die(mysql_error()); Link to comment https://forums.phpfreaks.com/topic/60426-solved-whats-wrong-with-my-code/#findComment-300576 Share on other sites More sharing options...
ballhogjoni Posted July 17, 2007 Author Share Posted July 17, 2007 I add the <?php echo $index_page_title; ?> part to see if the variable is even being sent. Its not echoing anything. So I know it has something to do with that. You are right, I need to add a where clause. Link to comment https://forums.phpfreaks.com/topic/60426-solved-whats-wrong-with-my-code/#findComment-300583 Share on other sites More sharing options...
The Little Guy Posted July 17, 2007 Share Posted July 17, 2007 make sure that this is the actual form field name: $_POST['indexpagetitle']; - it may be case sensitive too. Link to comment https://forums.phpfreaks.com/topic/60426-solved-whats-wrong-with-my-code/#findComment-300589 Share on other sites More sharing options...
ballhogjoni Posted July 17, 2007 Author Share Posted July 17, 2007 No I figured our what it was. I was including this page onto another page that did not have the query syntax or even the $index_page_title = $_POST['indexpagetitle'];. So thats why it wasn't echoing the POST. Sorry to waste your time, but I got it fixed. Link to comment https://forums.phpfreaks.com/topic/60426-solved-whats-wrong-with-my-code/#findComment-300591 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.