I have created a random string generator. What happens is that the user would click on a button and by using letters A and B it will construct a 3 letter string like these for example:
AAA
AAB
ABA
ABB
BAA
BAB
BBA
BBB
Now what I want is my random string generation to link with the “SessionId” column which is in the “Session” Table of my database where these strings will be stored in. What I want is the button to generate a string which is not in the database. For example look below (SessionId in Session Table):
Session Id
AAA
AAB
As these two sessions (string AAA and AAB) are in the database the generator should not display these two strings at all when clicking on the button and generating the strings as they are already in the database. Later on I want the genrated string to be stored in the database but at the moment I just want my generator to generate strings that are already in the database.
Does anyone know how to link this to my “Session Id” field in the “Session” Table and generate strings which are not in the database?
My code is in jsfiddle so you can see how the button works and the code used, click here to see
Below is my php code I am using at moment. This php code will let me connect to database and all user to enter a courseId in the textbox, if courseId is in textbox then user can enter else it will display an alert. Below is code:
<?php
$username="xxxxxxxxxxx";
$password="xxxxxx";
$database="mobile_app";
mysql_connect('localhost',$username,$password);
@mysql_select_db($database) or die("Unable to select database");
foreach (array('courseid') as $varname) {
$courseid = (isset($_POST[$varname])) ? $_POST[$varname] : '';
}
?>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<p>Course ID: <input type="text" name="courseid" value="<?php echo $courseid; ?>" /><input type="submit" value="Submit" name="submit" /></p> <!-- Enter User Id here-->
</form>
<?php
if (isset($_POST['submit'])) {
$query = "
SELECT cm.CourseId, cm.ModuleId,
c.CourseName,
m.ModuleName
FROM Course c
INNER JOIN Course_Module cm ON c.CourseId = cm.CourseId
JOIN Module m ON cm.ModuleId = m.ModuleId
WHERE
(c.CourseId = '".mysql_real_escape_string($courseid)."')
ORDER BY c.CourseName, m.ModuleId
";
$num = mysql_num_rows($result = mysql_query($query));
mysql_close()
;
?>
updated php code below:
<?php
$username=xxx";
$password="xxx";
$database="mobile_app";
$is_there = true;
$con = mysql_connect('localhost',$username,$password);
@mysql_select_db($database, $con) or die("Unable to select database");
while( $is_there ){
$id = id_generator(); // your function to generate id;
$result = mysql_query( "SELECT SessionId FROM Session WHERE SessionId = '$id'" );
if( mysql_num_rows( $result ) == 0 ) $is_there = false;
}
foreach (array('courseid', 'sessionid') as $varname) {
$$varname = (isset($_POST[$varname])) ? $_POST[$varname] : '';
}
?>
<h1>CREATING A SESSION</h1>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<p>Course ID: <input type="text" name="courseid" value="<?php echo $courseid; ?>" /><input type="submit" value="Submit" name="submit" /></p> <!-- Enter User Id here-->
</form>
<?php
if (isset($_POST['submit'])) {
$query = "
SELECT cm.CourseId, cm.ModuleId,
c.CourseName,
m.ModuleName
FROM Course c
INNER JOIN Course_Module cm ON c.CourseId = cm.CourseId
JOIN Module m ON cm.ModuleId = m.ModuleId
WHERE
(c.CourseId = '".mysql_real_escape_string($courseid)."')
ORDER BY c.CourseName, m.ModuleId
";
$num = mysql_num_rows($result = mysql_query($query));
mysql_close();
Your code on the server should be like:
This will stop when generated id is not found in the DB.