corrupshun Posted December 4, 2009 Share Posted December 4, 2009 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(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/183920-check-database-if-it-already-exists/ Share on other sites More sharing options...
eRott Posted December 4, 2009 Share Posted December 4, 2009 <?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.'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/183920-check-database-if-it-already-exists/#findComment-970994 Share on other sites More sharing options...
mikesta707 Posted December 4, 2009 Share Posted December 4, 2009 in addition to what eRott posted, you can check if the file exists by doing something like the following if (file_exists($path)){ //file exists } else { //file does not exist } Quote Link to comment https://forums.phpfreaks.com/topic/183920-check-database-if-it-already-exists/#findComment-970997 Share on other sites More sharing options...
corrupshun Posted December 4, 2009 Author Share Posted December 4, 2009 thank you both VERY much! This is exactly what I was looking for! who knew it would be as easy as file_exists() Quote Link to comment https://forums.phpfreaks.com/topic/183920-check-database-if-it-already-exists/#findComment-971153 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.