ebchost Posted June 13, 2011 Share Posted June 13, 2011 How to stop code for add new record in this situation if i have "test" categories alredy in table "categoryes" wen i want to ad new category with name "test". file: add_category.php (note: this file include_once("init.php"); for connection with database, who include_once "blog.php"; with function add_category and category_exist, <?php if ($submit) { if($name) { if(strlen($name)>24) { $error = "Naziv kateogrije ne moze biti duzi od 24 karaktera"; } else if (category_exist($name)) { $error = "Kategorija postoji"; } else $upisano = "kategorija: $name je uspesno upisana u bazu"; add_category($name); } } ?> <html> <body> <h1>Add Category</h1> <hr> <?php if($error) { echo "<p>$error</p>\n"; } else echo "</br> $upisano </br>\n"; ?> ... rest of code File: blog.php <?php function add_category($name) { $name = mysql_real_escape_string($name); mysql_query("INSERT INTO categories SET name='{$name}'")or die(mysql_error()); } function category_exist($name) { $name = mysql_real_escape_string($name); $query = mysql_query("SELECT COUNT(1) FROM categories WHERE name = '{$name}'") or die(mysql_error()); return (mysql_result($query,0)=='0')? false: true; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/239223-die-problem/ Share on other sites More sharing options...
ebchost Posted June 13, 2011 Author Share Posted June 13, 2011 update: stop with out die("alredy exists"); i want to show message "alredy exists" with form, with out adding new record. thanks Quote Link to comment https://forums.phpfreaks.com/topic/239223-die-problem/#findComment-1229014 Share on other sites More sharing options...
monkeytooth Posted June 13, 2011 Share Posted June 13, 2011 Your going to have to query your DB first to see if there is a match, if there is a match then no insert should be made. Which your on the right track kinda between the 2 functions you show as example. You should combine them in a manner of speaking. Kinda like not guaranteeing this will work as i am just using your code to re-sample the idea with function toAddorNottoAdd($name) { function category_exist($name) $name = mysql_real_escape_string($name); $query = mysql_query("SELECT COUNT(1) FROM categories WHERE name = '{$name}'") or die(mysql_error()); $wasItFound = (mysql_result($query,0)=='0')? false: true; if($wasItFound === false) { $name = mysql_real_escape_string($name); mysql_query("INSERT INTO categories SET name='{$name}'")or die(mysql_error()); echo $name . " inserted into DB"; } else { echo $name . " found in DB"; } } Quote Link to comment https://forums.phpfreaks.com/topic/239223-die-problem/#findComment-1229017 Share on other sites More sharing options...
monkeytooth Posted June 13, 2011 Share Posted June 13, 2011 function toAddorNottoAdd($name) { $name = mysql_real_escape_string($name); $query = mysql_query("SELECT COUNT(1) FROM categories WHERE name = '{$name}'") or die(mysql_error()); $wasItFound = (mysql_result($query,0)=='0')? false: true; if($wasItFound === false) { $name = mysql_real_escape_string($name); mysql_query("INSERT INTO categories SET name='{$name}'")or die(mysql_error()); echo $name . " inserted into DB"; } else { echo $name . " found in DB"; } } quick correction #1 :-\ Quote Link to comment https://forums.phpfreaks.com/topic/239223-die-problem/#findComment-1229018 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.