My Latest Script

Syneticus 0 Tallied Votes 132 Views Share

Alrighty! My goal here is to add the specified information into a MySQL database, but then also include a script that goes back and checks to make sure that the entrie given does not already exist. If you have any corrections, or any ideas on how to do this, I'd really appreciate it. I posted something simliar in the PHP section, but figured it might be more appropriate to put my code in the snippets.

// Checks if the domain and ip is in the list 

$quer = mysql_query('SELECT * FROM domains WHERE name="$domain"', $con) or die('No domain in the database'); 
$quer2 = mysql_query('SELECT * FROM vhostips WHERE ip="$ip"', $con) or die('No IP in the database'); 
$vhost2 = "$vhost" . "." . "$domain"; 
$query = "SELECT * FROM vhosts"; 
$result = mysql_query($query) or die(mysql_error()); 
$#### = mysql_fetch_array($result); 

print('$####["vhost"][0]'); 
// Eventually we'll have to see if the IP is attached to the server 
if ($quer && $quer2) { 
$insert = mysql_query("INSERT INTO 
vhosts 
(id, vhost, ip) 
VALUES 
('$id', '$vhost2', '$ip')"); 
if (!$insert) { 
die('Error in MySQL query: ' . mysql_error()); 
} else { 
$blah = "blah"; 
} 
} 
// Print the success page 

if ($blah) { 
print('V-Host ' . $vhost2 . ' added.  IP: ' . $ip . "."); 
} 

?>

Dani AI

Generated

wanted a safe check-then-insert flow; already hinted at a missing DB connection. The main improvements are: validate inputs, stop using the old mysql_* API, avoid concatenating user data into SQL, and prevent race conditions either with a UNIQUE constraint or by handling duplicate-key errors on INSERT.

A pragmatic pattern is: validate domain and IP, create a proper DB connection (PDO or mysqli), add a UNIQUE index on the vhost+ip pair, then attempt a single INSERT and catch duplicate-key errors. That avoids a separate SELECT followed by INSERT which is vulnerable to races. Also prefer an AUTO_INCREMENT primary key and store IPs in a consistent format (use inet_pton for IPv4/IPv6).

Example (PDO, prepared statements, minimal validation):

<?php
$pdo = new PDO('mysql:host=127.0.0.1;dbname=sites;charset=utf8mb4', 'dbuser', 'dbpass', [
  PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
]);

if (!filter_var($ip, FILTER_VALIDATE_IP)) {
  throw new InvalidArgumentException('Invalid IP');
}

$fullVhost = $vhost . '.' . $domain;

try {
  $stmt = $pdo->prepare('INSERT INTO vhosts (vhost, ip) VALUES (?, ?)');
  $stmt->execute([$fullVhost, $ip]);
  echo 'V-Host ' . $fullVhost . ' added. IP: ' . $ip;
} catch (PDOException $e) {
  if (isset($e->errorInfo[1]) && $e->errorInfo[1] == 1062) {
    echo 'Entry already exists: ' . $fullVhost;
  } else {
    throw $e;
  }
}
?>

Schema hints: use AUTO_INCREMENT id and a UNIQUE index to enforce uniqueness at the DB level:

ALTER TABLE vhosts
  ADD COLUMN id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  ADD UNIQUE KEY ux_vhost_ip (vhost, ip);

For reference on APIs and validation see the PHP PDO manual and filter_var docs: PHP PDO manual and filter_var.

Ginetta 0 Newbie Poster

express $con variable (in your sql query)
I don't see it anywhere.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.