desjardins2010 Posted January 5, 2011 Share Posted January 5, 2011 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> Quote Link to comment Share on other sites More sharing options...
Zurev Posted January 5, 2011 Share Posted January 5, 2011 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'])). Quote Link to comment Share on other sites More sharing options...
desjardins2010 Posted January 5, 2011 Author Share Posted January 5, 2011 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? Quote Link to comment Share on other sites More sharing options...
Zurev Posted January 5, 2011 Share Posted January 5, 2011 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"'> Quote Link to comment Share on other sites More sharing options...
desjardins2010 Posted January 5, 2011 Author Share Posted January 5, 2011 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 Quote Link to comment Share on other sites More sharing options...
Zurev Posted January 5, 2011 Share Posted January 5, 2011 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. Quote Link to comment Share on other sites More sharing options...
desjardins2010 Posted January 5, 2011 Author Share Posted January 5, 2011 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 Quote Link to comment Share on other sites More sharing options...
Zurev Posted January 5, 2011 Share Posted January 5, 2011 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. Quote Link to comment Share on other sites More sharing options...
desjardins2010 Posted January 5, 2011 Author Share Posted January 5, 2011 AHHHHHH ok, let me this this... sorry i couldn't wrap my head about this one! Quote Link to comment Share on other sites More sharing options...
desjardins2010 Posted January 5, 2011 Author Share Posted January 5, 2011 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>"; Quote Link to comment Share on other sites More sharing options...
Zurev Posted January 5, 2011 Share Posted January 5, 2011 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>"; Quote Link to comment Share on other sites More sharing options...
desjardins2010 Posted January 5, 2011 Author Share Posted January 5, 2011 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; } ?> Quote Link to comment Share on other sites More sharing options...
desjardins2010 Posted January 5, 2011 Author Share Posted January 5, 2011 what I would like to see is it delete the entry and then foward them back to list of ips and THERE show the message IP deleted... Quote Link to comment Share on other sites More sharing options...
Zurev Posted January 5, 2011 Share Posted January 5, 2011 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. Quote Link to comment Share on other sites More sharing options...
desjardins2010 Posted January 5, 2011 Author Share Posted January 5, 2011 tried the header redirect and it redirected me before even doing anything cause if i'm not mistaken you have to declare the header prior to any other text right? Quote Link to comment Share on other sites More sharing options...
Zurev Posted January 5, 2011 Share Posted January 5, 2011 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. Quote Link to comment Share on other sites More sharing options...
btherl Posted January 5, 2011 Share Posted January 5, 2011 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). Quote Link to comment Share on other sites More sharing options...
desjardins2010 Posted January 6, 2011 Author Share Posted January 6, 2011 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 Quote Link to comment Share on other sites More sharing options...
btherl Posted January 6, 2011 Share Posted January 6, 2011 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. Quote Link to comment Share on other sites More sharing options...
desjardins2010 Posted January 6, 2011 Author Share Posted January 6, 2011 well this is the output About to execute query: DELETE FROM ipdatabase WHERE userid='22' AND ipaddress='232.143.196.91' limit 1 Query succeeded and go into db and the entry is still there I only have three entries all with id 22 different ip addresses... don't get it Quote Link to comment Share on other sites More sharing options...
desjardins2010 Posted January 6, 2011 Author Share Posted January 6, 2011 ok, so I figured out one part I was comparing long2ip to db that is ip2long Quote Link to comment Share on other sites More sharing options...
btherl Posted January 6, 2011 Share Posted January 6, 2011 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. Quote Link to comment Share on other sites More sharing options...
Zurev Posted January 6, 2011 Share Posted January 6, 2011 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.