Jump to content

sending multiple values through a link


spires

Recommended Posts

Hi,

Does anyone know how to send muliple values through a link?
I can send id
but i also want to send the name and description.

to send the id i am using
[code]<A HREF="edit.php?id='.$row['id'].'" class="link">Edit</a>[/code]

And $_GET['id']; on the page it goes to.

How can i add the other values to this line of code?
:D

Link to comment
Share on other sites

yes, you can send as many as you wanted as long as your link is shorter than 1024 characters.
it however, need to separate by &

[code]
href="link.php?id=idvalue&name=namevalue&description=descriptionvalue"

so, a link would look like this:
<a href="link.php?id=idvalue&name=namevalue&description=descriptionvalue" class="link">edit</a>

[/code]
Link to comment
Share on other sites

Yea, and then on the page you just process as many links as you have.
if (isset($_GET['var'])) {
  $var = $_GET['var'];
}

Just remember that sending links through the URL isn't very secure, and you should always validate $_GET variables as much as you can depending on the type of information you will recieve, because they would be very easy to use in a malicious manner. I usually use the is_numeric() function to check ID's.
Link to comment
Share on other sites

Hi, thanks for you help.

I cant seem to get it to work.
I have used
[code]<a href="edit.php?id='.$row['id'].'&name='.$row['name'].'&description='.$row['description'].'" class="link">Edit</a>[/code]

on the main page and:

[code]if (isset($_GET['id'])) {
    $name = $_GET['name'];
$id=$_GET['description'];
}
[/code]

Could you pleade tell me what i'm doing wrong?

You can test it out your self.
Goto [url=http://www.spirestest.com/nicky/gallery/select_edit.php]http://www.spirestest.com/nicky/gallery/select_edit.php[/url]

Thank for all your help.
Link to comment
Share on other sites

you should follow pixy's practice, but for now, you can use these to test the code:

$id = $_GET['id'];
$name = $_GET['name'];
$description = $GET['description'];

put these in edit.php before printing out the values.
Link to comment
Share on other sites

Hi

sorry, No luck.
(what is pixy's practise?)


I know the $id is getting through, is just the other values.
this is the code for my edit page
[code]


<?php
include('db.php');
echo $id;
$arrErrors = array();

if (!empty($_POST['submit'])) {
if ($_POST['description']=='')
$arrErrors['description'] = 'Description.';
if ($_POST['name']=='')
$arrErrors['name'] = 'Name.';


if (count($arrErrors) == 0) {

$id = $_GET['id'];
$name = $_GET['name'];
$description = $_GET['description'];






$result = mysql_query("UPDATE image SET description = '$description', name = '$name' WHERE  id = '$id' LIMIT 1") or die(mysql_error());
if ($result) {
$link_home = '<div class="error">Your details have been updated.<br><a href="select_edit.php" class="link">Return to edit list</a>.';

} else {
$UNtaken = '<div class="error">Sorry, Your details have not been updated.';
}


}  else {
if (empty($title) || empty($art)) {
$strError = '<div class="error">error';
      foreach ($arrErrors as $error) {
          $strError .= "<li>$error</li>";
      }
      $strError .= '</div>';
}



}

}




?>


<html>
<head>
<title>New News</title>

<link href="../css/nicky.css" rel="stylesheet" type="text/css">
</head>

<body>

<center>
<a href="select_edit.php" class="link">back</a>
<table width="250px" bgcolor="#EEEEEE" class="TLRB_border">
<tr>
<td>
<form name="form1" method="post" action="<?php echo $PHP_SELF; ?>">
<?php echo "<INPUT type='hidden' name='id' value='".$_POST['id']."'>"; ?>
<table width="190px" align="center" bgcolor="#FFFFFF">
<tr>
<td colspan="2">Edit News</td>
</tr>
<tr>
<td class="loginBox_text">Name:</td>
<td>
<input name="name" Type="text" id="name" size="15" value="<?php echo $_POST['name']?>">
</td>
</tr>
<tr>
<td class="loginBox_text">Description:</td>
<td>
<textarea name="description" cols="30" wrap="PHYSICAL" id="description" value="<?php echo $_POST['description']?>"></textarea>
</td>
</tr>
<td colspan="2" align="right">
<input type="submit" name="submit" value="Submit">
</td>
</table>
</form>
</td>
<tr>
<td>

<center>
<?php echo $strError; ?>
<?php echo $UNtaken; ?>
<?php echo $link_home; ?>
<?php echo $hitch; ?>
<?php echo $final; ?>
</center>



</td>
</tr>
</tr>
</table>

</center>

</body>
</html>
[/code]
Link to comment
Share on other sites

Can you give us a sample link? Maybe it's going over the maximum number of characters.

Whenever you're using $_GET method to get variables, check that they're set first.
if (isset($_GET['id'])) {
    if (!is_numeric($_GET['id'])) {
        echo 'You are using an invalid id';
        // include footers
        die();
    }
    else {
        $is = $_GET['id'];
    }
}

That, of course, only works when you're doing numeric values, but it's the same principle.
Link to comment
Share on other sites

Hi

Heres a sample link [url=http://www.spirestest.com/nicky/gallery/select_edit.php]http://www.spirestest.com/nicky/gallery/select_edit.php[/url]
Then click on edit and edit the details.

Delete works fine.

i have echoed them out on the edit page. which means that they are getting through.

I just i cant seem to edit the details in the database?
Link to comment
Share on other sites

OK, the values are now displaying inside the text feilds.

and when you click submit, it displays the new code in the top left hand corner. (as it should)

But, its not affecting the database.
[url=http://www.spirestest.com/nicky/gallery/select_edit.php]http://www.spirestest.com/nicky/gallery/select_edit.php[/url]

Any more ideas?

[code]
<?php
include('db.php');
echo $id;
echo $name;
echo $description;

$arrErrors = array();

if (!empty($_POST['submit'])) {
if ($_POST['description']=='')
$arrErrors['description'] = 'Description.';
if ($_POST['name']=='')
$arrErrors['name'] = 'Name.';


if (count($arrErrors) == 0) {

$id = $_GET['id'];
$name = $_GET['name'];
$description = $_GET['description'];






$result = mysql_query("UPDATE image SET description = '$description', name = '$name' WHERE  id = '$id' LIMIT 1") or die(mysql_error());
if ($result) {
$link_home = '<div class="error">Your details have been updated.<br><a href="select_edit.php" class="link">Return to edit list</a>.';

} else {
$UNtaken = '<div class="error">Sorry, Your details have not been updated.';
}


}  else {
if (empty($title) || empty($art)) {
$strError = '<div class="error">error';
      foreach ($arrErrors as $error) {
          $strError .= "<li>$error</li>";
      }
      $strError .= '</div>';
}



}

}




?>


<html>
<head>
<title>New News</title>

<link href="../css/nicky.css" rel="stylesheet" type="text/css">
</head>

<body>

<center>
<a href="select_edit.php" class="link">back</a>
<table width="250px" bgcolor="#EEEEEE" class="TLRB_border">
<tr>
<td>
<form name="form1" method="post" action="<?php echo $PHP_SELF; ?>">
<?php echo "<INPUT type='hidden' name='id' value='".$_POST['id']."'>"; ?>
<table width="190px" align="center" bgcolor="#FFFFFF">
<tr>
<td colspan="2">Edit News</td>
</tr>
<tr>
<td class="loginBox_text">Name:</td>
<td>
<input name="name" Type="text" id="name" size="15" value="<?php echo $name; ?>">
</td>
</tr>
<tr>
<td class="loginBox_text">Description:</td>
<td>
<textarea name="description" cols="30" wrap="PHYSICAL" id="description" ><?php echo $description; ?></textarea>
</td>
</tr>
<td colspan="2" align="right">
<input type="submit" name="submit" value="Submit">
</td>
</table>
</form>
</td>
<tr>
<td>

<center>
<?php echo $strError; ?>
<?php echo $UNtaken; ?>
<?php echo $link_home; ?>
<?php echo $hitch; ?>
<?php echo $final; ?>
</center>



</td>
</tr>
</tr>
</table>

</center>

</body>
</html>
[/code]
Link to comment
Share on other sites

Instead of relying on links to edit the name and description, pull it out of the database.

[code]
<?php
include("db.php");
// I dont know what the echo $id is for...
if (isset($_GET['id'])) {
   if (!is_numeric($_GET['id'])) {
       die('You are trying to use an invalid link');
   }
   else {
       $id = $_GET['id'];
   }
}

if (isset($_POST['submitted'])) {
   $errors = array();
   if (empty($_POST['name'])) {
       $errors[] = 'You did not enter a name for the image.';
   }
   else {
       $name = $_POST['name'];
   }
   if (empty($_POST['desc'])) {
       $errors[] = 'You did not enter a description';
   }
   else {
        $desc = $_POST['desc'];
   }
   $id = $_POST['id'];
   if (empty($errors)) { // No errors, so update DB
       $query = "UPDATE image SET name='$name', description='$desc' WHERE id='$id'";
       $result = mysql_query($query);
       if ($query) {
            echo 'The image named '.$name.' was sucessfully updated.';
       }
       else {
            echo mysql_error();
       }
   }
   else {
        foreach ($errors as $msg) {
            echo '<li> '.$msg.'</li>';
        }
   }
}
else {
   $query = "SELECT name, description FROM image WHERE id='$id'";
   $result = mysql_query($query);
   if (mysql_num_rows($result) == 1) {
       $row = mysql_fetch_array($result, MYSQL_NUM);
       echo '<form action="thisfile.php" method="post">
       <b>Name:</b> <input type="text" name="name" value="'.$row[0].'"><br>
       <b>Description:</b><br>
       <textarea name="desc">'.$row[1].'</textarea><br>
       <input type="submit" name="submit" value="Submit">
       <input type="hidden" name="id" value="'.$id.'">
       <input type="hidden" name="submitted" value="TRUE">
       </form>';
   }
   else {
       echo 'We could not select that from the database.';
   }
}
?>
[/code]
Link to comment
Share on other sites

Just relying on $_GET too much. When I first started coding I thought that using the $_GET method looked cool so I tried to use it with EVERYTHING. O_O

You should consider actually spacing your code in a reasonable way. It looks like it's all just floating around in cyberspace--it makes it much easier to debug.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.