Jump to content

Help find the error -mysql_query function


Adastra

Recommended Posts

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]
<?php
require_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]
Link to comment
https://forums.phpfreaks.com/topic/27218-help-find-the-error-mysql_query-function/
Share on other sites

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]


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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.