Jump to content

trying to figure something out here.


desjardins2010

Recommended Posts

hey all;

 

i created a script that just allows a user to record an IP address and it presents them in a table..

 

issue: each record draws its own row and a colum for an image (for deleting).. I can't igure out how I can delete the one row when they click on image(little x) to delete...

 

issue2: it's showing the error message when you bring up the screen telling them they didn't enter anything even prior to them hitting submit...

 

issue3: it's allowing me to enter the same IP again

 

code:


<?php
session_start();
if (!isset($_SESSION['username'])) {
echo "Sorry you must be logged in to view this page<BR>";
echo "Please <a href='index.php'>GO BACK</a> and try again";
exit();
}
else {
if ($_SESSION['username']) {
$username = $_SESSION['username'];
$userid = $_SESSION['userid'];
}
}
//script v1.0 ipdatabase.php - create a list of IP addresses that the user has added to their list to remember. can add or remove IPs
?>
<style type="text/css">
<!--
.style1 {color: #FFFFFF}
-->
</style>

<table align="center" width="550" border="0" cellpadding="0" cellspacing="0" bgcolor="#000000">
  <!--DWLayoutTable-->
  <tr>
    <td height="106" colspan="3" valign="top"><img src="images/myvpc.png" width="550" height="106"></td>
  </tr>
  <tr>
    <td width="16" height="57"> </td>
    <td width="516" valign="top"><div align="center" class="style1">
      <p>IP DATABASE</p>
      <p align="left">add or remove IP address you have encountered in the game thus far for later use!</p>
    </div></td>
    <td width="18"> </td>
  </tr>
  <tr>
    <td height="100"> </td>
    <td valign="top">
      <form action="ipaddressadd.php" method="POST">
        <p> </p>
        <p><span class="style1">IP ADDRESS</span>:
          <input type="text" name="ipaddress" size="16">
          <input type="submit" name="submit" value="submit">
          </p>
      </form></td>
    <td> </td>
  </tr>
  <tr>
    <td height="17"></td>
    <td valign="top"><img src="images/filler.jpg" width="516" height="19"></td>
    <td></td>
  </tr>
  <tr>
    <td height="296"></td>
    <td valign="top">
      <?php
$submit = $_POST['submit'];
if ($submit) {
 $ipaddress = ip2long($_POST['ipaddress']);
 }
//script to add IP to database
include('connectdb.php');

mysql_select_db('heaven_users') or die (mysql_error());

if (isset($ipaddress,$submit)) {


mysql_query("INSERT INTO ipdatabase (userid,ipaddress) VALUE ('$userid','$ipaddress')") or die ("Could Not write to database, Please try again");
}
else
echo "<center><font color='#ff0000'>You didn't add anything mate!</font></center>";
echo "<br />";


echo "<table align='center' border='1'><tr align='center'><th><font color='#ffffff'>SAVED IPADDRESS</font></th></tr><tr>";

$query = mysql_query("SELECT * FROM ipdatabase WHERE userid = '$userid'");
while ($result = mysql_fetch_assoc($query)) {
$ip = long2ip($result['ipaddress']);
echo "<tr align=\"center\"><td><font color='#ffffff'>".$ip."</font></td><td><a href='deleteip.php'><img border='0' src='images/delete.png'></a></td></tr>";
}
echo "</table>";


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

Link to comment
Share on other sites

As for the issue of deleting a single image, why don't you make deleteip.php have a GET variable for the ip address/userID? That way you could avoid any users having the same IP address in the list collision? So it would be deleteip.php?uid=1&ip=123.456.78.21 ish.

 

And then you select all records with that user id and that ip address and delete it.

 

As for issue 2, you're just checking if ($_POST['submit'], which is going to return true as long as it exists, do if (isset($_POST['submit'])).

 

 

Link to comment
Share on other sites

sounds about right for the deleteip.php however it's just an image of an x that I'm using to simulate a del button... so when thats clicked how can I set varibles with it like a form?

 

You mean changing the link to something like so:

<a href='deleteip.php?uid=".$selectedID."&ip=".$ip"'>

Link to comment
Share on other sites

ok, so lets assume row1 colum 1 has an IP as 1.1.1.1 how can I when I click the image to go to deleteip.php pass that colums value to the script? you say use ?uid='.$selectID."$ip =".$ip''> where does ip get set

 

Well it's getting passed through the URL, the url will have the user ID and the ip address, and then the deleteip file can make use of that.

 

So say for instance you're in your deleteip.php file, you would retrieve the userID by calling $_GET['uid'], and the ip address by calling $_GET['ip'].

 

Then you can do whatever you need to do, I think for your purpose, you would want to select all rows with the userID $_GET['uid'] AND the ipaddress $_GET['ip'].

 

Note, you'll want to run some validation so not just anything can get passed in your URL's via $_GET.

Link to comment
Share on other sites

i'm still confused on how you plan on using $_GET with a field that is sitting in a table... when I click on the link to delete how does it know first of all to take the IP from the colum... say there is a table with two colums one is an IP address and the other an img of an x to delete... when I click the x img how does it now what colms x i'm pressing.. i could be really confised here but I don't see away to set the value of IP without hitting a submit button and capturing a name feild

Link to comment
Share on other sites

i'm still confused on how you plan on using $_GET with a field that is sitting in a table... when I click on the link to delete how does it know first of all to take the IP from the colum... say there is a table with two colums one is an IP address and the other an img of an x to delete... when I click the x img how does it now what colms x i'm pressing.. i could be really confised here but I don't see away to set the value of IP without hitting a submit button and capturing a name feild

 

Where it links is in the middle of a while loop, in the way it loops through each of the results, you're getting a different rows results each iteration. Meaning if you put in your while loop:

<a href="$ip">$ip</a>

It's going to make a link linking to that ip every single iteration, each one different (given the data is unique). So if your link includes $ip in it, it's going to be a different link each time. How it deletes that specific one would be in your deleteip.php file.

Link to comment
Share on other sites

ok, so whats wrong with this... it's erroring out saying unexpected .;'

 

while ($result = mysql_fetch_assoc($query)) {
$ip = long2ip($result['ipaddress']);
echo "<tr align='center'><td><font color='#ffffff'>".$ip."</font></td><td><a href='deleteip.php?uid=".$userid."&ip=".$ip"><img border='0' src='images/delete.png'></a></td></tr>";

Link to comment
Share on other sites

ok, so whats wrong with this... it's erroring out saying unexpected .;'

 

while ($result = mysql_fetch_assoc($query)) {
$ip = long2ip($result['ipaddress']);
echo "<tr align='center'><td><font color='#ffffff'>".$ip."</font></td><td><a href='deleteip.php?uid=".$userid."&ip=".$ip"><img border='0' src='images/delete.png'></a></td></tr>";

 

Missing the concatenation symbol . after your second $ip variable. Actually since the echo is in double quotes you don't even have to escape them to echo your variables.

 

You could do:

echo "<tr align='center'><td><font color='#ffffff'>$ip</font></td><td><a href='deleteip.php?uid=$userid&ip=$ip><img border='0' src='images/delete.png'></a></td></tr>";

Link to comment
Share on other sites

alright so thats working great... it's passing the right values across to deleteip.php I created deleteip.php quickly here is what I got.. it's saying deleted IP but it's not getting deleted from the db??

 

<?php
session_start();
if (!isset($_SESSION['username'])) {
echo "Sorry you must be logged in to view this page<BR>";
echo "Please <a href='index.php'>GO BACK</a> and try again";
exit();
}
else {
if ($_SESSION['username']) {
$username = $_SESSION['username'];
$userid = $_SESSION['userid'];
}
}

$ip = $_GET['ip'];

//deleted selected ip from selected users list
include('connectdb.php');

mysql_select_db('heaven_users') or die (mysql_error());

$delete = mysql_query("DELETE FROM ipdatabase WHERE userid='$userid' AND ipaddress='$ip' limit 1") or die (mysql_error()); 

if ($delete) {
echo "<font color='#ff0000'>Deleted IP</font>";
echo "<br />";
echo $ip;
echo "<br />";
echo $userid;
}

?>

Link to comment
Share on other sites

I can't seem to see why it wouldn't be deleting it from the database, if it says it was deleted then the query is being performed....

 

As for redirecting it back, you can do a simple header redirect, then set a session like $_SESSION['notice'] and make it equal to "IP Address successfully deleted.".

 

Back on your main page perform a check if the notice session is set, echo it out. That will work for anything you want really.

 

Link to comment
Share on other sites

Well ob_start at the top of the script handles the issue of it being able to be declared at any time.

 

Though if you call a redirect with header in the lines after you set a session, it will set the session first then redirect, to my knowledge.

Link to comment
Share on other sites

The header redirect must be put first, but it will not take effect until your script finishes.  I would try printing out your SQL to make sure it's correct (make sure you disable the header redirect when doing this or you won't see it).

Link to comment
Share on other sites

I don't understand what you mean about printing my sql? if I put header redirect at the top of the screen it will finsh the script prior to redirect?

 

and Zurev when you say to start a session for the notice in my if statement? like if ($delete) {$_SESSION['notice'];}

 

then in the main file use $_SESSION['notice']; = "Your IP have been removed!";

 

or do I need to use session_start('notice') in the deleteip.php

 

and dunno why string looks good but it;s not deleting the entry from db though it;s saying it is?

 

thanks guys

Link to comment
Share on other sites

What I mean is this:

 

$sql = "DELETE FROM ipdatabase WHERE userid='$userid' AND ipaddress='$ip' limit 1");
print "About to execute query: $sql<br>"; 
$delete = mysql_query($sql) or die (mysql_error());
print "Query succeeded";
exit(0);

 

Once your debugging is done you can remove the prints and the exit.  I would also check to make sure there are not duplicate entries in your ipdatabase table with the same userid and ip address.

 

Yes, the script will finish before the redirect.  That is my experience anyway.  If you produce a lot of output and your web server sends that output before your script has finished, you might get redirected before your script finishes.

 

Safest would be to use ob_start() as Zurev suggested.

 

I also agree with Zurev that setting the session variables and then redirecting will work.

 

Link to comment
Share on other sites

Hmm.. very strange.  Try adding this code directly after the delete:

 

$sql = "SELECT * FROM ipdatabase WHERE userid='$userid' AND ipaddress='$ip'";
print "About to execute query: $sql<br>"; 
$select = mysql_query($sql) or die(mysql_error());
while ($row = mysql_fetch_array($select)) {
    print $row['userid'] . ":" . $row['ipaddress'] . "<br>";
}

 

The reason is to check if the row is gone immediately after you delete it, because it may be that it gets deleted and then added back later.  Once we're 100% sure the delete is working properly then we can look elsewhere for the bug.

Link to comment
Share on other sites

I don't understand what you mean about printing my sql? if I put header redirect at the top of the screen it will finsh the script prior to redirect?

 

and Zurev when you say to start a session for the notice in my if statement? like if ($delete) {$_SESSION['notice'];}

 

then in the main file use $_SESSION['notice']; = "Your IP have been removed!";

 

or do I need to use session_start('notice') in the deleteip.php

 

and dunno why string looks good but it;s not deleting the entry from db though it;s saying it is?

 

thanks guys

 

With the session I meant in your deleteip file, after the if ($delete) do something like...

$_SESSION['notice'] = "IP Address removed successfully.";

 

Then on your main page do a check if that session exists, if so echo it out.

 

if (isset($_SESSION['notice']))
{
echo $_SESSION['notice'];
}

 

That way you can use it for more notices in the future, know that if you set something to the notice session, the user will be notified of it when they go to that page.

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.