更改SQL数据库php脚本

更改SQL数据库php脚本,php,mysql,Php,Mysql,我有这个问题已经3天了:( 我通过phpmyadmin创建了3个数据库 我已经把这3个数据库放到了下拉列表中 我想要的是,每次单击submit时,默认数据库都会根据下拉列表的值进行更改 这就是我找到的密码 <table width="300" border="0" align="center" cellpadding="0" cellspacing="1"> <tr> <td><form name="form2" method="post" > &

我有这个问题已经3天了:( 我通过phpmyadmin创建了3个数据库 我已经把这3个数据库放到了下拉列表中 我想要的是,每次单击submit时,默认数据库都会根据下拉列表的值进行更改

这就是我找到的密码

<table width="300" border="0" align="center" cellpadding="0" cellspacing="1">
<tr>
<td><form name="form2" method="post" >
<table width="100%" border="0" cellspacing="1" cellpadding="3">
<tr>
<td colspan="3"><center><strong>Select Default Database</strong></center></td>
</tr>
<tr>
<td><center><select name="db_used">
<?php 
$host="localhost";
$username="root";
$password="";

$link = mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
$result = mysql_list_dbs( $link );

while( $row = mysql_fetch_object( $result ) ){
echo "<option>$row->Database</option>";
}
?>

</select></center></td>
</tr>
<tr>
<td colspan="3" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</form>
</td>
</tr>
</table>



 <?php
$host="localhost";
$username="root";
$password="";
$db_name = "#this depends on the value of the dropdown list"; 

mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");
?>

选择默认数据库

非常感谢您提供的任何帮助~~

希望这对您有所帮助。您可以这样做:

创建index.php

<table width="300" border="0" align="center" cellpadding="0" cellspacing="1">
<tr>
<td><form name="form2" method="post" action='generate.php'>
<table width="100%" border="0" cellspacing="1" cellpadding="3">
<tr>
<td colspan="3"><center><strong>Select Default Database</strong></center></td>
</tr>
<tr>
<td><center><select name="db_used">
<?php 
$host="localhost";
$username="root";
$password="";

$link = mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
$result = mysql_list_dbs( $link );

while( $row = mysql_fetch_object( $result ) ){
echo "<option>$row->Database</option>";
}
?>

</select></center></td>
</tr>
<tr>
<td colspan="3" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</form>
</td>
</tr>
</table>
<?php
$HOMEDIR = getcwd() . "/";
# /home/virtual/magicplugin.com/var/www/html/
$TEMPLATEDIR = $HOMEDIR . "";

$fName = $TEMPLATEDIR . "dbConnection.php";
    $fh = fopen($fName, "r");   $fContents = fread($fh, filesize($fName));  fclose($fh);
    $fContents = str_replace("__databaseName__",$_REQUEST['db_used'],$fContents);
    $outFile ="index.php";
    $fp = fopen($outFile,"w");
        fwrite($fp,$fContents); fclose($fp);

        echo"Connection generated.Please Check";
        ?>
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1">
<tr>
<td><form name="form2" method="post" action='generate.php'>
<table width="100%" border="0" cellspacing="1" cellpadding="3">
<tr>
<td colspan="3"><center><strong>Select Default Database</strong></center></td>
</tr>
<tr>
<td><center><select name="db_used">
<?php 
$host="localhost";
$username="root";
$password="";

$link = mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
$result = mysql_list_dbs( $link );

while( $row = mysql_fetch_object( $result ) ){
echo "<option>$row->Database</option>";
}
?>

</select></center></td>
</tr>
<tr>
<td colspan="3" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</form>
</td>
</tr>
</table>



 <?php
$host="localhost";
$username="root";
$password="";
$db_name = "__databaseName__"; 

mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");
?>

选择默认数据库
板条箱生成.php

<table width="300" border="0" align="center" cellpadding="0" cellspacing="1">
<tr>
<td><form name="form2" method="post" action='generate.php'>
<table width="100%" border="0" cellspacing="1" cellpadding="3">
<tr>
<td colspan="3"><center><strong>Select Default Database</strong></center></td>
</tr>
<tr>
<td><center><select name="db_used">
<?php 
$host="localhost";
$username="root";
$password="";

$link = mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
$result = mysql_list_dbs( $link );

while( $row = mysql_fetch_object( $result ) ){
echo "<option>$row->Database</option>";
}
?>

</select></center></td>
</tr>
<tr>
<td colspan="3" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</form>
</td>
</tr>
</table>
<?php
$HOMEDIR = getcwd() . "/";
# /home/virtual/magicplugin.com/var/www/html/
$TEMPLATEDIR = $HOMEDIR . "";

$fName = $TEMPLATEDIR . "dbConnection.php";
    $fh = fopen($fName, "r");   $fContents = fread($fh, filesize($fName));  fclose($fh);
    $fContents = str_replace("__databaseName__",$_REQUEST['db_used'],$fContents);
    $outFile ="index.php";
    $fp = fopen($outFile,"w");
        fwrite($fp,$fContents); fclose($fp);

        echo"Connection generated.Please Check";
        ?>
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1">
<tr>
<td><form name="form2" method="post" action='generate.php'>
<table width="100%" border="0" cellspacing="1" cellpadding="3">
<tr>
<td colspan="3"><center><strong>Select Default Database</strong></center></td>
</tr>
<tr>
<td><center><select name="db_used">
<?php 
$host="localhost";
$username="root";
$password="";

$link = mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
$result = mysql_list_dbs( $link );

while( $row = mysql_fetch_object( $result ) ){
echo "<option>$row->Database</option>";
}
?>

</select></center></td>
</tr>
<tr>
<td colspan="3" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</form>
</td>
</tr>
</table>



 <?php
$host="localhost";
$username="root";
$password="";
$db_name = "__databaseName__"; 

mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");
?>

创建dbConnection.php

<table width="300" border="0" align="center" cellpadding="0" cellspacing="1">
<tr>
<td><form name="form2" method="post" action='generate.php'>
<table width="100%" border="0" cellspacing="1" cellpadding="3">
<tr>
<td colspan="3"><center><strong>Select Default Database</strong></center></td>
</tr>
<tr>
<td><center><select name="db_used">
<?php 
$host="localhost";
$username="root";
$password="";

$link = mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
$result = mysql_list_dbs( $link );

while( $row = mysql_fetch_object( $result ) ){
echo "<option>$row->Database</option>";
}
?>

</select></center></td>
</tr>
<tr>
<td colspan="3" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</form>
</td>
</tr>
</table>
<?php
$HOMEDIR = getcwd() . "/";
# /home/virtual/magicplugin.com/var/www/html/
$TEMPLATEDIR = $HOMEDIR . "";

$fName = $TEMPLATEDIR . "dbConnection.php";
    $fh = fopen($fName, "r");   $fContents = fread($fh, filesize($fName));  fclose($fh);
    $fContents = str_replace("__databaseName__",$_REQUEST['db_used'],$fContents);
    $outFile ="index.php";
    $fp = fopen($outFile,"w");
        fwrite($fp,$fContents); fclose($fp);

        echo"Connection generated.Please Check";
        ?>
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1">
<tr>
<td><form name="form2" method="post" action='generate.php'>
<table width="100%" border="0" cellspacing="1" cellpadding="3">
<tr>
<td colspan="3"><center><strong>Select Default Database</strong></center></td>
</tr>
<tr>
<td><center><select name="db_used">
<?php 
$host="localhost";
$username="root";
$password="";

$link = mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
$result = mysql_list_dbs( $link );

while( $row = mysql_fetch_object( $result ) ){
echo "<option>$row->Database</option>";
}
?>

</select></center></td>
</tr>
<tr>
<td colspan="3" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</form>
</td>
</tr>
</table>



 <?php
$host="localhost";
$username="root";
$password="";
$db_name = "__databaseName__"; 

mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");
?>

选择默认数据库

发生了什么?有任何错误消息吗?请在此特别小心。列出的数据库将是连接的用户(root!!)可以访问的所有数据库,包括
mysql、信息\u架构
。创建一个只能访问所需数据库的用户。
中的值位于
$\u POST['db\u used']
强烈建议创建一个可能要连接的数据库的白名单,而不是允许连接到任何可用的数据库(包括
信息\u架构,测试
,其他)@Znarkus mycode不起作用,我尝试创建一个类似$db\u name=“$dropdownlistvalue”的变量@MichaelBerkowski对不起,我还是个新手,我该怎么做?>。如果是这样的话,你可以接受答案。只要按正确的按钮。