Lee Posted January 15, 2011 Share Posted January 15, 2011 I've tried a few different methods, but I can't get this damn function to work. Can someone show me where I've gone wrong? Here's my code & form. I'm still baffled why it doesn't work! url is update-content.php?id=12 <?php include("../include/session.php"); if(!$session->logged_in) { header('Location: ../login.php'); die; } require_once('../include/functions.php'); function content($id) { $id = $_GET['id']; $connection = db_connect(); $query = sprintf("select * from content where id = '$id'", mysql_real_escape_string($id) ); $result = mysql_query($query); $number_of_posts = mysql_num_rows($result); if ($number_of_posts == 0) { return false; } $row = mysql_fetch_array($result); return $row; } function update($param) { // Get the content id from url to output into the editor $id = $_GET['id']; $connect = db_connect(); $page = mysql_real_escape_string($_POST['page']); $page_id = mysql_real_escape_string($_POST['page_id']); $title = mysql_real_escape_string($_POST['title']); $text = mysql_real_escape_string($_POST['text']); $query = ("UPDATE content SET page = '$page' page_id = '$page_id' title = '$title' text = '$text' WHERE id = '$id' "); $result = mysql_query($query); if (!$result) { return false; } else { return true; } } ?> <?php // Send form data to mysql if(isset($_POST['text'])) { $result = update($_POST); if($result === true) { echo 'Success!'; die(); } } ?> And the form.. <form form name="editor" id="editor" method="post" action="update-content.php"> <p><br /> <b>Assign to page</b><br /> <input name="page" id="page" size="60" maxlength="500" value="<?php $page = content($id); echo stripslashes($page['page']); ?>" /> <br /> <br /> <b>Page id</b> <input name="page_id" id="page_id" size="4" maxlength="4" value="<?php $page_id = content($id); echo stripslashes($page_id['page_id']); ?>" /> <br /> <br /> <b>Title</b> <span class="smalltext">(Just a short name this piece of content)</span><br /> <input name="title" id="title" size="60" maxlength="200" value="<?php $title = content($id); echo stripslashes($title['title']); ?>" /> <br /> <br /> <br /> <strong>Content</strong> <span class="smalltext">(paste html in here)</span><br /> <textarea name="text" id="text" cols="75" rows="15"><?php $text = content($id); echo stripslashes($text['text']); ?></textarea> <?php //turn the text area into CK Editor echo $ckeditor_ini; ?> <br /> <input type="image" src="../images/button_submit.gif" alt="submit" name="submit" value="submit" /> <a href="index.php"><img src="../images/button_cancel.gif" alt="Cancel" width="120" height="26" border="0" /> </form> Link to comment https://forums.phpfreaks.com/topic/224550-update-formfunction-not-working-cant-figure-out-why/ Share on other sites More sharing options...
Pikachu2000 Posted January 15, 2011 Share Posted January 15, 2011 Are you getting any errors? 1 - Here you define the function and require that $id be passed in as a parameter, then you immediately overwrite $id with whatever value $_GET['id'] holds. Why? function content($id) { $id = $_GET['id']; 2 - In the second function, you require the parameter $param, then never use its value. 3 - I don't see where you ever actually call either of the functions anywhere . . . Link to comment https://forums.phpfreaks.com/topic/224550-update-formfunction-not-working-cant-figure-out-why/#findComment-1159911 Share on other sites More sharing options...
Lee Posted January 15, 2011 Author Share Posted January 15, 2011 Thanks pikachu the $id in function content ($id) is just holding $id as an array that I can use to fetch data from the row and echo it into the form values. That part is working fine. Its the update() function that I am having trouble with. I am calling it in this line (well I think that's what it should be doing anyway) maybe this is the bit I've got wwrong: if(isset($_POST['text'])) { $id = $_GET['id']; $result = update($id); } Link to comment https://forums.phpfreaks.com/topic/224550-update-formfunction-not-working-cant-figure-out-why/#findComment-1159915 Share on other sites More sharing options...
Pikachu2000 Posted January 15, 2011 Share Posted January 15, 2011 If you don't need to actually pass a parameter to the function, you can simply omit it from the definition. Then you call the function with empty parentheses: $result = update(); Since the success or failure of the function depends on $_GET['id'] having a value, you should also check for that in the function. function update() { // Get the content id from url to output into the editor if( !empty($_GET['id']) ) { $id = $_GET['id']; $connect = db_connect(); $page = mysql_real_escape_string($_POST['page']); $page_id = mysql_real_escape_string($_POST['page_id']); $title = mysql_real_escape_string($_POST['title']); $text = mysql_real_escape_string($_POST['text']); $query = ("UPDATE content SET page = '$page' page_id = '$page_id' title = '$title' text = '$text' WHERE id = '$id' "); $result = mysql_query($query); if (!$result) { return false; } else { return true; } } else { return FALSE; } } Link to comment https://forums.phpfreaks.com/topic/224550-update-formfunction-not-working-cant-figure-out-why/#findComment-1159931 Share on other sites More sharing options...
Lee Posted January 15, 2011 Author Share Posted January 15, 2011 Thanks, I did try without a parameter and just setting the WHERE clause to $id (the value of $_GET ['id']) but it still doesn't update the database. Thanks for the tip about checking $_GET ['id'] I'm wondering if its something to do with this line: if(isset($_POST['text'])) { $result = update(); } It works ok for insert, but for some baffling reason, I can't make this update work. Link to comment https://forums.phpfreaks.com/topic/224550-update-formfunction-not-working-cant-figure-out-why/#findComment-1159933 Share on other sites More sharing options...
Pikachu2000 Posted January 15, 2011 Share Posted January 15, 2011 You could always add an else{} clause, something to the effect of this: if(isset($_POST['text'])) { $result = update(); } else { echo 'Function can\'t run because $_POST[\'text\'] isn\'t set.'; } Link to comment https://forums.phpfreaks.com/topic/224550-update-formfunction-not-working-cant-figure-out-why/#findComment-1159935 Share on other sites More sharing options...
Lee Posted January 15, 2011 Author Share Posted January 15, 2011 Hmm great, getting somewhere now thanks. The Function can't run because $_POST['text'] isn't set. message appears as soon as the page is loaded now, before I even submit the form. However, the <textarea name="text" id= "text"> is set, it has text in it. Now I'm baffled by that instead lol Link to comment https://forums.phpfreaks.com/topic/224550-update-formfunction-not-working-cant-figure-out-why/#findComment-1159938 Share on other sites More sharing options...
Pikachu2000 Posted January 15, 2011 Share Posted January 15, 2011 You might test it with <input type="submit" instead of <input type="image" and see what happens. IIRC, quite a few browsers have trouble with images as submit buttons. Link to comment https://forums.phpfreaks.com/topic/224550-update-formfunction-not-working-cant-figure-out-why/#findComment-1159942 Share on other sites More sharing options...
Lee Posted January 15, 2011 Author Share Posted January 15, 2011 It made no difference. Thanks anyway Link to comment https://forums.phpfreaks.com/topic/224550-update-formfunction-not-working-cant-figure-out-why/#findComment-1159943 Share on other sites More sharing options...
Lee Posted January 15, 2011 Author Share Posted January 15, 2011 WOOOHOOO!!! I solved it, only taken me a whole day to figure it out lol function update($params) { $id = $_POST['id']; // this is the bit that made it work.. $connection = db_connect(); $query = sprintf("update content set page = '%s', page_id = '%s', title = '%s', text = '%s' where id = '$id'", mysql_real_escape_string($params['page']), mysql_real_escape_string($params['page_id']), mysql_real_escape_string($params['title']), mysql_real_escape_string($params['text']) ); $result = mysql_query($query); if (!$result) { return false; } else { return true; } } Then I just added this to the form. It may not be ideal, but the damn thing works at last <input type="hidden" name="id" id="id" size="4" maxlength="4" value="<?php $id = content($id); echo stripslashes($id['id']); ?>" /> Link to comment https://forums.phpfreaks.com/topic/224550-update-formfunction-not-working-cant-figure-out-why/#findComment-1159949 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.