Adastra Posted November 14, 2006 Share Posted November 14, 2006 I have this tiny tiny updated script, but for some reason it's not updating the data in the database. I don't even get an error, all it does is give me the message "Gallery blah has been updated", but it still didn't update anything in the database. And I absolutely can't see what's wrong with my query. Can anyone help me find the error please? :)[code=php:0]<?phprequire_once('gal_confi.php');echo $header;$id = $_GET['id'];if ($_GET['action'] == "") { $result = mysql_query ("SELECT * FROM $gal_galleries WHERE id='$id'") or print ("Unable to select data.<br />".mysql_error()); while ($row = mysql_fetch_array($result)) { $date = date("M dS, Y",$row["timestamp"]); $timestamp = $row["timestamp"]; $old_title = $row["title"]; $old_description = $row["description"]; $old_title = str_replace('"','\'',$old_title); $old_description = str_replace('"','\'',$old_description); } echo "<strong>Edit Gallery # ".$id.", created on ".$date."</strong><br /><br />"; ?> <form method="post" action="gal_galleries_update.php?action=submit"> Gallery Title: <br /> <input type="text" size="70" name="title" id="title" value="<? echo $old_title; ?>" /> <br /> <br /> Gallery Description: <br /> <textarea cols="65" rows="10" name="description" id="description"><? echo $old_description; ?></textarea> <br /> <input type="submit" name="submit" value="submit"> </form> <?php} elseif (isset($_GET['action']) && ($_GET['action'] == "submit")) { $id = $_GET['id']; $title = addslashes($_POST['title']); $description = addslashes($_POST['description']); $result = mysql_query("UPDATE $gal_galleries SET title='$title',description='$description' WHERE id='$id'") or print ("Unable to post data.<br />".mysql_error()); if ($result != false) { print "Gallery <strong>$title</strong> has been updated!"; } else { mysql_error(); } }print "<br /><br />Back to <a href='index.php'>GalAdmy</a>.";echo $footer;?>[/code] Quote Link to comment Share on other sites More sharing options...
SharkBait Posted November 14, 2006 Share Posted November 14, 2006 On thing I do with my queries that is easier to debug would look like this:[code]<?php$str = "UPDATE gal_galleries SET title='{$title}',description='{$description}' WHERE id='{$id}'";// If there is an error the script will DIE and tell you why$query = mysql_query($str) or die("MySQL Error: <br /> {$str} <br />". mysql_error());// $num will return how many rows were updated from the query if successful$num = mysql_affected_rows();if($num > 0) { echo " Gallery {$title} has been updated.";} else { // Technically if your query doesn't work you wont get here}?>[/code] Quote Link to comment Share on other sites More sharing options...
Adastra Posted November 14, 2006 Author Share Posted November 14, 2006 thanks, I tried that snippet, but I still have the same problem: it doesn't do anything :(I don't understand why it's not updating - the selecting and everything else works without a problem... Quote Link to comment Share on other sites More sharing options...
blear Posted November 14, 2006 Share Posted November 14, 2006 When you submit and the page loads the }elseif{ part, $_GET['id']; is no longer set, so your mysql update query doesnt see any rows with id = ''You can either add a hidden form value,[code] <INPUT TYPE='hidden' NAME='id' VALUE='<?php echo $id; ?>' > [/code]and change from $_GET to $_POST, or you can append '&id=<?php echo $id; ?>' to the end of your ACTION= property on your form Quote Link to comment Share on other sites More sharing options...
Adastra Posted November 14, 2006 Author Share Posted November 14, 2006 thanks blear, that was the error. I don't understand though why it didn't pass the id on, I've defined it in the part which is in front of the first if {}.... 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.