Jump to content

Recommended Posts

Problem:

My code is not functioning at all on my form. I would like to see why?

 

Scripting Info:

  • Using PHP and MySQL
  • Using offline Server for testing(XAMPP)
  • Using default Server name and login.(localhost,root,[NO PASSWORD])
  • Database name is reh_temp
  • Table within database is named users
  • 1 Value in table "users" called users

 

Goal:

My form has 3 inputs. 2-Text and 1-Submit.

The 1st text input has the name of id.

And the 2nd input has the name of table.

In the PHP/MySQL part the values are inputed as follows:

 

The one with name of "id" gets put into the table users under the users value.

The one with name of "table" creates a new table named after what the user inputs

 

The rest of the script is just Character Length checks, Exsistance of value checks, Match check, ect.

 

Here's the code:

 

<?php

            $connect1 = mysql_connect("localhost","root","");
            
            if (!$connect)
               die('Could not connect to the database: ' . mysql_error());

          $id = $_POST['id'];
          $table = $POST['table'];

          //Check for existance
          if (!$id)
               die('You need to type in a session name.');
               else
               {
                if (!$table)
                    die('You need to re-enter you session name.');
               }

          //Check for existance in Database
          mysql_select_db("reh_temp", $connect1);
          
          $q_id = mysql_query("SELECT * FROM users WHERE users='$id'");
          if (mysql_num_rows($q_id)==0)
               {
                //Check Character Length
                if (strlen($id)<4||strlen($table)<4)
                    die('Please enter a session name that is more than 4 characters.');
                    else
                    {
                     //Check for Match
                     if ($id==$table)
                         {
                          //Insert value into users table
                          mysql_select_db("reh_temp", $connect1);
                          $q_id_reg = mysql_query("INSERT INTO users VALUES ('$id')");
                          
                          mysql_select_db("reh_temp", $connect1);
                          $session_table = "CREATE TABLE $table
                          (
                          password varchar(10)
                          )";
                          die ('Thanks!');
                         }
                         else
                         die ('Your session names do not match!');
                    }
               }
               else
               die ('The session name you choose is already in use. Please choose a different one.');
               {

          ?>

     <head>
          <title>ReH-0.1--Create a Session</title>
     </head>

     <body bgcolor="#575757">
          
          <center>
               <font color="#ffffff">Before encrypting your password, a session must be started. We need you to enter a personal session name that you will remember so that you password is protected by this session.</font><br><br>
               <form action="ReH-0.1.php" method="POST">
                    <input type="text" maxlength="10" size="10" name="id"><br>
                    <font color="#ffffff">Please choose a session name.</font><br><br>
                    <input type="text" maxlength="10" size="10" name="table"><br>
                    <font color="#ffffff">Please re-enter you session name.</font><br><br>
                    <input type="submit" value="Create Session">
               </form>
          </center>
          
     </body>
     
               <?php
               }
               echo "I'm Working!";
               ?>

 

Any help and suggestions would help. Thanks!

Link to comment
https://forums.phpfreaks.com/topic/224759-code-not-functioning/
Share on other sites

I mean it's not acting at all. I type in incorrect stuff that should not be allowed like under 4 characters. It does not reply with the "die('Your session name must more than 4 characters');" and just not storing the data. I've done all the checks and nothing is working. I've done PHP and MySQL before and this has never happened.

 

I don't know if it's the way I coded it or am I putting the PHP coding on the wrong page. The action is "session_start" and method is "POST". Any reasons or help to get it working.

Still isn't running... :( I did both. With the error code and the $connect1 changed to $connect. Any other suggestions or fixes. And by the way what happens when I hit the submit button is absolutely nothing. It just removes the text from the text fields and is on the same page. It used to move to the "session_start.php" page. But now it's not. Also I'm not looking for session_start(). I know what that is I'm creating a "so-called" session. It's for a password converter, the session just creates a user and from that user creates a table based off the session name given it doesn't break the checks for existence. That's literally all I need this portion of the code to do is to add value of 1st text field to users under user table and create a new table based off what they name it. The checks are to make sure the name doesn't already exist, match each other, and makes sure that the name is between 4-10 characters. If someone can make this code better fine, but I was for sure this should work.

Okay I see why my "action" value wasn't taking me to the other page. I accidentally had the value as the same page. But when it goes to next page it is just a blank page with no "echo" for the error. Nor did it store the data.

 

Sorry for the double-post. I just now found the Edit Message button.

did a little work to clean your code up. not sure why you want to create another table with one thing in it, why not just add a column to the users table.

 

 

<html>
<head>
	<title>ReH-0.1--Create a Session</title>
</head>
<body bgcolor="#575757">
	<center>
<?php
try {
	// Connect and select
	$connect = mysql_connect("localhost","root","");
	if(!$connect) {
		throw new Exception( mysql_error() );
	}
	mysql_select_db( "reh_temp", $connect );

	// verify post-data
	if( isset( $_POST['session'] ) && !empty( $_POST['session'] ) ) {
		if( strlen( $_POST['session'] ) < 4 ) {
			throw new Exception('Session must be longer than four characters');
		}
		$session = mysql_real_escape_string($_POST['session']);
	} else {
		throw new Exception( 'Session Required' );
	}
	if(isset($_POST['session_conf']) && !empty($_POST['session_conf'])) {
		if( strlen( $_POST['session_conf'] ) < 4 ) {
			throw new Exception('Session confirmation must be longer than four characters');
		}
		$session_conf = mysql_real_escape_string($_POST['session_conf']);
	} else {
		throw new Exception( 'Session Confirmation Required' );
	}

	// Query post data
	$query = mysql_query("SELECT * FROM users WHERE users='${session}'");
	if( mysql_num_rows( $query ) != 0 ) {
		//Check for Match
	if ( $session != $session_conf ) { 
		throw new Exception('Session does not match confirmation');
	}
	//Insert value into users table
	if( !@mysql_query( "INSERT INTO users VALUES ( '${session}' )" ) ) {
		throw new Exception( mysql_error() );
	}
	// if this executes the script is complete
	print '<font color="#0F0">Success</font>';
} catch(Exception $objException) {
	print '<font color="#f00">' . $obj->Exception->getMessage() . '</font><br/>';
}
?>
		<font color="#fff">Before encrypting your password, a session must be started. We need you to enter a personal session name that you will remember so that you password is protected by this session.</font><br />
		<form action="ReH-0.1.php" method="POST">
			<label for="session" style="color: #fff;">Session Name:
			<input type="text" maxlength="10" size="10" name="session"></label><br />
			<label for="session_conf" style="color: #fff;">Session Name Confirmation:
			<input type="text" maxlength="10" size="10" name="session_conf"></label><br />
			<input type="submit" value="Create Session">
		</form>
	</center>
</body>
</html>

Oh I'm sorry. I totally forgot about making sure my files are in the right spot. I feel so stupid now. It's just been a while since I've used XAMPP and done PHP. It sound like it's been mostly my fault this whole time.

 

Just to clarify.  It's not a matter of files being in the right spot.  It's about having the web-server parsing your files rather than accessing them from your file system in the browser.

Okay I've finally got my scripts to work, but on flaw I'd like to get solved. My echo is showing before from is filled out. Please correct me if my coding is out of format. The echo that is always up when I reload page is the "You must enter a Session Name" one.

 

Code:

<html>
   <head>
      <title>ReH-0.1--Create a Session</title>
   </head>
   <body bgcolor="575757">
     <center>
<?php
   
   $error = "Could not connect to the database";
   mysql_connect('localhost','root','') or die ($error);
   mysql_select_db('temp') or die ($error);
   
   //Set Variables
   $id = $_POST['id'];
   $table = $POST['table'];
   
   //Check for exsistence
   if (!$id or !$table)
     {
      echo "<font color='#ff0000'>You must enter a Session Name</font>";
     } else {
      echo "Congrats!";
     }

?>
         <h2 style="color:#fff">ReH-0.1 Password Encryption</h2><br><br>
         <font color="#fff">Before encrypting your password, a session must be started. We need you to enter a personal session name that you will remember so that you password is protected by this session.</font><br><br>
         <form action="session_start.php" method="POST">
            <label for="id" style="color: #fff;">Session Name:
            <input type="text" maxlength="10" size="10" name="id"></label><br><br>
            <label for="table" style="color: #fff;">Session Name Confirmation:
            <input type="text" maxlength="10" size="10" name="table"></label><br><br>
            <input type="submit" value="Create Session">
         </form>
      </center>
   </body>
</html>

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.