Question

I'm having troubles looking at comparing a string with a char. I have a dropdown menu with two options 1. Coach 2. Athlete. When submitted, values "C" or "A" are stored in my database under "type". I've already made the program see if "user" has already been created and will create a directory if the "user" doesn't exist. So want to be able to create a directory only when "C" is submitted as the user account "type".

My code below: mytest.php

<?php
$username = "root";
$password = "";
$hostname = "localhost";
$database = "basketball_database";
$table = "users";

mysql_connect($hostname, $username, $password)
 or die("Unable to connect to MYsql");
// echo "Connected to mysql<br>";

 mysql_select_db("$database")
 or die("Could not select Basketball_database");
 //echo "Connected to database";

 //check to see if user exists
 $userquery = mysql_query("SELECT login_name FROM $table WHERE login_name='$_POST[loginName]'");

  if (mysql_num_rows($userquery) != 0)
  {
      echo "Username already exists";
  }//end user exists

//insert data to database
  else
  {
 //insert into database
 $mysql = "INSERT INTO $table(login_name, password, last_name, first_name, type) VALUES('$_POST[loginName]','$_POST[password]','$_POST[lastName]','$_POST[firstName]','$_POST[dropdown]')";
 echo "User was added";
 echo '<br>';

 if(!mysql_query($mysql))
 die("Disconnected");

 mysql_close();

The section I need the help on.
//create folder

 //check to see if user is coach
 $typequery = mysql_query("SELECT type FROM $table WHERE type='$_POST[dropdown]'"); 
 $Ctype="C";
 if (strcmp($typequery,$Ctype))
  {
      echo "Directory not created for Athletes";
  }// directory for athletes

    //Submit button create directory
  else
  {
 if(isset($_POST['reg_submit'])){
     $username=$_POST['loginName'];
    $cur = getcwd();

    if(is_dir($cur . '/'.$username))
    {
        echo "Directory Exists";
    }//end directory exist

    //create directory
    else{
    $new = mkdir($cur . '/'.$username, 0777);

    if ( $new ) {
        echo '<p>Directory created</p>';
    } //end directory created
    else {
        echo '<p>Directory not created</p>';
    }//end directory failed
    }//end create directory
 }// end Submit button create directory
  }//end insert data to database
  }//end else user exist
?>

<html>
<title>User Added</title>

<body>
</body>
<form action="users-form.php" method="post">
<input type="submit" value="Add Another User">
</form>
</html>

If you need the corresponding page that uses the submit button let me know. Thank you

No correct solution

OTHER TIPS

If I understand well, then you should filter for (i.e.) the login_name to get type of the user:

$typequery = mysql_query("SELECT type FROM $table WHERE login_name='$_POST[loginName]");

Don't forget fetching the row from the $typequery - it's only a resource! (see http://php.net/mysql_query)

When you get the type you can compare it with a simple '==' operator, as @abbiya said.

Finally, php mysql library is deprecated, your code is very vulnerable - see this: Why shouldn't I use mysql_* functions in PHP?

Not sure what you want but the strcmp function returns 0 if both strings are equal. In your case even if user submits form with c or a the else part will be executed. Check the strcmp function return values. Also you can just compare chars with ===

If(typequery == ctype) { //create a folder }else if(typequery == 'A') {//show message }else {}

Try the above way

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top