Jump to content

[SOLVED] php & mysql


zero_ZX

Recommended Posts

Hi all :D

So i got a little question (im still a newb but im learning, thanks to you great guys (or girls) out there answering those questions :D)

I got the following form: (install.php)

DBuser:

DBpass:

Database:

DBhost: (localhost)

"submit"

 

I would really like that when i hit this submit button the db details gets entered into a config file..

Well but that's not the only thing i need done, further more i need the install.php to import a dbsetup.sql into the sql database specefied above...

I'm not asking you to code this :D (guess it would be wrong section then lol) but some help/hints on where to start/where to look or any tuts at all would be so awesome!

Link to comment
https://forums.phpfreaks.com/topic/145238-solved-php-mysql/
Share on other sites

Sorry for double posting, wanted to make sure that i got all's attention :)

 

When i want to edit my file i do the following:

<?php
$filename = '../conf_global.php';
$somecontent = "
<?php
$INFO['sql_driver']			=	'mysql';
$INFO['sql_host']			=	'$host';
$INFO['sql_database']			=	'$db';
$INFO['sql_user']			=	'$user';
$INFO['sql_pass']			=	'$pass';
$INFO['sql_tbl_prefix']			=	'ibf_';
$INFO['sql_debug']			=	'1';
$INFO['board_start']			=	'1229859937'; //this will get used later on
$INFO['installed']			=	'1';
$INFO['php_ext']			=	'php';
$INFO['safe_mode']			=	'0';
$INFO['board_url']			=	'http://your-url.com'; //this will get edited later on
$INFO['banned_group']			=	'5';
$INFO['admin_group']			=	'4';
$INFO['guest_group']			=	'2';
$INFO['member_group']			=	'3';
$INFO['auth_group']			=	'1';
$INFO['mysql_tbl_type']			=	'MyISAM';

?>";

// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {

    // In our example we're opening $filename in append mode.
    // The file pointer is at the bottom of the file hence
    // that's where $somecontent will go when we fwrite() it.
    if (!$handle = fopen($filename, 'a')) {
         echo "Cannot open file ($filename)";
         exit;
    }

    // Write $somecontent to our opened file.
    if (fwrite($handle, $somecontent) === FALSE) {
        echo "Cannot write to file ($filename)";
        exit;
    }

    echo "Success, wrote ($somecontent) to file ($filename)";

    fclose($handle);

} else {
    echo "The file $filename is not writable";
}
?>

 

Now i have a previusly form:

      <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="dbdata">
        <table width="100%">
          <tr><td>Hostname:</td><td><input class="text" name="hostname" type="text" size="20" value="localhost" /></td></tr>
          <tr><td>Username:</td><td> <input class="text" name="username" type="text" size="20" value="" /></td></tr>
          <tr><td>Database:</td><td> <input class="text" name="database" type="text" size="20" value="" /></td></tr>
          <tr><td>Password:</td><td> <input class="text" name="password" type="password" size="20" value="" /></td></tr>
          <tr><td align="center" colspan="2"><br/><input class="text" type="submit" name="submitBtn" value="Install" /></td></tr>
        </table>  
      </form>

Now i assign the entered value from the forms:

        $host = isset($_POST['hostname']) ? $_POST['hostname'] : '';
        $user = isset($_POST['username']) ? $_POST['username'] : '';
        $pass = isset($_POST['password']) ? $_POST['password'] : '';
	$db = isset($_POST['database']) ? $_POST['database'] : '';

 

My question is then, can i enter variables in the part where my file gets edited, or would it directly write the variable? if so, do you got another solution for me?

 

Thank you so much :D

Link to comment
https://forums.phpfreaks.com/topic/145238-solved-php-mysql/#findComment-762618
Share on other sites

I dunno where the edit button went.. well

I tried to insert see what happened and i got the following error:

Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in C:\xampp\htdocs\ipbfm\lhinstall\index.php on line 58

 

Here's my code:

<?php
/*------------------------------------------------------------------------------------------
     Micro DB Installer

     ©PhpToys 2007
     http://www.phptoys.com

     Released under the terms and conditions of the
     GNU General Public License (http://gnu.org).

     $Revision: 1.0 $
     $Date: 2007/09/05 $
     $Author: PhpToys $
     
     USAGE:
          Just copy the files to your webserver with a valid sql file.
          You can configure the sql file name by changing the $sqlFileToExecute
          variable.
--------------------------------------------------------------------------------------------*/
$sqlErrorText = '';
$sqlErrorCode = 0;
$sqlStmt      = '';
$sqlFileToExecute = 'test.sql';

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
<html>
<head>
   <title>Micro DB Installer</title>
   <link href="style/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <div id="main">
      <div class="caption">MICRO DB INSTALLER</div>
      <div id="icon"> </div>
      <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="dbdata">
        <table width="100%">
          <tr><td>Hostname:</td><td><input class="text" name="hostname" type="text" size="20" value="localhost" /></td></tr>
          <tr><td>Username:</td><td> <input class="text" name="username" type="text" size="20" value="" /></td></tr>
          <tr><td>Database:</td><td> <input class="text" name="database" type="text" size="20" value="" /></td></tr>
          <tr><td>Password:</td><td> <input class="text" name="password" type="password" size="20" value="" /></td></tr>
          <tr><td align="center" colspan="2"><br/><input class="text" type="submit" name="submitBtn" value="Install" /></td></tr>
        </table>  
      </form>
<?php    
    if (isset($_POST['submitBtn'])){
        $host = isset($_POST['hostname']) ? $_POST['hostname'] : '';
        $user = isset($_POST['username']) ? $_POST['username'] : '';
        $pass = isset($_POST['password']) ? $_POST['password'] : '';
	$db = isset($_POST['database']) ? $_POST['database'] : '';

//Update ../con_global.php after you press the submit button.

$filename = '../conf_global.php';
$somecontent = "
<php
$INFO['sql_driver']			=	'mysql';   [bold][color=blue] //line 58[/bold][/color]
$INFO['sql_host']			=	'$host';
$INFO['sql_database']			=	'$db';
$INFO['sql_user']			=	'$user';
$INFO['sql_pass']			=	'$pass';
$INFO['sql_tbl_prefix']			=	'ibf_';
$INFO['sql_debug']			=	'1';
$INFO['board_start']			=	'1229859937';
$INFO['installed']			=	'1';
$INFO['php_ext']			=	'php';
$INFO['safe_mode']			=	'0';
$INFO['board_url']			=	'http://www.lunarhosting.org';
$INFO['banned_group']			=	'5';
$INFO['admin_group']			=	'4';
$INFO['guest_group']			=	'2';
$INFO['member_group']			=	'3';
$INFO['auth_group']			=	'1';
$INFO['mysql_tbl_type']			=	'MyISAM';

?>";

// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {

    // In our example we're opening $filename in append mode.
    // The file pointer is at the bottom of the file hence
    // that's where $somecontent will go when we fwrite() it.
    if (!$handle = fopen($filename, 'a')) {
         echo "Cannot open file ($filename)";
         exit;
    }

    // Write $somecontent to our opened file.
    if (fwrite($handle, $somecontent) === FALSE) {
        echo "Cannot write to file ($filename)";
        exit;
    }

    echo "Success, wrote ($somecontent) to file ($filename)";

    fclose($handle);

} else {
    echo "The file $filename is not writable";
}


        
        $con = mysql_connect($host,$user,$pass);
	mysql_select_db("$db", $con);
        if ($con !== false){
           // Load and explode the sql file
           $f = fopen($sqlFileToExecute,"r+");
           $sqlFile = fread($f,filesize($sqlFileToExecute));
           $sqlArray = explode(';',$sqlFile);
           
           //Process the sql file by statements
           foreach ($sqlArray as $stmt) {
              if (strlen($stmt)>3){
           	     $result = mysql_query($stmt);
           	     if (!$result){
           	        $sqlErrorCode = mysql_errno();
           	        $sqlErrorText = mysql_error();
           	        $sqlStmt      = $stmt;
           	        break;
           	     }
           	  }
           }
        }

?>
      <div class="caption">RESULT:</div>
      <div id="icon2"> </div>
      <div id="result">
        <table width="100%">
<?php
        if ($sqlErrorCode == 0){
           echo "<tr><td>Installation was finished succesfully!</td></tr>";
        } else {
           echo "<tr><td>An error occured during installation!</td></tr>";
           echo "<tr><td>Error code: $sqlErrorCode</td></tr>";
           echo "<tr><td>Error text: $sqlErrorText</td></tr>";
           echo "<tr><td>Statement:<br/> $sqlStmt</td></tr>";
        }
?>
        </table>
     </div>
<?php            
    }
?>
<div id="source">Micro DB Installer 1.0</div>
    </div>
</body>   

Link to comment
https://forums.phpfreaks.com/topic/145238-solved-php-mysql/#findComment-762622
Share on other sites

PHP tries to find all variables in double quotes and evaluate them to current values. So when you do

 

$somecontent = "$INFO['sql_user'] = '$user';";

 

It looks for variable called $INFO and for variable called $user and wants to put their values into a string. Not exactly what you want, is it.

The actual error is because, when you use arrays in double quotes, you have to enclose them in {}

 

See here http://www.php.net/manual/en/language.types.string.php for more examples.

 

Now, you should use some placeholder instead of $INFO, and once you have created this string, replace the placeholder with $INFO

 

eg.

 

$somecontent = "%INFO%['sql_user'] = '$user';";
$somecontent = str_replace('%INFO%','$INFO',$somecontent);

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/145238-solved-php-mysql/#findComment-762656
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.