kanoy83 Posted November 13, 2013 Share Posted November 13, 2013 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! Quote Link to comment https://forums.phpfreaks.com/topic/283876-check-if-data-in-table-is-same-with-post/ Share on other sites More sharing options...
requinix Posted November 13, 2013 Share Posted November 13, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/283876-check-if-data-in-table-is-same-with-post/#findComment-1458164 Share on other sites More sharing options...
kanoy83 Posted November 14, 2013 Author Share Posted November 14, 2013 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 ? thanks Quote Link to comment https://forums.phpfreaks.com/topic/283876-check-if-data-in-table-is-same-with-post/#findComment-1458237 Share on other sites More sharing options...
requinix Posted November 16, 2013 Share Posted November 16, 2013 "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. Quote Link to comment https://forums.phpfreaks.com/topic/283876-check-if-data-in-table-is-same-with-post/#findComment-1458492 Share on other sites More sharing options...
Solution kanoy83 Posted November 16, 2013 Author Solution Share Posted November 16, 2013 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! Quote Link to comment https://forums.phpfreaks.com/topic/283876-check-if-data-in-table-is-same-with-post/#findComment-1458602 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.