Porkie Posted June 23, 2009 Share Posted June 23, 2009 hey im abit confused at what line of code i would use to have my database delete a record everytime one is added? can someone please set me straight ? cheers in advance Link to comment https://forums.phpfreaks.com/topic/163414-delete-data-from-mysql-databse-usign-php/ Share on other sites More sharing options...
flyhoney Posted June 23, 2009 Share Posted June 23, 2009 <?php $link = mysql_connect('localhost', 'mysql_user', 'mysql_password'); if (!$link) { die('Not connected : ' . mysql_error()); } // make foo the current db $db_selected = mysql_select_db('foo', $link); if (!$db_selected) { die ('Can\'t use foo : ' . mysql_error()); } $query = "DELETE FROM table WHERE id = 121"; if (mysql_query($query)) { echo 'successfully deleted'; } Link to comment https://forums.phpfreaks.com/topic/163414-delete-data-from-mysql-databse-usign-php/#findComment-862198 Share on other sites More sharing options...
Porkie Posted June 23, 2009 Author Share Posted June 23, 2009 where id = 121 ? what does that do? sorry thanks for help Link to comment https://forums.phpfreaks.com/topic/163414-delete-data-from-mysql-databse-usign-php/#findComment-862219 Share on other sites More sharing options...
Porkie Posted June 23, 2009 Author Share Posted June 23, 2009 also if i put ASC after the 121 will it delete like the oldest record 1st ? cheers Link to comment https://forums.phpfreaks.com/topic/163414-delete-data-from-mysql-databse-usign-php/#findComment-862231 Share on other sites More sharing options...
pkedpker Posted June 23, 2009 Share Posted June 23, 2009 Your question doesn't even make sense logically speaking "i would use to have my database delete a record everytime one is added?" the flaw in that question is deleting a record everytime one is added.. thats not the best logic.. the correct logic is figuring out why the records come in and stop them permanently... Step one would be delete the TABLE completely.. if any errors occur in your script you will know which script gives the tables you didn't want.. and delete those too. Why let something run for no reason thats so stupid seriously it makes me wonder sometimes... Link to comment https://forums.phpfreaks.com/topic/163414-delete-data-from-mysql-databse-usign-php/#findComment-862238 Share on other sites More sharing options...
Porkie Posted June 23, 2009 Author Share Posted June 23, 2009 mysql_select_db("", $con); if($row = mysql_fetch_array($ip)) [color=red]how could i make it so if the row exists it adds 1 to pageviews?[/color] else mysql_query("INSERT INTO UserINFO (IP, DATE, TIME) VALUES ('$ip', '$date', '$time')"); $query = "DELETE FROM table WHERE id = 500 ASC"; if (mysql_query($query)); mysql_close($con); ?> cheers it doesnt matter about the posts before i sort off wrote it wrong sorry. Link to comment https://forums.phpfreaks.com/topic/163414-delete-data-from-mysql-databse-usign-php/#findComment-862240 Share on other sites More sharing options...
pkedpker Posted June 23, 2009 Share Posted June 23, 2009 <?php mysql_select_db("", $con); $num_ips= mysql_num_rows($ip); if($num_ips != 0) { } else { mysql_query("INSERT INTO UserINFO (IP, DATE, TIME) VALUES ('$ip', '$date', '$time')"); $query = "DELETE FROM table WHERE id = 500 ASC"; if (mysql_query($query)); } mysql_close($con); ?> I'd make the page_viws $ip into a primary so if the same one comes in.. it will be ignored and your views will be unique then you can add a DELETE FROM table WHERE date > DATE_SUB(CURDATE(), INTERVAL 1 DAY) to delete any Ip's greater then 1 day.. which will of course when counting your $num_ips your unique views.. will go down.. so you will have to save them as a number in another table to keep the views going up and up. Link to comment https://forums.phpfreaks.com/topic/163414-delete-data-from-mysql-databse-usign-php/#findComment-862255 Share on other sites More sharing options...
Porkie Posted June 23, 2009 Author Share Posted June 23, 2009 hey cheers for the reply $ip is the person ip address i then wanted to make another field in my database $page_views so basically if my ip already exists it will add 1 to the field $page_views. if you get me ? Link to comment https://forums.phpfreaks.com/topic/163414-delete-data-from-mysql-databse-usign-php/#findComment-862285 Share on other sites More sharing options...
pkedpker Posted June 23, 2009 Share Posted June 23, 2009 hey cheers for the reply $ip is the person ip address i then wanted to make another field in my database $page_views so basically if my ip already exists it will add 1 to the field $page_views. if you get me ? well how hard is that? it can easily be done in 1 SQL Query.. expirment with MySQL Query Browser Not PHP.. and you will need to do something like UPDATE `page` SET `pageviews`=`pageviews`+1 FROM page, iptables i WHERE i.ip = $myIP; probably SQL syntax error somewhere there but you could figure it out i gtg now IDK if there is any easier way.. probably is but i am not good at mysql Link to comment https://forums.phpfreaks.com/topic/163414-delete-data-from-mysql-databse-usign-php/#findComment-862302 Share on other sites More sharing options...
Porkie Posted June 23, 2009 Author Share Posted June 23, 2009 <?php $ip=$_SERVER['REMOTE_ADDR']; $date= date("Y/m/d"); $time=time("H/i/s"); $con = mysql_connect("localhost","",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("", $con); if($row = mysql_fetch_array($ip)) { mysql_query UPDATE `page` SET `pageviews`=`pageviews`+1 FROM page, [color=red]iptables i WHERE i.ip = $myIP;[/color] im confused here! } else mysql_query("INSERT INTO UserINFO (IP, DATE, TIME) VALUES ('$ip', '$date', '$time')"); $query = "DELETE FROM table WHERE id = 500 ASC"; if (mysql_query($query)); mysql_close($con); ?> i think this is wrong but i carnt seem to understand y? Link to comment https://forums.phpfreaks.com/topic/163414-delete-data-from-mysql-databse-usign-php/#findComment-862325 Share on other sites More sharing options...
pkedpker Posted June 24, 2009 Share Posted June 24, 2009 <?php $ip=$_SERVER['REMOTE_ADDR']; $date= date("Y/m/d"); $time=time("H/i/s"); $con = mysql_connect("localhost","",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("", $con); if($row = mysql_fetch_array($ip)) { mysql_query UPDATE `page` SET `pageviews`=`pageviews`+1 FROM page, [color=red]iptables i WHERE i.ip = $myIP;[/color] im confused here! } else mysql_query("INSERT INTO UserINFO (IP, DATE, TIME) VALUES ('$ip', '$date', '$time')"); $query = "DELETE FROM table WHERE id = 500 ASC"; if (mysql_query($query)); mysql_close($con); ?> i think this is wrong but i carnt seem to understand y? honestly man you ignored alot of my shit.. like the num_rows bit you just going with the flow you have to actually think man not just copy/paste stuff.. i dont know how your table layout is and stuff.. p.s your mysql_select_db() shouldn't have parameters of "",$con .. but like "HomepageDb" .. i know that "" works for you now but thats because you have 1 scheme (database) if you get more which I know you will in the future.. it will screw up and start going to the other one.. your mysql_query is a function it has to be called as function $result = mysql_query("query goes here", $con); then you use the $result to access the queries success or failure or number of rows it returned for example. Link to comment https://forums.phpfreaks.com/topic/163414-delete-data-from-mysql-databse-usign-php/#findComment-862355 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.