hodgey87 Posted December 17, 2010 Share Posted December 17, 2010 Hey Guys, Im slowly updating our website to php/mysql, im getting there slowly but stuck at the minute. Im try to create a form where we can edit one of the pages text and then update the text in the database if that makes sense Basically at the moment i have a form where you can see the pages text like so: <table width="400" border="0" align="center" cellpadding="3" cellspacing="1"> <tr> <td><strong>Edit About Us English </strong></td> </tr> </table> <table width="400" border="0" align="center" cellpadding="0" cellspacing="1"> <tr> <td><form name="form1" method="post" action="send_update_english.php"> <table width="100%" border="0" cellspacing="1" cellpadding="3"> <tr> <td>Detail</td> <td>:</td> <td><textarea name="detail" cols="50" rows="4" id="detail"> [b]<?php mysql_connect("localhost", "web148", "123") or die(mysql_error()); mysql_select_db("web148") or die(mysql_error()); mysql_query("SET names 'utf8'"); $result = mysql_query("SELECT body FROM aboutenglish WHERE ID = '1'"); $row = mysql_fetch_array( $result ); echo nl2br($row['body']); ?>[/b] </textarea></td> </tr> <tr> <td> </td> <td> </td> <td><input type="submit" name="Submit" value="Submit"> <input type="reset" name="Submit2" value="Reset"></td> </tr> </table> </form> </td> </tr> </table> Ive got that php bit in the middle to pull out the text thats on that page but not sure if thats correct or not. But anyways on the 'send form' page i have: <?php // Details [b]$message="$detail";[/b] mysql_connect("localhost", "web148", "123") or die(mysql_error()); mysql_select_db("web148") or die(mysql_error()); mysql_query("SET names 'utf8'"); $result = mysql_query("UPDATE aboutenglish SET body = '$message' where ID = '1'"); ?> the update query works if i put text in there, but when i use '$message' the database is left blank. This is only my second week doing all this stuff, so bare with me I appreciate your answers. Quote Link to comment https://forums.phpfreaks.com/topic/221985-help-please-with-updating-a-record-sorry/ Share on other sites More sharing options...
denno020 Posted December 17, 2010 Share Posted December 17, 2010 Fairly sure you're creating yourself a CMS? I would suggest watching these youtube videos, I had no idea about how to use php AT ALL, before I watched them. They're made by youtube user flashbuilding. This isn't some sort of marketing crap for him, I genuinely learnt my php from his videos . Hopefully that link will take you to the first video in the playlist. You can then work your way through there. Videos are lengthy, but they're so worth it . Denno Quote Link to comment https://forums.phpfreaks.com/topic/221985-help-please-with-updating-a-record-sorry/#findComment-1148670 Share on other sites More sharing options...
hodgey87 Posted December 17, 2010 Author Share Posted December 17, 2010 Cheers for the link, ill definately check those videos out. Yeh i have no php experience at all, i can do bits like displaying data from a database but stuck on this updating records. Any ideas where im going wrong above? Quote Link to comment https://forums.phpfreaks.com/topic/221985-help-please-with-updating-a-record-sorry/#findComment-1148671 Share on other sites More sharing options...
denno020 Posted December 17, 2010 Share Posted December 17, 2010 Check this out. This is the edit page that you'll learn to create in those videos. It queries the database, and then fills the form with the data from the db, which can then be changed, and resubmitted. There is also javascript to make sure there isn't any empty fields when the form is submitted. <?php session_start(); include_once "admin_check.php"; ?> <?php //Add an if statement here to check if the requested pageID is valid. $pageID = ereg_replace("[^0-9]", "", $_POST['pageNumber']); //filter everything but numbers for security //Query the body section for the proper page include_once "../scripts/connect_to_mysql.php"; $sqlCommand = "SELECT pagetitle, linklabel, pagebody FROM pages WHERE id='$pageID' LIMIT 1"; $query = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error()); while ($row = mysqli_fetch_array($query)) { $pagetitle = $row["pagetitle"]; $linkLabel = $row["linklabel"]; $pageBody = $row["pagebody"]; $pageBody = str_replace("<br />", "", $pageBody); } mysqli_free_result($query); ?> <!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=utf-8" /> <title>Editing Page</title> <!--The following script/s are used for TinyMCE --> <script type="text/javascript" src="../scripts/tiny_mce/tiny_mce.js"></script> <script type="text/javascript"> tinyMCE.init({ theme : "advanced", mode : "textareas", plugins : "fullpage", theme_advanced_buttons3_add : "fullpage" }); </script> <!-- End of TinyMCE scripting --> <script type="text/javascript"> function validate_form(){ valid = true; if(document.form.pageTitle.value == ""){ alert("Please enter the page title."); valid = false; }else if(document.form.linkLabel.value == ""){ alert("Please enter the link label"); valid = false; }else if(document.form.pageBody.value == ""){ alert("Please enter a page body"); valid = false; } return valid; } </script> </head> <body> <p>Be sure to fill in all fields, as they're all required.</p> <form id="form" name="form" method="post" action="page_edit_parse.php" onsubmit="return validate_form();"> <table width="100%" border="0" cellspacing="1" cellpadding="3"> <tr> <td width="300" align="right">Page Full Title</td> <td><label for="pageTitle"></label> <input name="pageTitle" type="text" id="pageTitle" size="150" value="<?php echo $pagetitle ?>" /></td> </tr> <tr> <td align="right">Link Label</td> <td><label for="linkLabel"></label> <input name="linkLabel" type="text" id="linkLabel" size="30" value="<?php echo $linkLabel ?>" /></td> </tr> <tr> <td align="right" valign="top">Page Body</td> <td><label for="pageBody"></label> <textarea name="pageBody" id="pageBody" cols="150" rows="8" value="<?php echo $pageBody ?>"></textarea></td> </tr> <tr> <td align="right"> </td> <td> <input name="pageID" type="hidden" value="<?php echo $pageID ?>"/> <input type="submit" name="submitPage" id="submitPage" value="Create Page Now" /></td> </tr> </table> </form> <p> </p> </body> </html> This is the code that is run when the form is submitted (the changes are submitted). This contains the update functionality that you will be interested in <?php $pageID = $_POST['pageID']; $pageTitle = $_POST['pageTitle']; $linkLabel = $_POST['linkLabel']; $pageBody = $_POST['pageBody']; //Filter Function ========================================================================= function filterFunction($var){ $var = nl2br(htmlspecialchars($var)); $var = eregi_replace("'","'",$var); $var = eregi_replace("`","'",$var); return $var; } $pageTitle = filterFunction($pageTitle); $linkLabel = filterFunction($linkLabel); $pageBody = filterFunction($pageBody); //End Filter Function ===================================================================== include_once "../scripts/connect_to_mysql.php"; //Add the info into the database table $query = mysqli_query($myConnection, "UPDATE pages SET pageTitle='$pageTitle',linkLabel='$linkLabel',pageBody='$pageBody', lastmodified ='now()' WHERE id='$pageID'") or die (mysqli_error($myConnection)); echo 'Operation Completed Successfully! <br/><br/><a href="index.php">Click Here</a>'; exit(); ?> N.B. The code above, while I typed it myself, it was based directly of the code in the videos from flashbuilding, so it's not my code, I merely understand it. Sorry that I am unable to read your code and understand it completely, hopefully you'll be able to model your efforts on these working files that I have provided though . Denno Quote Link to comment https://forums.phpfreaks.com/topic/221985-help-please-with-updating-a-record-sorry/#findComment-1148678 Share on other sites More sharing options...
hodgey87 Posted December 17, 2010 Author Share Posted December 17, 2010 That looks overly complicated at the minute :-\ with my problem it just looks like the text thats in the text box isnt passing over the 'send' form, the update query and everything else works fine. in the edit page i have: <td><textarea name="detail" cols="50" rows="4" id="[b]detail[/b]"> and the 'send' page i have: $message="$detail"; $result = mysql_query("UPDATE aboutenglish SET body = '$message' where ID = '1'"); is that correct for passing the details across Quote Link to comment https://forums.phpfreaks.com/topic/221985-help-please-with-updating-a-record-sorry/#findComment-1148705 Share on other sites More sharing options...
Ryflex Posted December 17, 2010 Share Posted December 17, 2010 Hi hodgey87, You're on the right way but when using a FORM with the METHOD POST you do need to give the $detail a value. You could do that by using $detail = $_POST['detail']; And then you simply insert $detail in the database mysql_query("UPDATE aboutenglish SET body = '$detail' where ID = '1'"); since you're not using the mysql_affected_rows you don't have to use $result you can simpy let the query run. Ryflex Quote Link to comment https://forums.phpfreaks.com/topic/221985-help-please-with-updating-a-record-sorry/#findComment-1148744 Share on other sites More sharing options...
eddyys Posted December 17, 2010 Share Posted December 17, 2010 One suggestion to make, you should put the connection into a handler and then specify that handler when you set the database to use. Ex. $link = mysql_connect('localhost', 'mysql_user', 'mysql_password'); if (!$link) { die('Not connected : ' . mysql_error()); } // make foo the current db $db_selected = mysql_select_db('foo', $link); if (!$db_selected) { die ('Can\'t use foo : ' . mysql_error()); } http://www.php.net/manual/en/function.mysql-select-db.php Quote Link to comment https://forums.phpfreaks.com/topic/221985-help-please-with-updating-a-record-sorry/#findComment-1148748 Share on other sites More sharing options...
hodgey87 Posted December 21, 2010 Author Share Posted December 21, 2010 Cheers guys, worked perfectly Quote Link to comment https://forums.phpfreaks.com/topic/221985-help-please-with-updating-a-record-sorry/#findComment-1149838 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.