Jump to content

[SOLVED] Inserting Info


cheechm

Recommended Posts

Hello,

I am sucessfully updating 2 databases with info, however if I use insert it enters the data over and over again, and if I update it every hour then there will be a problem.

 

so this it the code:

 

<?php
// Lets open connections to MySQL database
// -------------------------------------

$con1 = mysql_connect(***************);
$db = mysql_select_db(***, ($con1))
or die ("Couldn't select database.");

$con2 = mysql_connect(*****************);
$db = mysql_select_db(***, ($con2))
or die ("Couldn't select database.");

// lets get the data from the forums
// -------------------------------------

$sql = "SELECT * FROM ***";
$query = mysql_query($sql, $con1);

if (mysql_num_rows($query) == 0) {

echo "Nothing to copy. Exiting now.";

exit;

}

// lets insert it into the auction site
// -------------------------------------

while($result=mysql_fetch_assoc($query))
{
$sql="INSERT INTO ***(username,password,email,regdate,birthdate)
VALUES('".$result['username']."','".$result['password']."','".$result['email']."','".$result['joindate']."','".$result['birthday_search']."')";
mysql_query($sql,$con2);
}

mysql_close($con1);
mysql_close($con2);

?>

So how could I set it to only insert into the database if it doesn't exist.

ie
[code=php:0]
<?
if data exists in table
{
Update the current data
}
else data doesn't exist intable
{
Insert the new data
}
?>

 

How would I be able to do that?

Thanks in advance

Link to comment
https://forums.phpfreaks.com/topic/70955-solved-inserting-info/
Share on other sites

first try to select the data. if no rows are returned, it doesn't exist, so insert it:

 

$sql = "SELECT FROM some_table WHERE item1 = '2' and itme2 = 'whatever'";
$result = mysql_query($sql) or die(mysql_error());
if (mysql_numrows($result) == 0) { // The record doesn't exist, so insert it
    $isql = "INSERT INTO some_table (item1, item2) VALUES ('2', 'whatever')";
    mysql_query($isql) or die(mysql_error());
}

Link to comment
https://forums.phpfreaks.com/topic/70955-solved-inserting-info/#findComment-356724
Share on other sites

you must use the record id, updating the record with that id:

 

$sql = "SELECT id FROM some_table WHERE item1 = '2' and itme2 = 'whatever'";
$result = mysql_query($sql) or die(mysql_error());
if (mysql_numrows($result) == 0) { // The record doesn't exist, so insert it
    $isql = "INSERT INTO some_table (item1, item2) VALUES ('2', 'whatever')";
    mysql_query($isql) or die(mysql_error());
} else { // The record already exists, so update it.
    list($id) = mysql_fetch_row($result);
    $usql = "UPDATE some_table SET item1 = '2', item2 = 'whatever' WHERE id = '$id'";
    mysql_query($usql) or die(mysql_error());
}

 

Link to comment
https://forums.phpfreaks.com/topic/70955-solved-inserting-info/#findComment-356742
Share on other sites

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.