rusking Posted November 23, 2010 Share Posted November 23, 2010 Im trying to make a php page that searchs for whats typed in a field and if it doesnt exits then add. I am tring to use whats below but cant get it to work. Any thoughts <table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC"> <tr> <form name="form1" method="" action=""> <td> <table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF"> <tr> <td colspan="3"><strong>Member Search</strong></td> </tr> <tr> <td width="78">Username</td> <td width="6">:</td> <td width="294"><input name="myusername" type="text" id="myusername"></td> </tr> <tr> <td> </td> <td> </td> <td><input type="submit" name="Submit" value="Search"></td> </tr> </table> </td> </form> </tr> </table> $myusername = username <?php $con = mysql_connect("mysqlserver","user","pass"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("dbtest", $con); mysql_query("INSERT INTO partylist (Username, Key) VALUES ($myusername , 'keytest')"); mysql_close($con); ?> Link to comment https://forums.phpfreaks.com/topic/219615-search-for-add-if-not/ Share on other sites More sharing options...
Pikachu2000 Posted November 23, 2010 Share Posted November 23, 2010 $myusername is never assigned a value in the script. If you have error reporting enabled, you should be getting an "undefined variable" warning, unless register_globals = On (which it should not). Link to comment https://forums.phpfreaks.com/topic/219615-search-for-add-if-not/#findComment-1138597 Share on other sites More sharing options...
rusking Posted November 23, 2010 Author Share Posted November 23, 2010 Im using notepad to write it. Does $myusername = username not declair it? Link to comment https://forums.phpfreaks.com/topic/219615-search-for-add-if-not/#findComment-1138599 Share on other sites More sharing options...
Pikachu2000 Posted November 23, 2010 Share Posted November 23, 2010 No. That isn't within the <?php ?> tag structure, and if it was, it would be throwing an error. Inside the <?php tags, you'd need to put this: $username = 'username'; Link to comment https://forums.phpfreaks.com/topic/219615-search-for-add-if-not/#findComment-1138601 Share on other sites More sharing options...
rusking Posted November 23, 2010 Author Share Posted November 23, 2010 Thats seem to work for that, cant be sure as its not showing up in the table. Im checking the bottom of the code for porblems and testing it alone to see whats up Link to comment https://forums.phpfreaks.com/topic/219615-search-for-add-if-not/#findComment-1138605 Share on other sites More sharing options...
rusking Posted November 23, 2010 Author Share Posted November 23, 2010 Ok it runs, returns not erros but never enters the data <?php $con = mysql_connect("mysqlserveraddress","user","pass"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("dbtest", $con); mysql_query("INSERT INTO partylist (Username, Key) VALUES (frank , 'keytest')"); mysql_close($con); ?> Link to comment https://forums.phpfreaks.com/topic/219615-search-for-add-if-not/#findComment-1138608 Share on other sites More sharing options...
Pikachu2000 Posted November 23, 2010 Share Posted November 23, 2010 Frank needs to be in single quotes in the query string: VALUES ('frank', . . . Link to comment https://forums.phpfreaks.com/topic/219615-search-for-add-if-not/#findComment-1138611 Share on other sites More sharing options...
rusking Posted November 23, 2010 Author Share Posted November 23, 2010 using <?php $con = mysql_connect("mesqlserver","user","pass"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("dbtest", $con); mysql_query("INSERT INTO partylist (Username, Key) VALUES ('frank' , 'keytest')"); mysql_close($con); ?> Still nothing. I provide a screenshoot below of phpadmin to double check me Link to comment https://forums.phpfreaks.com/topic/219615-search-for-add-if-not/#findComment-1138619 Share on other sites More sharing options...
Pikachu2000 Posted November 23, 2010 Share Posted November 23, 2010 key is a reserved word in MySQL, so you either need to rename that field, or enclose `key` in backticks (which aren't the same as single quotes) when you use it. Link to comment https://forums.phpfreaks.com/topic/219615-search-for-add-if-not/#findComment-1138624 Share on other sites More sharing options...
rusking Posted November 23, 2010 Author Share Posted November 23, 2010 Sorry got a lil side tracked. I changed the "Key" to "ID" same result. Link to comment https://forums.phpfreaks.com/topic/219615-search-for-add-if-not/#findComment-1138744 Share on other sites More sharing options...
Pikachu2000 Posted November 23, 2010 Share Posted November 23, 2010 Copy your new query string into phpMyAdmin, and see if it executes there. Link to comment https://forums.phpfreaks.com/topic/219615-search-for-add-if-not/#findComment-1138755 Share on other sites More sharing options...
rusking Posted November 24, 2010 Author Share Posted November 24, 2010 welp i got the submit part...i used http://www.w3schools.com/PHP/php_mysql_insert.asp and made and insert.php with a forum.html. Still lost as to how to have it search to see if the record exist befor submiting. I think maybe a qury? not sure if thats right or not the html <html> <body> <form action="insert.php" method="post"> Recordname: <input type="text" name="firstname" /> Username: <input type="text" name="lastname" /> ID: <input type="text" name="age" /> <input type="submit" /> </form> </body> </html> And the insert.php <?php $con = mysql_connect("mysqlserver","user","pass"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("dbtest", $con); $sql="INSERT INTO partylist (RecordID, Username, ID) VALUES ('$_POST[firstname]','$_POST[lastname]','$_POST[age]')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "1 record added"; mysql_close($con) ?> Link to comment https://forums.phpfreaks.com/topic/219615-search-for-add-if-not/#findComment-1138769 Share on other sites More sharing options...
rusking Posted November 24, 2010 Author Share Posted November 24, 2010 like in my head i think query for the 'lastname" (ps need to rename that) If query = 'lastname" Then echo "Already exist" Else .....then the submit txt but im not sure how to format and word that all Link to comment https://forums.phpfreaks.com/topic/219615-search-for-add-if-not/#findComment-1138771 Share on other sites More sharing options...
rusking Posted November 24, 2010 Author Share Posted November 24, 2010 i know this is prob way off but this is kinda in my mind how i think it would work <?php $con = mysql_connect("mesqlserver","user","pass"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("dbtest", $con); $query = "SELECT username, id, message FROM partylist"; if (0) //lol idk, i used 0 becuase i know it means false {$sql="INSERT INTO partylist (RecordID, Username, ID) VALUES ('$_POST[firstname]','$_POST[username]','$_POST[id]')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "Welcome New User"; Else echo "User already exist"} mysql_close($con) ?> Link to comment https://forums.phpfreaks.com/topic/219615-search-for-add-if-not/#findComment-1138772 Share on other sites More sharing options...
rusking Posted November 24, 2010 Author Share Posted November 24, 2010 I got this far but cant figure out why dreamweaver (i switched to that) says the else statment has errors <?php $con = mysql_connect("sqladdress","user","pass"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("dbtest", $con); $query = "SELECT username, id, message FROM partylist"; if ($query =='username') echo "User already exist" else $sql="INSERT INTO partylist (RecordID, Username, ID) VALUES ('$_POST[firstname]','$_POST[username]','$_POST[id]')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "Welcome New User"; mysql_close($con) ?> Link to comment https://forums.phpfreaks.com/topic/219615-search-for-add-if-not/#findComment-1138779 Share on other sites More sharing options...
Pikachu2000 Posted November 24, 2010 Share Posted November 24, 2010 Missing a line terminating semicolon on the previous line. Also, even though it isn't technically necessary, it's a good idea to enclose the statements in curly braces. It helps readability immensely. Link to comment https://forums.phpfreaks.com/topic/219615-search-for-add-if-not/#findComment-1138783 Share on other sites More sharing options...
rusking Posted November 24, 2010 Author Share Posted November 24, 2010 Missing a line terminating semicolon on the previous line. Also, even though it isn't technically necessary, it's a good idea to enclose the statements in curly braces. It helps readability immensely. OOo wow way over my head. Can you show me or at leat point me to the direction i need to goto to learn? Link to comment https://forums.phpfreaks.com/topic/219615-search-for-add-if-not/#findComment-1138796 Share on other sites More sharing options...
rusking Posted November 24, 2010 Author Share Posted November 24, 2010 oo on the echo "User already exist"? shoulde read echo "User already exist"; Link to comment https://forums.phpfreaks.com/topic/219615-search-for-add-if-not/#findComment-1138799 Share on other sites More sharing options...
rusking Posted November 24, 2010 Author Share Posted November 24, 2010 hmm i must be off somewhere because now it says welcome user and puts nothing in the table. Even if the user exist i still get a Welcome new user . Well it does put something in the table...blankness. Just a blank entry <?php $con = mysql_connect("yesmyserver","user","pass"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("dbtest", $con); $query = "SELECT username, id, message FROM partylist"; if ($query =='username') echo "User already exist"; else { $sql="INSERT INTO partylist (RecordID, Username, ID) VALUES ('$_POST[firstname]','$_POST[username]','$_POST[id]')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "Welcome New User"; } mysql_close($con) ?> Link to comment https://forums.phpfreaks.com/topic/219615-search-for-add-if-not/#findComment-1138800 Share on other sites More sharing options...
rusking Posted November 24, 2010 Author Share Posted November 24, 2010 Correction, my html was off where i renamed the vaules. It post now. The only catch is it it always post and always says welcome new user. I think im useing the incrorrect vaule at the if ($query =='[username]') part but not sure what the correct would be. <?php $con = mysql_connect("wserver.com","user","pass"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("dbtest", $con); $query = "SELECT username, id, message FROM partylist"; if ($query =='[username]') echo "User already exist"; else { $sql="INSERT INTO partylist (RecordID, Username, ID) VALUES ('$_POST[firstname]','$_POST[username]','$_POST[id]')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "Welcome New User"; } mysql_close($con) ?> Link to comment https://forums.phpfreaks.com/topic/219615-search-for-add-if-not/#findComment-1138807 Share on other sites More sharing options...
Pikachu2000 Posted November 25, 2010 Share Posted November 25, 2010 OK, let's start with this. It doesn't do anything at all. The query is never executed with mysql_query(), and all that block of code does is check to see if the query string equals the string [username], which it obviously does not. $query = "SELECT username, id, message FROM partylist"; if ($query == '[username]') { echo "User already exist"; Also, I'm moving this thread to PHP Coding help, since this is ultimately a PHP issue. Link to comment https://forums.phpfreaks.com/topic/219615-search-for-add-if-not/#findComment-1139286 Share on other sites More sharing options...
rusking Posted November 26, 2010 Author Share Posted November 26, 2010 Hi all, thread go moved, can anyone help with this? Link to comment https://forums.phpfreaks.com/topic/219615-search-for-add-if-not/#findComment-1140043 Share on other sites More sharing options...
Pikachu2000 Posted November 26, 2010 Share Posted November 26, 2010 Did you fix the problems I pointed out in my previous post, or are you having trouble with that? Link to comment https://forums.phpfreaks.com/topic/219615-search-for-add-if-not/#findComment-1140062 Share on other sites More sharing options...
rusking Posted November 26, 2010 Author Share Posted November 26, 2010 oo hey i didnt kow you where in this section too I don't fully understand how to correct it. I know i want it to search for the username to see if it exist but that was the best i could come up with and am not sure how to make it "do" something. I have made the username via the html then tried the same username agin. Says welcome still and then i check the database and the username is already there. Link to comment https://forums.phpfreaks.com/topic/219615-search-for-add-if-not/#findComment-1140065 Share on other sites More sharing options...
Pikachu2000 Posted November 26, 2010 Share Posted November 26, 2010 Let's start here: $query = "SELECT username, id, message FROM partylist"; if ($query == '[username]') { echo "User already exist"; } Right now, what that chunk of code says is: "If the string: "SELECT username, id, message FROM partylist" is literally the same string as: "[username]", echo to the browser the string: "User already exist. Do you have an understanding of how arrays and forms work, and what happens when a form is submitted? If not, we should start there, or you're just going to end up more confused. Link to comment https://forums.phpfreaks.com/topic/219615-search-for-add-if-not/#findComment-1140067 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.