Jump to content

[SOLVED] insert record only when auth-code is right


bluecuracao

Recommended Posts

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.

Link to comment
Share on other sites

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.
   }
}

Link to comment
Share on other sites

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.

 

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.