homepage Welcome to WebmasterWorld Guest from 54.167.173.250
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
check mysql record if already exist
hassanzobeen




msg:4409609
 1:34 pm on Jan 22, 2012 (gmt 0)

hello
I am facing a problem while creating a a code
I need to add 3 validations into this code
validations are s follow

1. check if a url address already exist into database then give an error i.e. URL address already exist in database

2. check if title row of the database is blank(unable to detect by function it gives error i.e. We cannot add this url because we are unable to detect its title

3. check if url contains https:// protocol it give error i.e. You are not allowed to add https:// URLs



My php code is

$sql="INSERT INTO $tbl_name(webname, urladdr)VALUES('$title', '$webname')";
$result=mysql_query($sql);

if($result){
echo "Successful";
echo "<BR>";
echo "<a href='index.php'>Back to main page</a>";
}

else {
echo "ERROR";
}



$tbl_name is weblist (which I already defined in function)

webname, urladdr
are rows in that table


$title is a website title detected by the function

$webname is the URL address of the website


Please solve my problem as soon as possible

 

Dijkgraaf




msg:4409682
 8:37 pm on Jan 22, 2012 (gmt 0)

hassanzobeen, you need to at least research possible ways of doing things yourself first and then try them out. We are not generally not here to program things for you, only to help you if you truly get stuck or can't solve a problem.
At that point you need to post what you have tried on how you are stuck or the error you are getting. You are posting too many threads where you aren't even explaining what you have tried to do, or what problem you have encountered.

rocknbil




msg:4409916
 4:36 pm on Jan 23, 2012 (gmt 0)

Put your thoughts into programming, like so. The only thing that's odd is if they are adding a URL, it won't have a title in the database yet, so I'm checking post.

// I need to add 3 validations into this code
$errors = check_data();
if ($errors) { echo "<p>Errors:</p><ul>$errors</ul>"; }
else {
// process form as usual
}

Then your function:

function check_data() {
// Set to null and return value at end of function
// this is what makes "if $errors" above work
$err = null;

//1. check if a url address already exist into
//database then give an error i.e. URL address already
//exist in database

$url = strtolower(preg_replace('/[^\/\:a-z\d]/i','',$_POST['url'])); // not complete but a start

$query = "select id from table where url='$url'";
// do the query
if ($id = mysql_fetchrow_array()) {
$err .= "<li>That URL already exists in the database.</li>";
}

//2. check if title row of the (?-->) database is blank(unable
//to detect by function it gives error i.e. We cannot
//add this url because we are unable to detect its title
// (You could just overwrite it if it exists, the
// database record doesn't exist yet)

$title = preg_replace('/[^a-z\d\s\-]/i','',$_POST['title']);
if (empty($title)) {
$err .= "<li>You must have a title for this URL.</li>";
}

//3. check if url contains https:// protocol it give
//error i.e. You are not allowed to add https:// URLs


if (preg_match('/^https\:\/\//i',$_POST['url'])) {
$err .= "<li>HTTPS URL's are not allowed.</li>";
}
return $err;

} // end of function

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved