Jump to content

Recommended Posts

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

?>

 

 

Link to comment
https://forums.phpfreaks.com/topic/239223-die-problem/
Share on other sites

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";
	}
}

Link to comment
https://forums.phpfreaks.com/topic/239223-die-problem/#findComment-1229017
Share on other sites

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  :-\

Link to comment
https://forums.phpfreaks.com/topic/239223-die-problem/#findComment-1229018
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.