I need a little help with a sniplet of code as im very new to PHP development

Hello there i need a little help as im very new to php and i need a little help on something. I need some code that will display an error message or a html form depending on what is or isn’t in the value feild of a table in a mysql database

and these are the values i have to use in the table of the mysql database. Well the ones i wish to use anyway.

`user_id``value``created_at`

these are the total that can be used

`id``field_id``user_id``value``created_at`

What it is i want to do is if the value of value is empty show a html form and if the value of value has data in it then show/echo an error message that i can wrap a CSS

class around it saying "Sorry you already have value that you created on created_at" This is a message that will be individual to every registered user defined by their personal row in the users table in the database.

Thanks for the help in advance! ::slight_smile:

[php]

<?php $con = mysql_connect(hostname,username,password); // the connection to ur database $db = mysql_select_db(dbname); // select the database name $rs = mysql_query("select * from table_name"); // the sql query to fetch records from ur table if(mysql_num_rows($rs)>0) { if($row['value']=='') // display the html form { ?>
 <form>
      <!-- design your html form here -->
 </form>
 <?php } else // display the css div with message
 { ?>   
 <div class="cssname">Sorry, you already have data created on <?= $row['created_at']; ?>.
 <?php } 

}
else
echo ‘Failed to retrieve records’;
?>
[/php]

Hiya there, :smiley:

Thanks for your help and your php code submittion however it isnt working :frowning: if you see the code below and scroll down to line 83 where your code starts. See if its something that im doing wrong!!! all the other code above line 83 is my code thats in the page where i want your code to work in.

[php]<?php

include("./xmlapi.php"); //XMLAPI cpanel client class

// Default whm/cpanel account info

$ip = “localhost”; // should be WHM ip address
$account = “dbusername”; // cpanel user account name
$passwd =“dbpassword”; // cpanel user password
$port =2083; // cpanel secure authentication port unsecure port# 2082

$email_domain = ‘domain.com’; // email domain (usually same as cPanel domain)
$email_quota = 50; // default amount of space in megabytes

/End of Setting**********/

/**Start of Username Ban Array/

$user = array(‘user’, ‘admin’, ‘administrator’);
if (in_array(‘user’, $user, true)) {
echo “Sorry you are using a banned username”;
}

/***End of Username Ban Array/

function getVar($name, $def = ‘’) {
if (isset($_REQUEST[$name]))
return $_REQUEST[$name];
else
return $def;
}
// check if overrides passed
$email_user = getVar(‘user’, ‘’);
$email_pass = getVar(‘pass’, $passwd);
$email_vpass = getVar(‘vpass’, $vpasswd);
$email_domain = getVar(‘domain’, $email_domain);
$email_quota = getVar(‘quota’, $email_quota);

$msg = ‘’;
if (!empty($email_user))
while(true) {

if ($email_pass !== $email_vpass){ //check password
$msg = “Email password does not match”;
break;
}

$xmlapi = new xmlapi($ip);

$xmlapi->set_port($port); //set port number. cpanel client class allow you to access WHM as well using WHM port.

$xmlapi->password_auth($account, $passwd); // authorization with password. not as secure as hash.

// cpanel email addpop function Parameters
$call = array(domain=>$email_domain, email=>$email_user, password=>$email_pass, quota=>$email_quota);

$xmlapi->set_debug(0); //output to error file set to 1 to see error_log.

$result = $xmlapi->api2_query($account, “Email”, “addpop”, $call ); // making call to cpanel api

//for debugging purposes. uncomment to see output
//echo ‘Result\n

’;
//print_r($result);
//echo ‘
’;

if ($result->data->result == 1){
$msg = $email_user.’@’.$email_domain.’ account created’;

} else {
$msg = $result->data->reason;
break;
}

break;
}

?>

<?php echo '
'.$msg.'
'; ?> <?php // START OF YOUR CUSTOM CODE $con = mysql_connect(localhost,username,password); // the connection to ur database $db = mysql_select_db(mydatabasename); // select the database name $rs = mysql_query("select * from mytablename"); // the sql query to fetch records from ur table if(mysql_num_rows($rs)>0) { if($row['value']=='') // display the html form { ?> // THIS IS THE START OF MY FORM
Username:
Password:
Verify Password:

// THIS IS THE END OF MY FORM
 <?php } else // display the css div with message  // THIS IS WHERE YOUR CODE RESTARTS
 { ?>   
 <div class="error">Sorry, you already have data created on <?= $row['created_at']; ?>.
 <?php } 

}
else
echo ‘Failed to retrieve records’;
?> // END OF YOUR CUSTOM CODE[/php]

I have also provided a picture of the table from phpmyadmin to show you how the table inside the database is structured. Please see below.

Please do let me know why you think its not working. Thanks so much in advance for all your valued help.

I have a funny feeling that the code your posted for me on line that shows

[php]if($row[‘value’]==’’)[/php]

Has not a definishion in my code so i think thats why its not working as it should. So my next question would be how would i define the above row of code to work in my file?.

Again thanks for all help in advance.

yes, there is one line missing in there…sorry my bad.

add this line
$row = mysql_fetch_assoc($rs);

before the if condition line i.e., … ‘if($row[‘value’]==…’

perfect now one more thing i have an issue with i have created an in_array() shown below. What this array is suposed to do is prevent as many usernames i want to ban banned from being registered. But at the moment it doesnt ban the usernames and its shows the message Sorry you are using a banned username all the time the page is open. Would you know how to get this to work? so it actually stops restricted usernames from being registered and if its a banned username then display the error message.

[php]/**Start of Username Ban Array/

$user = array(‘user’, ‘admin’, ‘administrator’);
if (in_array(‘user’, $user, true)) {
echo ‘

Sorry you are using a banned username’;
}

/***End of Username Ban Array/[/php]

you are always getting “sorry you are using a banned username” message because the condition is always becoming true.

the condition is always becoming true because you hard-coded the first parameter of the function (in_array) to ‘user’. Instead you are supposed to give the user input as first parameter.

for ex: if the user has submitted data thru POST,
[php]
$userinput = $_POST[‘username’];
$user = array(‘user’, ‘admin’, ‘administrator’);
if (in_array($userinput, $user, true)) {
echo ‘

Sorry you are using a banned username’;
}
[/php]

here, i am sending the value given by user as first parameter to in_array() function instead of a static string ‘user’.

hope it is understood well.

Ok that worked ok but its saying its banning the username, whch is half way there as the message only comes up when a banned username is entered. But it still allows it to be created how would i actually stop the account process from being processed if a banned username is entered.

Also i have a problem when the form is submitted

This is an image of the form page when i open it in the browser (all good)

However something odd happends when i press on the " Create Mailbox" button and below is what happands. And im not sure why its doing it

We are nearly there buddy its almost working as it should be. If you want the whole code of the page to date let me know and ill post it here for you.

Thanks again in advance.

no need of the code i guess…
what u must have done is like this…

[php]
$userinput = $_POST[‘username’];
$user = array(‘user’, ‘admin’, ‘administrator’);
if (in_array($userinput, $user, true)) {
echo ‘

Sorry you are using a banned username’;
}
// code for inserting the records placed here like this… this executes independent of user input condition… which is happening in ur case. right?
[/php]

instead u should place the insertion code in the else part of the if condition like below

[php]
$userinput = $_POST[‘username’];
$user = array(‘user’, ‘admin’, ‘administrator’);
if (in_array($userinput, $user, true))
{
echo ‘

Sorry you are using a banned username’;
}
else
{
// code for inserting the records… this executes independent of user input condition
}
[/php]

now, the insertion of records takes place only when the condition is false i.e., only when the user gives a valid user name.

cheers !!

Ok, now i have another error come up it says:

“Parse error: syntax error, unexpected T_VARIABLE in /home/user/public_html/portal/dir/index.php on line 87”

Line 87 is “$db = mysql_select_db(databasename);” but the database name in my file is the correct one. When i re-upload the old file without the update above ban code in it its all fine with no errors.

Here is the full file with the above ban array update put in.

[php]<?php

include("./xmlapi.php"); //XMLAPI cpanel client class

// Default whm/cpanel account info

$ip = “localhost”; // should be WHM ip address
$account = “username”; // cpanel user account name
$passwd =“password”; // cpanel user password
$port =2083; // cpanel secure authentication port unsecure port# 2082

$email_domain = ‘domain.com’; // email domain (usually same as cPanel domain)
$email_quota = 10; // default amount of space in megabytes

/End of Setting**********/

$userinput = $_POST[‘user’];
$user = array(‘user’, ‘admin’, ‘administrator’);
if (in_array($userinput, $user, true))
{
echo ‘

Sorry you are using a banned username
’;
}
else
{

function getVar($name, $def = ‘’) {
if (isset($_REQUEST[$name]))
return $_REQUEST[$name];
else
return $def;
}
// check if overrides passed
$email_user = getVar(‘user’, ‘’);
$email_pass = getVar(‘pass’, $passwd);
$email_vpass = getVar(‘vpass’, $vpasswd);
$email_domain = getVar(‘domain’, $email_domain);
$email_quota = getVar(‘quota’, $email_quota);

$msg = ‘’;
if (!empty($email_user))
while(true) {

if ($email_pass !== $email_vpass){ //check password
$msg = ‘

Email password does not match’;
break;
}

$xmlapi = new xmlapi($ip);

$xmlapi->set_port($port); //set port number. cpanel client class allow you to access WHM as well using WHM port.

$xmlapi->password_auth($account, $passwd); // authorization with password. not as secure as hash.

// cpanel email addpop function Parameters
$call = array(domain=>$email_domain, email=>$email_user, password=>$email_pass, quota=>$email_quota);

$xmlapi->set_debug(0); //output to error file set to 1 to see error_log.

$result = $xmlapi->api2_query($account, “Email”, “addpop”, $call ); // making call to cpanel api

//for debugging purposes. uncomment to see output
//echo ‘Result\n

’;
//print_r($result);
//echo ‘
’;

if ($result->data->result == 1){
$msg = $email_user.’@’.$email_domain.’ account created’;

} else {
$msg = $result->data->reason;
break;
}

break;
}

?>

<?php echo '
'.$msg.'
'; ?> <?php $con = mysql_connect(localhost,username,password); $db = mysql_select_db(databasename); $rs = mysql_query("select * from wp_mngl_custom_field_values"); if(mysql_num_rows($rs)>0) { $row = mysql_fetch_assoc($rs); if($row['value']=='') { ?>
Username:
Password:
Verify Password:

<?php } else // display the css div with message { ?>
Sorry, you already have reached your quota for this account. Due to high demand for the service we carnt issue you with another mailbox on this account at this stage. From our records we can see you have already registered <?= $row['value']; ?> on <?= $row['created_at']; ?>. <?php } } else echo 'Failed to retrieve records';

?>[/php]

If you could let me know why you think this may be that would be super

UPDATE FROM MY LAST POST

I had the wrong database password entered into the file lol. But this is the error coming up now the correct password for the database was updated in the file.

Parse error: syntax error, unexpected $end in /home/myebox/public_html/portal/signupscript/index.php on line 112

“unexpected $end” error typically means that some where you missed to close “}”… so, u dont u go thru ur code thoroughly and find out where u missed to close it. :slight_smile:

Well, i tried to do that myself… i figured, ‘}’ is missing at the very end… after the line “echo ‘Failed to retrieve records’;”

i feel suck a noob i forgot the curly thiny on the line the file is working as it should now lol thanks so much for all your help

Right one very last thing and this is really the last lol when the the user creates they username

i want it to update ‘value’ feild in the table with the username followed by @domain.com as this will then stop the user from creating any more than 1 username/email address. This is why i asked for this code

[php]<?php

$con = mysql_connect(localhost,username,password);
$db = mysql_select_db(username);

$rs = mysql_query(“select * from wp_mngl_custom_field_values”);
if(mysql_num_rows($rs)>0)
{
$row = mysql_fetch_assoc($rs);
if($row[‘value’]==’’)
{ ?>

Username:
Password:
Verify Password:

<?php } else // display the css div with message { ?>
Sorry, you already have reached your quota for this account. Due to high demand for the service we carnt issue you with another mailbox on this account at this stage. From our records we can see you have already registered <?= $row['value']; ?> on <?= $row['created_at']; ?>. <?php } } else echo 'Failed to retrieve records'; } ?>[/php]

As this is the code to check the ‘value’ feild in the table for data and if there is it displays an error saying the user has reached thier quota for thier account. But if there value of ‘value’ is empty it displays the form. But at this moment in time i have no way of getting this file to update the ‘value’ table with the data the user has posted in the form, followed by @domain.com.

By the way suck was suposed to be such ops :o i have garn so red faced now sorry.

simple,

right after the record insertion code, add one more query as follows

$rs1 = mysql_query(“update tablename set value=’”.$username."@domanname.com’ where userid=".$userid);

this line updates the value field as u desired.

Ops when i put that line of code in i got a mysql connection error lol, Where in the below code would i put the above line of code.

[php]$userinput = $_POST[‘user’];
$user = array(‘user’, ‘admin’, ‘administrator’, ‘shit’, ‘fucker’);
if (in_array($userinput, $user, true))
{
echo ‘

Sorry you are using a banned username’;
}
else
{

function getVar($name, $def = ‘’) {
if (isset($_REQUEST[$name]))
return $_REQUEST[$name];
else
return $def;
}
// check if overrides passed
$email_user = getVar(‘user’, ‘’);
$email_pass = getVar(‘pass’, $passwd);
$email_vpass = getVar(‘vpass’, $vpasswd);
$email_domain = getVar(‘domain’, $email_domain);
$email_quota = getVar(‘quota’, $email_quota);

$msg = ‘’;
if (!empty($email_user))
while(true) {

if ($email_pass !== $email_vpass){ //check password
$msg = ‘

Email password does not match’;
break;
}

$xmlapi = new xmlapi($ip);

$xmlapi->set_port($port); //set port number. cpanel client class allow you to access WHM as well using WHM port.

$xmlapi->password_auth($account, $passwd); // authorization with password. not as secure as hash.

// cpanel email addpop function Parameters
$call = array(domain=>$email_domain, email=>$email_user, password=>$email_pass, quota=>$email_quota);

$xmlapi->set_debug(0); //output to error file set to 1 to see error_log.

$result = $xmlapi->api2_query($account, “Email”, “addpop”, $call ); // making call to cpanel api

//for debugging purposes. uncomment to see output
//echo ‘Result\n

’;
//print_r($result);
//echo ‘
’;

if ($result->data->result == 1){
$msg = $email_user.’@’.$email_domain.’ account created’;

} else {
$msg = $result->data->reason;
break;
}

break;
}

?>[/php]

Would i be right in saying it would go before this line of code?

$xmlapi = new xmlapi($ip);

Because after that it starts to call things from the cPanel API file

i think the code i gave shall be placed as below

[php]
if ($result->data->result == 1){
$msg = $email_user.’@’.$email_domain.’ account created’;
// place the code i gave here…
} else {

[/php]

Ok when i place the line of code

$rs1 = mysql_query("update tablename set value='".$username."@domanname.com' where userid=".$userid);

below

if ($result->data->result == 1){ $msg = $email_user.'@'.$email_domain.' account created';

I get the following error message that comes up, It will create the email account on the server as im not using a banned username. But that error message will come up that shouldnt be there.

[code]Warning: mysql_query() [function.mysql-query]: Access denied for user ‘nobody’@‘localhost’ (using password: NO) in /home/username/public_html/portal/signupscript/index.php on line 71

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /home/username/public_html/portal/signupscript/index.php on line 71[/code]

This error only comes up when i click on create mailbox button, when you open the page there are no errors.

i guess, you have some different mechanism for executing sql queries thru php. thats why, my script is not executed…

so, you have to tell me how you are inserting user details into the database table… so, that we can follow same procedure to execute the update statement as well.

Sponsor our Newsletter | Privacy Policy | Terms of Service