Jump to content

delete data from mysql databse usign php


Porkie

Recommended Posts

<?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';
}

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...

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.

<?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.

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

<?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?

 

<?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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.