check if a value exists in my database table

Issue

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 {
?>

Solution

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.

Answered By – Sahan Perera

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published