Jump to content


[solved] edit.php?act=edit?id=1


9 replies to this topic

#1 Marxx

    Young Padawan

  • Members
  • Pip
  • 116 posts
  • Gender:Male
  • Location:Finland

Posted 22 September 2006 - 01:49 PM

Hi all!

I need some help whit double switches..

i got this edit.php where i need to use url like this
edit.php?act=edit?id=1

this is what i had tryed but not working.. ?

$act = $_GET["act"];
  $id = $_GET["id"]; 

  $res = mysql_query("SELECT * FROM testi WHERE id = $id") or sqlerr(__FILE__,__LINE__);
  $arr = mysql_fetch_assoc($res);

if ($act == "do_edit") {

  $name = trim($_POST['name2']);
  $kuvaus = trim($_POST['kuvaus']);

  if (!$name || !$kuvaus)
		stderr("Virhe", "Tietoja puuttuu!!");
	
  $kokonimi = sqlesc($name);
  $kuvailu = sqlesc($kuvaus);

mysql_query ("UPDATE testi SET nimi = '$kokonimi', kuvaus = '$kuvailu' WHERE id = $id") or sqlerr(__FILE__,__LINE__);

$returl = "edit.php?act=edit?id=$id";
header("Refresh: 0; url=$returl");
}

if ($act == "edit") {
?>
  <div id="content">
  
  <table border=1 cellspacing=0 cellpadding=5>
  <form method=post action=edit.php?act=do_edit>
	<tr><td>Nimi</td><td><input type=text name=name2 value=<?=$arr["nimi"]?> size=20></td></tr>
	<tr><td>Kuvaus</td><td><input type=text name=kuvaus value=<?=$arr["kuvaus"]?> size=50></td></tr>
	<tr><td colspan=2><input type=submit value='Muokkaa' class=btn></td></tr>
	</form></table>
  
  </div>


error what came up is this Notice: Undefined index: id in c:\public_html\edit.php and if i turn off error reporting page will not show.. :(

Thanks for all help! :P

#2 Matthew.

    Official Spammer .Matt

  • Members
  • PipPipPipPip
  • 2,749 posts
  • Gender:Male
  • Location:England

Posted 22 September 2006 - 01:54 PM

Try this:
$act = $_GET['act'];
$id = $_GET['id'];

Act and id are array keys so you use single quotes.

Also try this:
header("Location: $returl");

Using location instead of a 0 refresh.

#3 Marxx

    Young Padawan

  • Members
  • Pip
  • 116 posts
  • Gender:Male
  • Location:Finland

Posted 22 September 2006 - 02:09 PM

hmm.. sorry but doesn't work... :(

header thing works great tho! :P Thanks for that...

#4 Av-

    I Feel Left Out

  • Members
  • PipPipPipPip
  • 1,971 posts
  • Gender:Male
  • Location:10 ft. below sea level

Posted 22 September 2006 - 02:35 PM

When submitting a form its no longer $_GET but $_POST['act']

2 ways of fixing it
if(isset($_GET['act'])){
$act = $_GET['act']
}elseif(isset($_POST['act'])){
$act = $_POST['act']
}

// or

$act = $_REQUEST['act'];

let me know if that fixed it


Edit: nvm, missed the &act=do_edit in the form location url

Edited by Avalanche, 22 September 2006 - 02:37 PM.


#5 Marxx

    Young Padawan

  • Members
  • Pip
  • 116 posts
  • Gender:Male
  • Location:Finland

Posted 22 September 2006 - 02:47 PM

hmmh.. noh, not working..

It works whit one switch. If i use only id and redirect form into another file to do mysql things. But i don't want to do it that way.

Could my php.ini contain something what could cause this error?


//EDIT oh man..

god damn i'm stupid!! it not work if i put url edit.php?act=edit?id=1
It should be edit.php?act=edit&id=1

haha.. well, thanks for all who helped! :P

Edited by Marxx, 22 September 2006 - 02:56 PM.


#6 Matthew.

    Official Spammer .Matt

  • Members
  • PipPipPipPip
  • 2,749 posts
  • Gender:Male
  • Location:England

Posted 22 September 2006 - 02:54 PM

Sorry hang on can you explain the problem again, i'm confused. Say what works and what doesn't (btw, you are referring to switches - you arent using switches :P)

#7 Av-

    I Feel Left Out

  • Members
  • PipPipPipPip
  • 1,971 posts
  • Gender:Male
  • Location:10 ft. below sea level

Posted 22 September 2006 - 07:19 PM

lol, it was in the code multiple times and even in the title and we all overlooked it :D

#8 Hayden

    P2L Jedi

  • Members
  • PipPipPip
  • 716 posts
  • Gender:Male
  • Location:Texas

Posted 23 September 2006 - 01:59 PM

<?php
define("URI", "http://".$_SERVER['HTTP_HOST']);
define("SELF", $_SERVER['PHP_SELF']);
$act = htmlspecialchars($_GET["act"]);
$id = htmlspecialchars($_GET["id"]);

$res = mysql_query("SELECT * FROM testi WHERE id = $id") or die(mysql_error());
$arr = mysql_fetch_assoc($res);

if ($act == "do_edit") {
	$name = htmlspecialchars(trim($_POST['name2']));
	$kuvaus = htmlspecialchars(trim($_POST['kuvaus']));
	if (!$name || !$kuvaus)
		stderr("Virhe", "Tietoja puuttuu!!");
	$kokonimi = sqlesc($name);
	$kuvailu = sqlesc($kuvaus);
	mysql_query ("UPDATE testi SET nimi = '$kokonimi', kuvaus = '$kuvailu' WHERE id = $id") or die(mysql_error());
	$returl = "?act=edit&id=$id";
	header("Location: ".URI.SELF.$returl);
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php if ($act == "edit") { ?>
<div id="content">
<form method="post" action="<?php echo URI.SELF; ?>?act=do_edit">
<table border=1 cellspacing=0 cellpadding=5>
	<tr><td>Nimi</td><td><input type="text" name="name2" value="<?php echo $arr["nimi"]; ?>" size="20" /></td></tr>
	<tr><td>Kuvaus</td><td><input type="text" name="kuvaus" value="<?php echo $arr["kuvaus"]; ?>" size="50" /></td></tr>
	<tr><td colspan=2><input type="submit" value="Muokkaa" class="btn" /></td></tr>
</table>
</form>
</div>
<?php } ?>
</body>
</html>

I don't know what stderr() and sqlesc() are. I presume they're functions that you created that are outside of this code. *shrugs*

Edited by SpatialVisionary, 23 September 2006 - 02:03 PM.


#9 Crofty

    Young Padawan

  • Members
  • Pip
  • 32 posts

Posted 24 September 2006 - 07:17 AM

This is the way i use and its really easy to add new things and saves alot of line coding.

function generate_page()
{
	$act= $_GET['act'];
	if($act==''){
	$act= 'home';
	}
	switch($act)
	{
		case"edit":
		$id = $_GET['id'];
	   //do mysql query and other stuff
		break;
}
}

Edited by Crofty, 24 September 2006 - 07:37 AM.


#10 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 24 September 2006 - 03:17 PM

View PostMarxx, on Sep 22 2006, 01:47 PM, said:

...i'm stupid!! it not work if i put url edit.php?act=edit?id=1
It should be edit.php?act=edit&id=1

haha.. well, thanks for all who helped! :ph34r:
People, please read before posting, he fixed his own error, it was a simple misunderstanding of query strings.
This topic is now solved.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users