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

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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