Page 1 of 1

Hello again, why isnt this working? Update statement

Posted: Tue Apr 13, 2004 5:30 pm
by Ne0
im making a simple database driven site, and all was go. I started working on the admin page. Adding a page worked fine. Editing one went down the tubes.

ok, what the page does is make a form, displays radio buttons with each pages ID, then, hit submit, and this happens:

Code: Select all

<?
if ($_GET['p'] == "epage") {

	if ($_POST['title'] == "") {
$q=0;
$id=$_POST['id'];
$sql=mysql_query("SELECT * FROM pages WHERE id=$id");
$rows=mysql_numrows($sql_getlinks1);
while ($q < $rows) {
	$thetitle=mysql_result($sql_getlinks1,$q,"title");
	$theid=mysql_result($sql_getlinks1,$q,"id");
	$thecontent=mysql_result($sql_getlinks1,$q,"content");
	$theorder=mysql_result($sql_getlinks1,$q,"l_order");
	++$q;
}
?>

<font size="2">
<form action="admin.php?p=epage" method="post">
<input type="hidden" name="id" value="<? echo "$id"; ?>">
Title: <input type="text" name="title" value="<? echo "$thetitle"; ?>"> (will show up as the link)<br>
Order: <input type="text" name="order" value="<? echo "$theorder"; ?>"> (order on navbar, 0,1,2..)<br>
Content:<br>
<textarea name="content" cols="60" rows="15"><? echo "$thecontent"; ?></textarea><br>
<input type="submit" value="EDIT">
</form>
<?
	}
	else{

$id=$_POST['id'];
$title = stripslashes($_POST['title']);
$l_order = stripslashes($_POST['order']);
$content = stripslashes($_POST['content']);


$query = "UPDATE pages set title='$title', l_order='$l_order', content='$content' WHERE id='$id'";
$result = mysql_query($query);
	if ($result) {
		echo "Your page has been updated succesfully.<br>
		<a href=admin.php>Back to Admin</a>";
		}
	else{
		echo "Sorry, your page has not been updated.<br>" . mysql_error() . "<br>
		<a href=admin.php>Back to Admin</a>";
		}

	}
}


include 'footer.html';

?>
it didnt even select the correct page...

thanks in advanced

Posted: Tue Apr 13, 2004 5:34 pm
by markl999
You have $sql=mysql_query("SELECT * FROM pages WHERE id=$id"); but everywhere else uses $sql_getlinks1 ?

Posted: Tue Apr 13, 2004 5:42 pm
by Ne0
aha, ty. I guess that happens when i use the same code for dif things and 4get to change the variables...

but now its not selecting anything:

Code: Select all

<?
if ($_GET['p'] == "epage") {

	if ($_POST['title'] == "") {
$q=0;
$id=$_POST['id'];
$sql=mysql_query("SELECT * FROM pages WHERE id=$id");
$rows=mysql_numrows($sql);
while ($q < $rows) {
	$thetitle=mysql_result($sql,$q,"title");
	$theid=mysql_result($sql,$q,"id");
	$thecontent=mysql_result($sql,$q,"content");
	$theorder=mysql_result($sql,$q,"l_order");
	++$q;
}
?>

<font size="2">
<form action="admin.php?p=epage" method="post">
<input type="hidden" name="id" value="<? echo "$id"; ?>">
Title: <input type="text" name="title" value="<? echo "$thetitle"; ?>"> (will show up as the link)<br>
Order: <input type="text" name="order" value="<? echo "$theorder"; ?>"> (order on navbar, 0,1,2..)<br>
Content:<br>
<textarea name="content" cols="60" rows="15"><? echo "$thecontent"; ?></textarea><br>
<input type="submit" value="EDIT">
</form>
<?
	}
	else{

$id=$_POST['id'];
$title = stripslashes($_POST['title']);
$l_order = stripslashes($_POST['order']);
$content = stripslashes($_POST['content']);


$query = "UPDATE pages set title='$title', l_order='$l_order', content='$content' WHERE id='$id'";
$result = mysql_query($query);
	if ($result) {
		echo "Your page has been updated succesfully.<br>
		<a href=admin.php>Back to Admin</a>";
		}
	else{
		echo "Sorry, your page has not been updated.<br>" . mysql_error() . "<br>
		<a href=admin.php>Back to Admin</a>";
		}

	}
}


include 'footer.html';

?>

Posted: Tue Apr 13, 2004 5:49 pm
by markl999
You need to add some debugging, here's one way of doing it:

Code: Select all

<?php
if ($_GET['p'] == 'epage') {
   if (empty($_POST['title'])) {
    $id = $_POST['id'];
    $query = "SELECT * FROM pages WHERE id=$id";
    echo $query; //debugging
    $result = mysql_query($query) or die(mysql_error());
    if(mysql_num_rows($result) == 0){
        echo 'No Results Found';
    } else {
        $row = mysql_fetch_assoc($result);
        $thetitle = $row['title'];
        $theid = $row['id'];
        $thecontent = $row['content'];
        $theorder = $row['l_order'];
    }
}
?>
I've just used a simple mysql_fetch_assoc() as you don't need to do a while loop, as there should only ever be 0 or 1 results. Make sure the echo $query; bit looks 'right'.