quite an advanced if statement..

  • Nem
  • Guru
  • Guru
  • Nem
  • Posts: 1243
  • Loc: UK

Post 3+ Months Ago

hey,

Say if i have a script that only runs when certain variables are entered...

I want to make an if statement so...

"If" that script has run then do this action...

} else { "do nothing".

how will i be able to achieve this? I will need to name the code that runs once certain variables are placed?
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Veter
  • Novice
  • Novice
  • User avatar
  • Posts: 20

Post 3+ Months Ago

Put your scripts in functions and call to the functions when you need it.

Use returning values then you need to know if your script is done or not.
  • rjstephens
  • Professor
  • Professor
  • User avatar
  • Posts: 774
  • Loc: Brisbane, Australia

Post 3+ Months Ago

Uh, what language is this? If it's PHP, then, what he said
  • Nem
  • Guru
  • Guru
  • Nem
  • Posts: 1243
  • Loc: UK

Post 3+ Months Ago

so for example

PHP Code: [ Select ]
$function1 = "code here";


then
PHP Code: [ Select ]
"if (function1 = 1);
 
 
 
second code here
  1. "if (function1 = 1);
  2.  
  3.  
  4.  
  5. second code here


yes this is php, im kind of new to this but oh well
i need this to be done if the connection to the database is done, so i should really do:

PHP Code: [ Select ]
if (conn = 1)
??
becuase on my connection script i have
PHP Code: [ Select ]
$conn = mysql_connect("$location","$username","$password");
  • Veter
  • Novice
  • Novice
  • User avatar
  • Posts: 20

Post 3+ Months Ago

PHP Code: [ Select ]
$conn = mysql_connect("$location","$username","$password");
 
 
 
if($conn){ ...do your script... } else {echo "Youre not connected"; }
 
 
 
 
  1. $conn = mysql_connect("$location","$username","$password");
  2.  
  3.  
  4.  
  5. if($conn){ ...do your script... } else {echo "Youre not connected"; }
  6.  
  7.  
  8.  
  9.  


and put @ before mysql_connect()

PHP Code: [ Select ]
$conn = @mysql_connect("$location","$username","$password");


It will not show errors if something will be wrong with mysql server or viriables.
  • Nem
  • Guru
  • Guru
  • Nem
  • Posts: 1243
  • Loc: UK

Post 3+ Months Ago

what is the "@" for?
  • Nem
  • Guru
  • Guru
  • Nem
  • Posts: 1243
  • Loc: UK

Post 3+ Months Ago

Another Q: how do i insert current date and time in to a mysql row??
  • Veter
  • Novice
  • Novice
  • User avatar
  • Posts: 20

Post 3+ Months Ago

Nem wrote:
what is the "@" for?

Read my post. It said right under the code box.

Nem wrote:
Another Q: how do i insert current date and time in to a mysql row??


Use time(); function
PHP Code: [ Select ]
$curtime = time();

$curtime will have current timestamp.

And then add $curtime in your mysql querry.
  • Nem
  • Guru
  • Guru
  • Nem
  • Posts: 1243
  • Loc: UK

Post 3+ Months Ago

oh ok, thanks!

but the time bit wont add the date as well though, will it?
  • Nem
  • Guru
  • Guru
  • Nem
  • Posts: 1243
  • Loc: UK

Post 3+ Months Ago

so altogether:

PHP Code: [ Select ]
 
$conn = @mysql_connect("$location","$username","$password");
 
 
 
if($conn){ ...do your script... } else {echo "Youre not connected"; }
 
 
  1.  
  2. $conn = @mysql_connect("$location","$username","$password");
  3.  
  4.  
  5.  
  6. if($conn){ ...do your script... } else {echo "Youre not connected"; }
  7.  
  8.  



//edited bit underneath

could it be: $lastvisit = date( "F d, Y, H:i, T.");

where $lastvisit is a row
  • Veter
  • Novice
  • Novice
  • User avatar
  • Posts: 20

Post 3+ Months Ago

You have to extract the date from the timestamp with date() function.

or do like this:
PHP Code: [ Select ]
$time   = date( "Y-m-d H:i:s", time() );
  • Nem
  • Guru
  • Guru
  • Nem
  • Posts: 1243
  • Loc: UK

Post 3+ Months Ago

do you see anything wrong with this?

PHP Code: [ Select ]
 
$conn = @mysql_connect("$location","$username","$password");
 
if ($conn) {
 
 
 
$ip = $_SERVER["REMOTE_ADDR"];
 
$user = $_POST["user"] ;
 
$pass = $_POST["pass"];
 
$lastvisit = date( "F d, Y, H:i, T.");
 
 
 
$sql = "INSERT INTO gs_admin (ip, lastvisit, user, pass)
 
        VALUES ('$ip','$lastvisit','$user', '$pass')";  
 
    $query = mysql_query($sql) or die("Cannot query the database.<br>" . mysql_error());
 
   echo = "creating your admin account";
 
   
 
   } else {
 
   echo ("Could not connect"); }
 
 
  1.  
  2. $conn = @mysql_connect("$location","$username","$password");
  3.  
  4. if ($conn) {
  5.  
  6.  
  7.  
  8. $ip = $_SERVER["REMOTE_ADDR"];
  9.  
  10. $user = $_POST["user"] ;
  11.  
  12. $pass = $_POST["pass"];
  13.  
  14. $lastvisit = date( "F d, Y, H:i, T.");
  15.  
  16.  
  17.  
  18. $sql = "INSERT INTO gs_admin (ip, lastvisit, user, pass)
  19.  
  20.         VALUES ('$ip','$lastvisit','$user', '$pass')";  
  21.  
  22.     $query = mysql_query($sql) or die("Cannot query the database.<br>" . mysql_error());
  23.  
  24.    echo = "creating your admin account";
  25.  
  26.    
  27.  
  28.    } else {
  29.  
  30.    echo ("Could not connect"); }
  31.  
  32.  
  • Scorpius
  • Proficient
  • Proficient
  • User avatar
  • Posts: 401
  • Loc: Scorpion Hole

Post 3+ Months Ago

Well you have an un needed space
PHP Code: [ Select ]
$user = $_POST["user"] ;

change to
PHP Code: [ Select ]
$user = $_POST["user"];

don't know if thats the only one, but only one i see.
  • Veter
  • Novice
  • Novice
  • User avatar
  • Posts: 20

Post 3+ Months Ago

PHP Code: [ Select ]
echo ("Could not connect");

should be
PHP Code: [ Select ]
echo "Could not connect";
  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Posts: 3245
  • Loc: South Africa

Post 3+ Months Ago

For the date time you could also use the SQL NOW() function.
PHP Code: [ Select ]
 
$conn = @mysql_connect("$location","$username","$password");
 
if ($conn) {
 
 
 
$ip = $_SERVER["REMOTE_ADDR"];
 
$user = $_POST["user"] ;
 
$pass = $_POST["pass"];
 
 
 
$sql = "INSERT INTO gs_admin (ip, lastvisit, user, pass)
 
       VALUES ('$ip',NOW(),'$user', '$pass')";      
 
    $query = @mysql_query($sql);
 
   if (mysql_errno($conn) > 0){
 
        echo = "A database error occured<br>" . mysql_error();
 
    }else{
 
        echo = "creating your admin account";    
 
    }
 
  } else {
 
    echo ("Could not connect");
 
}
 
 
  1.  
  2. $conn = @mysql_connect("$location","$username","$password");
  3.  
  4. if ($conn) {
  5.  
  6.  
  7.  
  8. $ip = $_SERVER["REMOTE_ADDR"];
  9.  
  10. $user = $_POST["user"] ;
  11.  
  12. $pass = $_POST["pass"];
  13.  
  14.  
  15.  
  16. $sql = "INSERT INTO gs_admin (ip, lastvisit, user, pass)
  17.  
  18.        VALUES ('$ip',NOW(),'$user', '$pass')";      
  19.  
  20.     $query = @mysql_query($sql);
  21.  
  22.    if (mysql_errno($conn) > 0){
  23.  
  24.         echo = "A database error occured<br>" . mysql_error();
  25.  
  26.     }else{
  27.  
  28.         echo = "creating your admin account";    
  29.  
  30.     }
  31.  
  32.   } else {
  33.  
  34.     echo ("Could not connect");
  35.  
  36. }
  37.  
  38.  


is the way I would deal with this. Why dont you just write your connection to a function
PHP Code: [ Select ]
 
function openCon(){
 
 $db = connect to db;
 
 if ($db){
 
 return $db;
 
 }else{
 
  return false;
 
 }
 
}
 
 
 
if ($db_con = openCon()){
 
//process
 
}else{
 
//failed to connect
 
}
 
 
  1.  
  2. function openCon(){
  3.  
  4.  $db = connect to db;
  5.  
  6.  if ($db){
  7.  
  8.  return $db;
  9.  
  10.  }else{
  11.  
  12.   return false;
  13.  
  14.  }
  15.  
  16. }
  17.  
  18.  
  19.  
  20. if ($db_con = openCon()){
  21.  
  22. //process
  23.  
  24. }else{
  25.  
  26. //failed to connect
  27.  
  28. }
  29.  
  30.  


HTH
  • Nem
  • Guru
  • Guru
  • Nem
  • Posts: 1243
  • Loc: UK

Post 3+ Months Ago

hm, still doesnt seem to work.. nothing shows up.

My scenario:

I have a form asking for certain inputs to connect to database. Once it connects the sql code retrieves the information by a $_POST because the form is a $_POST.

Once it connects it automatically creates tables, and it also mentions it.

This i tested and works.

Straight after creating the tables in the database i need it to create the admin account. So a row in one of the tables it has created.

Would i still need to connect again or just insert the row?

I tried to just insert the row using:

PHP Code: [ Select ]
 
$ip = $_SERVER["REMOTE_ADDR"];
 
$user = $_POST["user"] ;
 
$pass = $_POST["pass"];
 
 
 
//insert the stuff
 
$sql = "INSERT INTO gs_admin (ip, lastvisit, user, pass)
 
       VALUES ('$ip',NOW(),'$user', '$pass')";
 
 
  1.  
  2. $ip = $_SERVER["REMOTE_ADDR"];
  3.  
  4. $user = $_POST["user"] ;
  5.  
  6. $pass = $_POST["pass"];
  7.  
  8.  
  9.  
  10. //insert the stuff
  11.  
  12. $sql = "INSERT INTO gs_admin (ip, lastvisit, user, pass)
  13.  
  14.        VALUES ('$ip',NOW(),'$user', '$pass')";
  15.  
  16.  


But the code above this starts of with "sql = " which i reckon stops it from using this.

Whats the best thing to do in this case?
  • Nem
  • Guru
  • Guru
  • Nem
  • Posts: 1243
  • Loc: UK

Post 3+ Months Ago

i have also now tried:

PHP Code: [ Select ]
 
//Now insert the admin stuff.  GETTING PROBLEMS BELOW THIS BIT
 
$ip = $_SERVER["REMOTE_ADDR"];
 
$user = $_POST["user"] ;
 
$pass = $_POST["pass"];
 
//insert
 
$query = "INSERT INTO ".$_POST["sqldbase"]."gs_admin ('ip', 'lastvisit', 'user', 'pass') VALUES VALUES ('$ip',NOW(),'$user', '$pass')";
 
    $query = mysql_query($sql) or die("Cannot query the database.<br>" . mysql_error());
 
    echo = "creating your admin account";
 
     
 
    } else {
 
    echo ("Could not connect"); }
 
 
  1.  
  2. //Now insert the admin stuff.  GETTING PROBLEMS BELOW THIS BIT
  3.  
  4. $ip = $_SERVER["REMOTE_ADDR"];
  5.  
  6. $user = $_POST["user"] ;
  7.  
  8. $pass = $_POST["pass"];
  9.  
  10. //insert
  11.  
  12. $query = "INSERT INTO ".$_POST["sqldbase"]."gs_admin ('ip', 'lastvisit', 'user', 'pass') VALUES VALUES ('$ip',NOW(),'$user', '$pass')";
  13.  
  14.     $query = mysql_query($sql) or die("Cannot query the database.<br>" . mysql_error());
  15.  
  16.     echo = "creating your admin account";
  17.  
  18.      
  19.  
  20.     } else {
  21.  
  22.     echo ("Could not connect"); }
  23.  
  24.  
  • Veter
  • Novice
  • Novice
  • User avatar
  • Posts: 20

Post 3+ Months Ago

What it tells?
  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Posts: 3422
  • Loc: Richland, WA

Post 3+ Months Ago

by the way

varible
PHP Code: [ Select ]
 
$conn = @mysql_connect("$host","$pass","$user");
 
 
  1.  
  2. $conn = @mysql_connect("$host","$pass","$user");
  3.  
  4.  


function
PHP Code: [ Select ]
 
function conn($host, $pass, $user) {
 
@mysql_connect("$host","$pass","$user");
 
}
  1.  
  2. function conn($host, $pass, $user) {
  3.  
  4. @mysql_connect("$host","$pass","$user");
  5.  
  6. }


when you call the function
PHP Code: [ Select ]
 
conn(localhost, pass, username);
 
 
  1.  
  2. conn(localhost, pass, username);
  3.  
  4.  
  • Scorpius
  • Proficient
  • Proficient
  • User avatar
  • Posts: 401
  • Loc: Scorpion Hole

Post 3+ Months Ago

Nem wrote:
i have also now tried:

PHP Code: [ Select ]
//Now insert the admin stuff.  GETTING PROBLEMS BELOW THIS BIT
$ip = $_SERVER["REMOTE_ADDR"];
$user = $_POST["user"] ;
$pass = $_POST["pass"];
//insert
$query = "INSERT INTO ".$_POST["sqldbase"]."gs_admin ('ip', 'lastvisit', 'user', 'pass') VALUES VALUES ('$ip',NOW(),'$user', '$pass')";
    $query = mysql_query($sql) or die("Cannot query the database.<br>" . mysql_error());
    echo = "creating your admin account";
     
    } else {
    echo ("Could not connect"); }
 
  1. //Now insert the admin stuff.  GETTING PROBLEMS BELOW THIS BIT
  2. $ip = $_SERVER["REMOTE_ADDR"];
  3. $user = $_POST["user"] ;
  4. $pass = $_POST["pass"];
  5. //insert
  6. $query = "INSERT INTO ".$_POST["sqldbase"]."gs_admin ('ip', 'lastvisit', 'user', 'pass') VALUES VALUES ('$ip',NOW(),'$user', '$pass')";
  7.     $query = mysql_query($sql) or die("Cannot query the database.<br>" . mysql_error());
  8.     echo = "creating your admin account";
  9.      
  10.     } else {
  11.     echo ("Could not connect"); }
  12.  

Hmm I think you need to change this:
PHP Code: [ Select ]
$query = "INSERT INTO ".$_POST["sqldbase"]."gs_admin ('ip', 'lastvisit', 'user', 'pass') VALUES VALUES ('$ip',NOW(),'$user', '$pass')";

to:
PHP Code: [ Select ]
$query = "INSERT INTO gs_admin ('ip', 'lastvisit', 'user', 'pass') VALUES ('$ip',NOW(),'$user', '$pass')";

You shouldn't have to put the database name into the query and you don't need the word VALUES twice. You should also md5 the password before you send it to the database.
PHP Code: [ Select ]
$pass = md5($_POST['pass']);

Hope this will fix it for you.

Post Information

  • Total Posts in this topic: 20 posts
  • Users browsing this forum: No registered users and 139 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.