Results 1 to 11 of 11
  1. #1
    Join Date
    Mar 2004
    Location
    Somewhere
    Posts
    21

    Unanswered: Mysql/php problem

    I am having trouble with this script that it won't actually email or insert the data into the table
    The table is called STAFF with the fields (Nickname, Title, Description, HoodID, BlockID, email, password)
    the script is below

    <?php

    ini_set ('Display_errors' , 1);

    error_reporting (E_ALL & ~E_NOTICE);

    if ($dbc = @mysql_connect ('localhost' , 'blitz_campus' , 'campus')) //connect to database

    {

    if (!@mysql_select_db ('blitz_campus'))

    {

    die ('<p>Could not select the database because: <b>' . mysql_error() . '</b></p>'); //produce error if could not connect to database

    }

    } else {

    die ('<p>Could not connect to MySQL because: <b>' . mysql_error() . '</b></p>'); //produce error if could not connect

    }

    if ($_POST['Title'] = ND) { //check position

    $query = "INSERT INTO STAFF (Nickname, HoodID, Position) VALUES ('{$_POST['Nickname']}', '{$_POST['HoodID']}', '{$_POST['Position']}'"; //produce query

    } elseif ($_POST['Title'] = BL) {

    $query = "INSERT INTO STAFF (Nickname, BlockID) VALUES ('{$_POST['Nickname']}', '{$_POST['BlockID']}'"; //produce query

    } elseif ($_POST['Title'] = BD) {

    $query = "INSERT INTO STAFF (Nickname, BlockID, Position) VALUES ('{$_POST['Nickname']}', '{$_POST['BlockID']}', '{$_POST['Position']}'"; //produce query

    } elseif ($_POST['Title'] = NL) {

    $n = rand(1000,9999); //produce 4 digit code

    $password = crypt ($n); //encrypt password

    $query = "INSERT INTO STAFF (Nickname, HoodID, Email, Password) VALUES ('{$_POST['Nickname']}', '{$_POST['HoodID']}', '{$_POST['Email']}, '{$Password}'";

    //produce email

    $body = "Congratulations on becoming a NL in The campus, on becoming NL you now have access to the staff section on the recruitment database located at www.campus.blitzonline.net. You Password is {$n}. You can change this when you first logon. Good Luck in your new position";

    mail ($_POST['Email'], 'Campus Recuitment Database logon', $body, 'From: newuser@campus.blitzonline.net');

    } elseif ($_POST['Title'] = Cstaff) {

    $n = rand(1000,9999); //produce 4 digit code

    $password = crypt ($n); //encrypt password

    $query = "INSERT INTO STAFF (Nickname, Position, Email, Password) VALUES ('{$_POST['Nickname']}', '{$_POST['Position']}', '{$_POST['Email']}, '{$Password}'";

    $body = "Congratulations on becoming a NL in The campus, on becoming C-staff you now have access to the staff section on the recruitment database located at www.campus.blitzonline.net. You Password is {$n}. You can change this when you first logon. Good Luck in your new position"; //produce email

    mail ($_POST['Email'], 'Campus Recuitment Database logon', $body, 'From: newuser@campus.blitzonline.net');

    } else

    {
    print "Unable to add to database";
    }

    include ('addhtml.php');

    ?>

    any help would be welcome thanks :P Blitz
    CU Blitz

  2. #2
    Join Date
    Jan 2004
    Location
    India
    Posts
    168
    You script seems to be OK, You may have to debug this at each point. For all the queries put a die(mysql_error()) Then check till where the control comes.
    This would be the only way to finx this.
    Are you getting any error? If so put it here. Lets have a look on that.

    Regards
    Rajesh
    Freelance and Technology Consultant
    -------------------
    Dreams are for ever
    -------------------

  3. #3
    Join Date
    Mar 2004
    Location
    Somewhere
    Posts
    21
    no, no error, just no email and no files in the table :P Blitz
    CU Blitz

  4. #4
    Join Date
    Dec 2004
    Location
    Beernem, Belgium
    Posts
    1

    PHP/MYsQl Problem

    The reason why your script isn't working is that you don't execute the query.
    You just define the query in the variable $query but you don't execute the statement.

    Insert the before the mail-function :

    $Result1 = mysql_query($query, $your_connection_string) or die(mysql_error());

    Then your query should be executed.

  5. #5
    Join Date
    Mar 2004
    Location
    Somewhere
    Posts
    21
    ok thanks, was sure i forgot something. thanks alot :P Dan
    CU Blitz

  6. #6
    Join Date
    Mar 2004
    Location
    Somewhere
    Posts
    21
    Still unable to get it to work, the form won't mail me, and also won't store in the tables
    any help would be appreciated
    getting the error
    Query failed: You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
    <?php

    ini_set ('Display_errors' , 1);

    error_reporting (E_ALL & ~E_NOTICE);

    if ($dbc = @mysql_connect ('localhost' , 'blitz_campus' , 'campus')) //connect to database

    {

    if (!@mysql_select_db ('blitz_campus'))

    {

    die ('<p>Could not select the database because: <b>' . mysql_error() . '</b></p>'); //produce error if could not connect to database

    }

    } else {

    die ('<p>Could not connect to MySQL because: <b>' . mysql_error() . '</b></p>'); //produce error if could not connect

    }

    if ($_POST['Title'] = ND) { //check position

    $query = "INSERT INTO STAFF (Nickname, HoodID, Position) VALUES ('{$_POST['Nickname']}', '{$_POST['HoodID']}', '{$_POST['Position']}'"; //produce query
    $result = mysql_query($query) or die('Query failed: ' . mysql_error());
    } elseif ($_POST['Title'] = BL) {

    $query = "INSERT INTO STAFF (Nickname, BlockID) VALUES ('{$_POST['Nickname']}', '{$_POST['BlockID']}'"; //produce query
    $result = mysql_query($query) or die('Query failed: ' . mysql_error());

    } elseif ($_POST['Title'] = BD) {

    $query = "INSERT INTO STAFF (Nickname, BlockID, Position) VALUES ('{$_POST['Nickname']}', '{$_POST['BlockID']}', '{$_POST['Position']}'"; //produce query

    $result = mysql_query($query) or die('Query failed: ' . mysql_error());
    } elseif ($_POST['Title'] = NL) {

    $n = rand(1000,9999); //produce 4 digit code

    $password = crypt ($n); //encrypt password

    $query = "INSERT INTO STAFF (Nickname, HoodID, Email, Password) VALUES ('{$_POST['Nickname']}', '{$_POST['HoodID']}', '{$_POST['Email']}, '{$Password}'";

    $result = mysql_query($query) or die('Query failed: ' . mysql_error());
    //produce email

    $body = "Congratulations on becoming a NL in The campus, on becoming NL you now have access to the staff section on the recruitment database located at www.campus.blitzonline.net. You Password is {$n}. You can change this when you first logon. Good Luck in your new position";

    mail ($_POST['Email'], 'Campus Recuitment Database logon', $body, 'From: newuser@campus.blitzonline.net');

    $result = mysql_query($query) or die('Query failed: ' . mysql_error());

    } elseif ($_POST['Title'] = Cstaff) {

    $n = rand(1000,9999); //produce 4 digit code

    $password = crypt ($n); //encrypt password

    $query = "INSERT INTO STAFF (Nickname, Position, Email, Password) VALUES ('{$_POST['Nickname']}', '{$_POST['Position']}', '{$_POST['Email']}, '{$Password}'";

    $body = "Congratulations on becoming a NL in The campus, on becoming C-staff you now have access to the staff section on the recruitment database located at www.campus.blitzonline.net. You Password is {$n}. You can change this when you first logon. Good Luck in your new position"; //produce email

    mail ($_POST['Email'], 'Campus Recuitment Database logon', $body, 'From: newuser@campus.blitzonline.net');

    $result = mysql_query($query) or die('Query failed: ' . mysql_error());

    } else

    {
    print "Unable to add to database";
    }
    include ('addhtml.php');

    ?>
    Last edited by blitzaceuk; 01-07-05 at 05:37.
    CU Blitz

  7. #7
    Join Date
    Jul 2003
    Posts
    73
    I notice you're not closing the parenthesis for each of your queries. For example:
    PHP Code:
    $query "INSERT INTO STAFF (Nickname, HoodID, Position) VALUES ('{$_POST['Nickname']}', '{$_POST['HoodID']}', '{$_POST['Position']}'"
    should be:
    PHP Code:
    $query "INSERT INTO STAFF (Nickname, HoodID, Position) VALUES ('{$_POST['Nickname']}', '{$_POST['HoodID']}', '{$_POST['Position']}')"
    (note the close parenthesis - ) - at the end of the statement)
    Joel Dixon
    Analyst Programmer
    Melbourne, Australia

  8. #8
    Join Date
    Mar 2004
    Location
    Somewhere
    Posts
    21
    Hey thanks :P Blitz
    CU Blitz

  9. #9
    Join Date
    Mar 2004
    Location
    Somewhere
    Posts
    21
    Still having trouble, it runs through fine its just that it only stores data in 2 of the fields, so i am assuming it is something to do with the if statements as the mail function is not being sent, could someone please check the if statements to see what i am doing wrong, thanks so much, Dan
    Code:
    <html>
    <head>
    <title>Campus Recruitment v0.1</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>
    <body>
    
    <?php
    ini_set ('Display_errors' , 1);
    error_reporting (E_ALL & ~E_NOTICE);
    include ('addhtml.php');
    	if ($dbc = @mysql_connect ('localhost' , 'blitz_campus' , 'campus')) //connect to database
    	{
    		if (!@mysql_select_db ('blitz_campus'))
    		{
    		die ('<p>Could not select the database because: <b>' . mysql_error() . '</b></p>'); //produce error if could not connect to database
    		}
    
    	}	 
    	else
    	{
    	die ('<p>Could not connect to MySQL because: <b>' . mysql_error() . '</b></p>'); //produce error if could not connect
    	}
    	
    if ($_POST['Title'] = "ND")
    	{ 
    	$query = "INSERT INTO STAFF (Nickname, HoodID, Position) VALUES ('{$_POST['Nickname']}', '{$_POST['HoodID']}',  '{$_POST['Position']}')"; //produce query
    	$result = mysql_query($query) or die('Query failed: ' . mysql_error());
    	print "Added ND to database";
    	}
    elseif ($_POST['Title'] = "BL")
    	{
    	$query = "INSERT INTO STAFF (Nickname, BlockID) VALUES ('{$_POST['Nickname']}', '{$_POST['BlockID']}')"; //produce query
    	$result = mysql_query($query) or die('Query failed: ' . mysql_error());
    	print "Added BL to Database";
    	} 
    elseif ($_POST['Title'] = "BD")
    	{
    	$query = "INSERT INTO STAFF (Nickname, BlockID, Position) VALUES ('{$_POST['Nickname']}', '{$_POST['BlockID']}',  '{$_POST['Position']}')"; //produce query
    	$result = mysql_query($query) or die('Query failed: ' . mysql_error());
    	print "Added BD to database";
    	}
    elseif ($_POST['Title'] = "NL")
    	{
    	$n = rand(1000,9999); //produce 4 digit code
    	$password = crypt ($n); //encrypt password
    	$query = "INSERT INTO STAFF (Nickname, HoodID, Email, Password) VALUES ('{$_POST['Nickname']}', '{$_POST['HoodID']}',  '{$_POST['Email']}, '{$Password}')";
    	$result = mysql_query($query) or die('Query failed: ' . mysql_error());
    //produce email
    	$body = "Congratulations on becoming a NL in The campus, on becoming NL you now have access to the staff section on the recruitment database located at www.campus.blitzonline.net. You Password is {$n}. You can change this when you first logon. Good Luck in your new position";
    	mail ($_POST['Email'], 'Campus Recuitment Database logon', $body, 'From: newuser@campus.blitzonline.net');
    	$result = mysql_query($query) or die('Query failed: ' . mysql_error());
    	print "Added NL to database with password" . $n . " ";
    	}
    elseif ($_POST['Title'] = "Cstaff")
    	{
    	$n = rand(1000,9999); //produce 4 digit code
    	$password = crypt ($n); //encrypt password
    	$query = "INSERT INTO STAFF (Nickname, Position, Email, Password) VALUES ('{$_POST['Nickname']}', '{$_POST['Position']}',  '{$_POST['Email']}, '{$password}')";
    	$body = "Congratulations on becoming a NL in The campus, on becoming C-staff you now have access to the staff section on the recruitment database located at www.campus.blitzonline.net. You Password is {$n}. You can change this when you first logon. Good Luck in your new position"; //produce email
    	mail ($_POST['Email'], 'Campus Recuitment Database logon', $body, 'From: newuser@campus.blitzonline.net');
    	$result = mysql_query($query) or die('Query failed: ' . mysql_error());
    	print "Added Cstaff to database with password" . $n . " ";
    	}
    else
    	{ 
    	print "Unable to add to database";
    	}
    ?>
    
    
    
    </body>
    
    </html>
    html form posting to php script
    Code:
    <html>
    <head>
    <title>Campus Recruitment v0.1</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>
    <body>
    
    <html>
    
    
    
    <head>
    
    
    
    <title>Campus Recruitment 0.1 beta</title>
    
    
    
    </head>
    
    
    
    <body>
    
    
    
    <form action="add.php" method="post">
    
    
    
    <p>Nickname: <input type="text" name="Nickname" size="20" maxsize="20" /><br>
    
    
    
    Email: <input type="text" name="Email" size="40" maxsize="80" /><br>
    
    
    
    Job: 
    
    <select name="Title">
    
      <option selected>-Select-</option>
    
      <option value="BD">BD</option>
    
      <option value="BL">BL</option>
    
      <option value="ND">ND</option>
    
      <option value="NL">NL</option>
    
      <option value="Cstaff">C-staff</option>
    
    </select>
    
    <br>
    
    Position: 
    
    <input name="Position" type="text" id="Position">
    
    <br>
    
    Hood: 
    
    <select name="HoodID">
    
      <option selected>-None-</option>
    
      <option value="1">Life Sciences</option>
    
    </select>
    
    <br>
    
    Block: 
    
    <select name="BlockID">
    
      <option selected>-None-</option>
    
      <option value="1">Test1</option>
    
      <option value="2">Test2</option>
    
    </select>
    
    <p>
    
      <input type="submit" name="submit" value="submit" />
    
    </form>
    
    
    
    </body>
    
    
    
    </html>
    
    Added ND to database
    
    
    </body>
    
    </html>
    Last edited by blitzaceuk; 01-13-05 at 10:39.
    CU Blitz

  10. #10
    Join Date
    Jan 2004
    Location
    India
    Posts
    168
    After this line

    else
    {
    print "Unable to add to database";
    }
    // add this line so that we can know which query is exaclty executed and what is the information in that
    echo "Query: $query";

    Then check the query and if still it does not work let me know.
    Regards
    Rajesh
    Freelance and Technology Consultant
    -------------------
    Dreams are for ever
    -------------------

  11. #11
    Join Date
    Mar 2004
    Location
    Somewhere
    Posts
    21
    Solved, i finally realised what i was doing wrong in the if statment
    i had if ($title = "ND");
    when it should be ($title == "ND")
    Thanks to everyone that has helped :P Daz
    Last edited by blitzaceuk; 01-20-05 at 06:42.
    CU Blitz

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •