Check if Data in Table is same with POST

hi Guys,

just for a quick question summary:

I want to add new data in my table however, i want to check if my new data name has same name with stored data name in table, if not same name we can proceed to adding new data/post.

in my database table:

=====================
ID | title | remarks
01 | orange | fruit
02 | mango | fruit

addfile.php

$newtitle = mysql_real_escape_string($_POST['title']);
$newtitle = htmlentities($newtitle);


$sqldbtitle = "SELECT * FROM tblmainfile WHERE strcmp('$newtitle','title') = 0 LIMIT 1;
$getdbtitle = mysqli_query($con,$sqldbtitle);

newfile.php;

<form>
<tr>
<td>Title:</td>
<td><input type="text" name="title" value="<?php echo $newtitle; ?>"></td>
</tr>
</form>

goal:

if i will add orange, it will notify me that there is already orange in table.

questions:

  1. will strcmp be faster or i will just use mysql query?

thanks a lot!

If the title column must contain unique values, put a unique index on that column. That way you can do the insert, and if the value already exists you will get a duplicate key error.

hi, each title has its own unique index number, may i know how can i do that? thanks

Hello Friend ! You can proceed with using mysql query. strcmp is just used to compare two strings where as mysql query is used to find out the data directly in the database

@dreamdezigns

this is not working


if (isset($submit)) {
    $date = date("Ymd");
    $newtitle =  $_POST['title'];
    $querytitle = mysql_real_escape_string($_POST['title']);
    $queryalbum = mysql_real_escape_string($_POST['album']);
 
    // can i replace $artist?
    $checktitle = mysql_query("SELECT * from table WHERE artist = '$artist' ");
	
    if (!$checkTitle) {
        die('Query Failed');
    }

    $result = mysql_fetch_array($checktitle );

    if ($result['column_title'] == $newtitle )  {
        echo "THE SAME";
    } 
    print_r($result);
}

hi got it,

Function: to see and find if data exist in table before Insert
Table structure:
id - auto increment
artist - specific id number
title - unique title

if (isset($submit)) {
    $newtitle =  $_POST['title'];
    $querytitle = mysql_real_escape_string($_POST['title']);
    $queryalbum = mysql_real_escape_string($_POST['album']);
     // col_title is name of column title in table
     // indexid is the id of the table
    $sqldbtitle = "SELECT * from tblmainfile WHERE col_title = '$newtitle' ";
     $checktitle = mysqli_query($con, $sqldbtitle);
    // This is assuming  query only returns one result
     $resultcheck = mysqli_fetch_array($checktitle);
     // now check the value for 'title'
     if ($resultcheck['col_title'] == $newtitle) {
     echo "NEW TITLE IS SAME WITH EXISTING DATA";
     } else {
          echo "NOT THE SAME TITLE";
     }
}

thanks cheers!