You are here: Re: variable problem in address book script « All PHP « IT news, forums, messages
Re: variable problem in address book script

Posted by Mr. Newt on 01/01/07 17:23

"Geoff Berrow" <blthecat@ckdog.co.uk> wrote in message
news:m7eip2tctq0975ldcftstek1k0gb036ict@4ax.com...
> Message-ID: <r9OdnVl04t_fqwTYnZ2dnUVZ_sudnZ2d@adelphia.com> from Mr.
> Newt contained the following:
>
>>> $mode=$_POST['mode'];
>>> $name=$_POST['name'];
>>> $phone=$_POST['phone'];
>>> $email=$_POST['email'];
>>> $id=$_POST['id'];
>>>
>>>
>>> --
>>>
>>> //Aho
>>
>>Thanks guys. When I tried # 3 above, the error "Undefined index: mode"
>>popped up.
>>When I tried # 2, the error was "Undefined variable: mode." The remove
>>function worked though. =)
>>#1 same as # 2 (so I turned it back off).
>
> They should be $_GET not $_POST
>
> try this:
> $mode=(isset($_GET['mode']))? $_GET['mode']) : "" ;
>
> (and similar for all the other variables)
>
> I suspect the error "Undefined variable: mode." is more to do with
> faults in the original script and error reporting set to E_ALL than your
> setup.
>
>
>
> --
> Geoff Berrow 0110001001101100010000000110
> 001101101011011001000110111101100111001011
> 100110001101101111001011100111010101101011

I added the following:

$mode=(isset($_GET['mode']))?($_GET['mode']) : "" ;
$id=(isset($_GET['id']))?($_GET['id']) : "" ;
$name=(isset($_GET['name']))?($_GET['name']) : "" ;
$phone=(isset($_GET['phone']))?($_GET['phone']) : "" ;
$email=(isset($_GET['email']))?($_GET['email']) : "" ;
$info=(isset($_GET['info']))?($_GET['info']) : "" ;

The only error message I get now is "PHP Warning: Module 'mysql' already
loaded in Unknown on line 0." However, add, edit still don't work. I
receive an error message saying page can't be displayed and the url shows
PHP_SELF (it shows the correct directory before that). Should I change
***************
Print "<td><a href=" .$_SERVER['PHP_SELF']. "?id=" . $info['id'] ."&name=" .
$info['name'] . "&phone=" . $info['phone'] ."&email=" . $info['email'] .
"&mode=edit>Edit</a></td>";
Print "<td><a href=" .$_SERVER['PHP_SELF']. "?id=" . $info['id']
.."&mode=remove>Remove</a></td></tr>";
**************
too? I assume this is where the url is derived?

I'll post my current script, just in case I've done something wrong. Thanks
again. Robert
**************
<html>

<head>
<title>Address Book</title>
</head>
<body>

<?php

// Connects to your Database [br]
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("test") or die(mysql_error());

$mode=(isset($_GET['mode']))?($_GET['mode']) : "" ;
$id=(isset($_GET['id']))?($_GET['id']) : "" ;
$name=(isset($_GET['name']))?($_GET['name']) : "" ;
$phone=(isset($_GET['phone']))?($_GET['phone']) : "" ;
$email=(isset($_GET['email']))?($_GET['email']) : "" ;
$info=(isset($_GET['info']))?($_GET['info']) : "" ;


//add mode adds a form
if ( $mode=="add")
{
Print '<h2>Add Contact</h2>
<p>
<form action=';
echo 'PHP_SELF';
Print '
method=post>
<table>
<tr><td>Name:</td><td><input type="text" name="name" /></td></tr>
<tr><td>Phone:</td><td><input type="text" name="phone" /></td></tr>
<tr><td>Email:</td><td><input type="text" name="email" /></td></tr>
<tr><td colspan="2" align="center"><input type="submit" /></td></tr>
<input type=hidden name=mode value=added>
</table>
</form> <p>';
}

//added mode adds the data
if ( $mode=="added")
{
mysql_query ("INSERT INTO address (name, phone, email) VALUES ('$name',
'$phone', '$email')");
}

//edit mode adds a pre-populated form
if ( $mode=="edit")
{
Print '<h2>Edit Contact</h2>
<p>
<form action=';
echo 'PHP_SELF';
Print '
method=post>
<table>
<tr><td>Name:</td><td><input type="text" value="';
Print $name;
print '" name="name" /></td></tr>
<tr><td>Phone:</td><td><input type="text" value="';
Print $phone;
print '" name="phone" /></td></tr>
<tr><td>Email:</td><td><input type="text" value="';
Print $email;
print '" name="email" /></td></tr>
<tr><td colspan="2" align="center"><input type="submit" /></td></tr>
<input type=hidden name=mode value=edited>
<input type=hidden name=id value=';
Print $id;
print '>
</table>
</form> <p>';
}

//edited mode updates the data for a given ID
if ( $mode=="edited")
{
mysql_query ("UPDATE address SET name = '$name', phone = '$phone', email =
'$email' WHERE id = $id");
Print "Data Updated!<p>";
}

//Remove mode removes data for a given ID
if ( $mode=="remove")
{
mysql_query ("DELETE FROM address where id=$id");
Print "Entry has been removed <p>";
}

//This pulls the data and puts it into an array, then prints in alphabetical
order based on name
$data = mysql_query("SELECT * FROM address ORDER BY name ASC")
or die(mysql_error());
Print "<h2>Address Book</h2><p>";
Print "<table border cellpadding=3>";
Print "<tr><th width=100>Name</th><th width=100>Phone</th><th
width=200>Email</th><th width=100 colspan=2>Admin</th></tr>";
Print "<td colspan=5 align=right><a href=" .$_SERVER['PHP_SELF'].
"?mode=add>Add Contact</a></td>";
while($info = mysql_fetch_array( $data ))
{
Print "<tr><td>".$info['name'] . "</td> ";
Print "<td>".$info['phone'] . "</td> ";
Print "<td> <a href=mailto:".$info['email'] . ">" .$info['email'] .
"</a></td>";
Print "<td><a href=" .$_SERVER['PHP_SELF']. "?id=" . $info['id'] ."&name=" .
$info['name'] . "&phone=" . $info['phone'] ."&email=" . $info['email'] .
"&mode=edit>Edit</a></td>";
Print "<td><a href=" .$_SERVER['PHP_SELF']. "?id=" . $info['id']
.."&mode=remove>Remove</a></td></tr>";
}
Print "</table>";
?>


</body>
</html>

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация