Jump to content

Check database if it already exists?


corrupshun

Recommended Posts

Alright, so I've been wanting to make a script soley for my personal use, to insert games into a database.

Is there a way that before I submit the title, side(title), and path while checking to see if any of it is already in the database? Also for the path I would like to check if it is actually there.

For example: a path would be the path to a flash game such as "swf/game.swf"

Now I would like to make the script to check if there is such a file and to check if it's already in the database..

Here's my code if needed.

(Right now it doesn't check anything and don't tell me it's vulnerable to SQL Injection, cuz i know that ;))

<?php
$con = mysql_connect("localhost","root","");
if(!$con) {
die('failed to connect to db' . '<br />');
}
else {
echo "CONNECTED" . "<br />";
}
mysql_select_db("Corrupshun", $con);
$query = "INSERT INTO Games (Title, Side, Path) VALUES ('$_POST[title]','$_POST[side]','$_POST[path]')";
if(mysql_query("$query",$con)) {
echo "Inserted $_POST[title], $_POST[side], and $_POST[path] successfully into database";
}
else {
echo "Did not enter." . mysql_error();
}
?>

Link to comment
https://forums.phpfreaks.com/topic/183920-check-database-if-it-already-exists/
Share on other sites

<?php

mysql_connect ("localhost", "username", "password") or die (mysql_error());
mysql_selectdb ("Corrupshun") or die (mysql_error());

$title = $_POST['title'];
$side = $_POST['side'];
$path = $_POST['path'];

$check = mysql_query("SELECT * FROM games WHERE title='$title' AND side='$side' AND path='$path'");
$numrows = mysql_num_rows($check);

if ($numrows >= 1)
{
     echo 'Game already exists.';
} else {
     mysql_query("INSERT INTO games (title, side, path) VALUES ('$title','$side','$path')");
     echo 'Successfully inserted '.$title.' into the database.';
}

?>

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.