Forum Moderators: coopster

Message Too Old, No Replies

$ GET["name"] problem.

I'm beginer,need help.

         

Dragosh

12:51 pm on Aug 7, 2007 (gmt 0)

10+ Year Member



The first file is called 1.php and it looks like this :

<html>

<SCRIPT language="JavaScript">
function submitform()
{
document.myform.submit();
}
</SCRIPT>
See interesting facts from this category: <br />
<form name="myform" action="delet.php" method="get">
<?php

$con= mysql_connect("localhost","root","");

if (!$con)
{
die('Could not connect to the database' . mysql_error());
}

mysql_select_db("facts", $con);

$result=mysql_query("select * from content");
while($row=mysql_fetch_array($result))
{

$id=$row['id'] ;
echo $row['id']. '. '. $row['facts'] . ' <input type="button" value="Delete" name="'.$id.'" onclick="javascript: submitform()" /><br />' ;

}
mysql_close($con);
?>

</form>
</html>


The second file is called delet.php :

<?php

if(isset($_GET["name"])) { $a=$_GET["name"];
$con= mysql_connect("localhost","root","");

if (!$con)
{
die('Could not connect to the database' . mysql_error());
}

mysql_select_db("facts", $con);

$delete=mysql_query("delete from content where id='$a'") ;
if (!$delete)
{
die('Could not delete row');
}

mysql_close($con); } else echo 'Error!';
?>

Actually what i want to do with these files is to delete rows from the database. Everything works perfect except $_GET["name"] . delet.php just doesnt want to take the information from the form in 1.php .
Somebody tell me please what i did wrong. Thanks in advance!

Habtom

12:56 pm on Aug 7, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Assuming everything else is correct:
<input type="button" value="Delete" name="'.$id.'"

You don't have a "name"
$_GET["name"]

The name of that input is having a variable $id
$_GET['{here goes there value of $id}'] should work.

Habtom

Habtom

12:57 pm on Aug 7, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



. . . and welcome to WebmasterWorld.

Dragosh

1:11 pm on Aug 7, 2007 (gmt 0)

10+ Year Member



I dont understand what i should change. Please explain a little bit easier.
PS: Thanks, i see this site is very good :)

Habtom

1:17 pm on Aug 7, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In the first file, change this part:
<input type="button" value="Delete" name="'.$id.'" onclick="javascript: submitform()" />

To This:
<input type="button" value="'.$id.'" name="Delete" onclick="javascript: submitform()" />

In the second file, change:
if(isset($_GET["name"])) { $a=$_GET["name"];

To
if(isset($_GET["Delete"])) { $a=$_GET["Delete"];

When you understand the changes, you can change the names as well, but what is mentioned above should solve your problem as it is. If you face any problem, please post it.

Habtom

Dragosh

1:22 pm on Aug 7, 2007 (gmt 0)

10+ Year Member



I think you didnt understand what my script does. I want this script to (1.php) select every row from the database and echo it on the screen with a button "Delete" after it. When i press Delete the id of the row is sent to delet.php which takes the id and executes the mysql command and deletes the respective row with the respective id.
PS: Thanks for quick help ;)

Dragosh

1:25 pm on Aug 7, 2007 (gmt 0)

10+ Year Member



when i click the button instead of getting http://www.example.com/facts/delet.php?name=6 i only get
http://www.example.com/facts/delet.php?

[edited by: dreamcatcher at 2:05 pm (utc) on Aug. 7, 2007]
[edit reason] Use example.com, thanks. [/edit]

WesleyC

1:28 pm on Aug 7, 2007 (gmt 0)

10+ Year Member



No, he is correct. I think you're mixing up the name and value fields--the "name" attribute is how you'll reference the input element in the second form, and the "value" attribute is the actual value your form processing page will receive.

Also, I would recommend doing an intval() or mysql_real_escape_string() on the input before you use it in your delete query--otherwise a malicious user could end up deleting much more than one row. :)

Dragosh

1:30 pm on Aug 7, 2007 (gmt 0)

10+ Year Member



Actually only me will have access to delete rows. but i still dont understand: i followed Habtom's advice but it just doesnt work. :(

Habtom

1:40 pm on Aug 7, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



it just doesnt work.

What do you get? What is not working?

[edited by: Habtom at 1:41 pm (utc) on Aug. 7, 2007]

Dragosh

1:43 pm on Aug 7, 2007 (gmt 0)

10+ Year Member



second file doesnt want to take the value from the first one. can it be the problem in the submit mode? is it corectly written? when i hit the button is the value taken?

Dragosh

1:44 pm on Aug 7, 2007 (gmt 0)

10+ Year Member



Or maybe somebody has an already done script to delete rows from a database, coz the script from spoono doesnt work for me.

Habtom

1:46 pm on Aug 7, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<input type="button" value="Delete" name="'.$id.'" onclick="javascript: submitform()" />

It seems the loop is going to generage a number of buttons.

Instead of the above line, see if the following works, it will provide you with a text link to delete the row.

<a href="delet.php?name=$id"> Delete </a>

Habtom

1:48 pm on Aug 7, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



second file doesnt want to take the value from the first one. can it be the problem in the submit mode? is it corectly written? when i hit the button is the value taken?

Did you change this part?
if(isset($_GET["Delete"])) { $a=$_GET["Delete"];

is the URL carrying the value?

Dragosh

1:51 pm on Aug 7, 2007 (gmt 0)

10+ Year Member



Thanks a lot Habtom. The link really worked! i just changed to be like this :

<a href="delet.php?name='. $id .'"> Delete </a>

It doesnt look professional, but it works!
Thanks again!

WesleyC

2:04 pm on Aug 7, 2007 (gmt 0)

10+ Year Member



If you must have it as a button...

'<input type="button" onclick="window.location="delet.php?id='.$id.'>'

dreamcatcher

2:07 pm on Aug 7, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



And you can use print_r to view your incoming array.

echo '<pre>';
print_r [uk2.php.net]($_GET);
echo '</pre>';

And set your error reporting to E_ALL, that will help you debug many issues early on.

dc

Dragosh

2:18 pm on Aug 7, 2007 (gmt 0)

10+ Year Member



WesleyC it didnt work. I think text link looks better ;)
dreamcatcher, well im a newbie. more than 75% of what you told me i didnt understand :))

Dragosh

2:21 pm on Aug 7, 2007 (gmt 0)

10+ Year Member



Thanks for helping me all.
Now a little question not on theme : where could i get some tutorials on how to make templates for php? i could find on w3schools.
Thanks all!

dreamcatcher

6:52 pm on Aug 7, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



dreamcatcher, well im a newbie. more than 75% of what you told me i didnt understand happy!

Not a problem. Basically, $_GET is an array. Not sure how familiar you are with arrays, but when working with them, you can always see the values of arrays by using the above code. This is sometimes useful when using any of the superglobal arrays that PHP has. For information see the following:

[uk2.php.net...]

When you are coding, popping the following at the top of your script after the opening <?php tag activates error reporting:

error_reporting(E_ALL);

You`ll find that useful for future projects because anytime an error is encountered thats PHP related, the error will be shown on the screen. Sometimes a quick look at the error message can help you debug an issue, rather than staring at your code for hours and hours without any clues. You can also activate this error reporting level in your PHP.ini file if you have access to that. This is your server PHP config file.

You should always try and code your projects to run under E_ALL if possible. In the long run it will make you a better programmer.

Good luck,

dc

Dragosh

5:20 am on Aug 8, 2007 (gmt 0)

10+ Year Member



E_ALL is activated in php.ini