bluecuracao Posted August 27, 2009 Share Posted August 27, 2009 Hello, I'm trying to create an insert record form that only actually inserts when the auth-code entered by the user is correct (=exists in a mysql table). Code is this: <?php require_once('Connections/connectDB.php'); ?> <?php if (!function_exists("GetSQLValueString")) { function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { if (PHP_VERSION < 6) { $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; } $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue); switch ($theType) { case "text": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "long": case "int": $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break; case "double": $theValue = ($theValue != "") ? doubleval($theValue) : "NULL"; break; case "date": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; break; } return $theValue; } } mysql_select_db($database_connectDB, $connectDB); $query_RSauth = "SELECT Code FROM auth"; $RSauth = mysql_query($query_RSauth, $connectDB) or die(mysql_error()); $row_RSauth = mysql_fetch_assoc($RSauth); $totalRows_RSauth = mysql_num_rows($RSauth); $editFormAction = $_SERVER['PHP_SELF']; if (isset($_SERVER['QUERY_STRING'])) { $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']); } if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "insert")) { if ($RSauth == $_POST['auth']) { $insertSQL = sprintf("INSERT INTO Benutzer (Anrede, Vorname, Nachname, Benutzername, Passwort, Firma, telefon, handy, email) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)", GetSQLValueString($_POST['Anrede'], "text"), GetSQLValueString($_POST['Vorname'], "text"), GetSQLValueString($_POST['Nachname'], "text"), GetSQLValueString($_POST['Benutzername'], "text"), GetSQLValueString($_POST['Passwort'], "text"), GetSQLValueString($_POST['Firma'], "text"), GetSQLValueString($_POST['telefon'], "text"), GetSQLValueString($_POST['handy'], "text"), GetSQLValueString($_POST['email'], "text")); mysql_select_db($database_connectDB, $connectDB); $Result1 = mysql_query($insertSQL, $connectDB) or die(mysql_error()); } } ?> I'm assuming the line "if ($RSauth == $_POST['auth'])" returns false, cause when i delete it, the insert works. Any ideas on this? Or another method to achive the same result (insert only when the auth-code is correct)? I really tried a lot of stuff and nothing seems to work, help would be greatly appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/172137-solved-insert-record-only-when-auth-code-is-right/ Share on other sites More sharing options...
TeNDoLLA Posted August 27, 2009 Share Posted August 27, 2009 To me your code looks a little bit messy. But in short this should do it <?php if (isset($_POST['auth_code'])) { $authCode = $_POST['auth_code']; $sql = "SELECT * FROM auth WHERE auth_code = $authCode"; $result = mysql_query($sql); if (mysql_num_rows($result)) { // Do the insert here. } else { // Code not found in db. } } Quote Link to comment https://forums.phpfreaks.com/topic/172137-solved-insert-record-only-when-auth-code-is-right/#findComment-907622 Share on other sites More sharing options...
Hybride Posted August 27, 2009 Share Posted August 27, 2009 Also, that line you mentioned: if ($RSauth == $_POST['auth']) there is no mention of $_POST['auth'] anywhere in the code. You were using an "auth" table, but that's not the same thing. Quote Link to comment https://forums.phpfreaks.com/topic/172137-solved-insert-record-only-when-auth-code-is-right/#findComment-907629 Share on other sites More sharing options...
bluecuracao Posted August 27, 2009 Author Share Posted August 27, 2009 To me your code looks a little bit messy. But in short this should do it Thank you very very much! It works! I just had to correct a tiny mistake (i think) before i got it to work: $sql = "SELECT * FROM auth WHERE auth_code = $authCode"; didn't work, but $sql = "SELECT * FROM auth WHERE auth_code = '$authCode'"; did. Quote Link to comment https://forums.phpfreaks.com/topic/172137-solved-insert-record-only-when-auth-code-is-right/#findComment-907689 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.