Copy link to clipboard
Copied
Hey everyone,
I'm a bit of a newbie at PHP and am trying to create my first "dynamic" site. I have a registration table with fields idNo (primary key & auto-increment), username, password, email. I also have another table called profile with fields idNo, location, musicalInfluences, aboutMe etc. All of these fields except idNo have the default value of "update." What I want to be able to do is add a second insert statement to the user registration behaviour so that I can have the idNo number added to the profile tables as well as the registration table. That way the registration and profile are linked by idNo and I can create a record-set do automatically populate the profile table on my profile page when the user logs in. All of the different profile traits would say "update" and I can then use an update server behaviour for the user to update there profile.
I might be approaching this the wrong way so I'm open to suggestions on how to create a user login and linked profile page. One thing I could not work out was how, when the user clicks login with the correct credentials, I was supposed to separate a first time user, directed to the create profile page, from a user who has already created a profile page and just wants to see it. The only way I could think of was by using the default method explained before and then having an update page. Sorry if that was a bit of a waffle. Here is my code. Any help would be much appreciated!!
This is the script that I have included on my registration page. I just extracted the php and used an include to keep the html page tidy.
<?php require_once('Connections/soundstorm.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;
}
}
// *** Redirect if username exists
$MM_flag="MM_insert";
if (isset($_POST[$MM_flag])) {
$MM_dupKeyRedirect="usernameExists.php";
$loginUsername = $_POST['username'];
$LoginRS__query = sprintf("SELECT username FROM registration WHERE username=%s", GetSQLValueString($loginUsername, "text"));
mysql_select_db($database_soundstorm, $soundstorm);
$LoginRS=mysql_query($LoginRS__query, $soundstorm) or die(mysql_error());
$loginFoundUser = mysql_num_rows($LoginRS);
//if there is a row in the database, the username was found - can not add the requested username
if($loginFoundUser){
$MM_qsChar = "?";
//append the username to the redirect page
if (substr_count($MM_dupKeyRedirect,"?") >=1) $MM_qsChar = "&";
$MM_dupKeyRedirect = $MM_dupKeyRedirect . $MM_qsChar ."requsername=".$loginUsername;
header ("Location: $MM_dupKeyRedirect");
exit;
}
}
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "registerForm")) {
$insertSQL = sprintf("INSERT INTO registration (username, password, email) VALUES (%s, %s, %s)",
GetSQLValueString($_POST['username'], "text"),
GetSQLValueString($_POST['password'], "text"),
GetSQLValueString($_POST['email'], "text"));
mysql_select_db($database_soundstorm, $soundstorm);
$Result1 = mysql_query($insertSQL, $soundstorm) or die(mysql_error());
$insertGoTo = "registerSuccess.html";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}
?>
Copy link to clipboard
Copied
you can use a trigger in mysql. this trigger is for new records in your registration table. you can use mysql's last_insert_id() to get the last record inserted. just search for mysql triggers to get more info. note that this is not php code, it is mysql.