Yassine Zouaoui
Yassine Zouaoui

Reputation: 3

check if a value exists in my database table

I want to check if a URL exists in my MySQL database table for example if Url=exist, message=url already exist

<?php
if(isset($_POST['Submit'])){
    $dbhost = 'localhost';
    $dbuser = '####';
    $dbpass = '#######';
    $conn = mysql_connect($dbhost, $dbuser, $dbpass);
    $Title = $_POST['Title'];
    $Url = $_POST['Url'];
    $email_data = $_POST['email_data'];
    $type_data = $_POST['type_data'];
    $sql = "INSERT INTO table ". "(Title,Url,email_data,type_data)"."VALUES('$Title','$Url','$email_data','$type_data')";
    mysql_select_db('dbname');
    $retval = mysql_query( $sql, $conn );
        if(! $retval ){
            die('Could not enter data: ' . mysql_error());
        }
    echo "<script type='text/javascript'>alert('submitted successfully!')</script>";
    mysql_close($conn);
} else {
?>

Upvotes: 0

Views: 3906

Answers (3)

Karthik Keyan
Karthik Keyan

Reputation: 426

$query = mysql_query('select url from table-name where url=$_post['url']');
if(mysql_fetch_rows($query) != 0){
echo "URL allready Exists";
}else{ 
Insert Query 
}

Hi, Try this code.

Upvotes: 1

Patrik Fr&#246;hler
Patrik Fr&#246;hler

Reputation: 1291

Its a bit hard know what you are doing by just looking at the code but if you wondering how to check if something is in the database you could do like this:

PHP:

$sql = mysql_query("SELECT * FROM dbname WHERE Url='$Url'");
if(mysql_num_rows($sql) > 0){
    echo "alreday exist";
}

Upvotes: 0

Sahan Perera
Sahan Perera

Reputation: 194

You can do like this

$sql = "SELECT Url FROM 'your_table_name' WHERE Url = $_POST['Url']";

Run this sql and if it is return true you can say its exist.

Upvotes: 1

Related Questions