Jump to content

check if Data in Table is same with POST


kanoy83
Go to solution Solved by kanoy83,

Recommended Posts

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!

Link to comment
Share on other sites

Use a query, not PHP and definitely not some hybrid of SQL and PHP (like you have there).

 

- Use a SELECT to check in general whether the "title" has been used.

- When you're ready to save the new thing, do an INSERT IGNORE and check the affected_rows to see if it was inserted. Make sure the title has a UNIQUE index on it. If it inserted then great, otherwise do whatever your "title already exists" action is.

Link to comment
Share on other sites

Use a query, not PHP and definitely not some hybrid of SQL and PHP (like you have there).

 

- Use a SELECT to check in general whether the "title" has been used.

- When you're ready to save the new thing, do an INSERT IGNORE and check the affected_rows to see if it was inserted. Make sure the title has a UNIQUE index on it. If it inserted then great, otherwise do whatever your "title already exists" action is.

 

can you show me some sample queries ? :D thanks

Link to comment
Share on other sites

"SELECT 1 FROM tblmainfile WHERE title = '" . mysql_real_escape_string($_POST['title']) . "'"
If that returns anything then the title is already taken.

"INSERT IGNORE INTO tblmainfile (the rest of your insert query)"
If the affected_rows count is == 1 then it inserted as a new title, otherwise there was a problem that probably meant the title already exists (as in someone made a new one in between the time it took you to do the first SELECT and to do the INSERT). Don't forget the unique index on the title.

 

Side comment: Are you using mysqli? You included mysql_real_escape_string() in your code but mysqli_query() as well. If you're using the mysql_* functions then stop and use mysqli or PDO instead; if you're using mysqli then don't use mysql_real_escape_string() and use prepared statements instead.

Link to comment
Share on other sites

  • Solution
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!

Link to comment
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.