PHP表单未连接到数据库

时间:2018-11-05 15:55:02

标签: php sql-server

我正在构建一个表单来更新/更改/管理页面侧边栏中的链接。到目前为止,除更新功能外,所有基本功能都可以工作。表单加载完毕,当我添加一些调试代码时,所有变量均通过,SQL查询正确构建并在直接将其放入SQL Studio时执行。

问题是更新页面未连接到数据库。是提取数据的同一连接设置(可能是问题的一部分?),所以我不明白为什么它不连接。

下面是代码的通用版本。如果很重要,在右侧栏加载到整个页面上以显示活动促销时,也会调用getpromos.php。还有一些基本的Java正在切换显示哪个促销内容,以便在管理页面上进行编辑。

该页面会加载“促销名称”下拉菜单,以及添加新促销的选项。然后显示所选促销以进行编辑。通常,它会转发回编辑页面,但我已对该部分进行了注释,以进行故障排除。我现在不担心要验证数据或任何东西,我只是在努力使更新功能首先起作用。

adminrightbar.php

    <h4>Promo Manager</h4><br><br><hr><br>

<?php

include ('scripts/getpromos.php');
$promoselect=0;

echo "<div style=\"height:500px; margin-left:0; margin-right:auto;\">";
echo "<div style=\"height:75px; margin-left:0; margin-right:auto;\"><h1> Select which Promo you would like to edit.</h1> <br><select name=\"promomenu\" onchange=\"changepromo(value)\">";
echo "<option>Please Select a Promo</option>";
while ($row = sqlsrv_fetch_array($results))  {

  echo "<option value=\"".@$row['id']."\">".@$row['name']."</option>";

}
echo "<option value=\"99\">Add a New Promo</option>";
echo "</select></div>";
sqlsrv_close($thedb);


include ('scripts/getpromos.php');

while ($row = sqlsrv_fetch_array($results)) {
    echo "<div id=\"promodiv".@$row['id']."\" class=\"promoselectbox\"><h1>".$row['name']."</h1></br>";


    echo "<div style=\"float: left; width:600px; margin-left:0;  margin-top: 25px; margin-right:auto;\">";
    echo "<form action=\"scripts/updatepromos.php?id=".$row['id']."\" method=\"POST\">";
    echo "Promo Name: <input type=\"text\" name=\"name\" id=\"name\" value=\"".@$row['name']."\"  style=\"width: 300px;\"><br><br>";
    echo "Promo Links to: <input type=\"text\" name=\"url\" id=\"url\" value=\"".@$row['url']."\" style=\"width: 300px;\"><br><br>";
    echo "Promo Image: <input type=\"text\" name=\"image\" id=\"image\" value=\"".@$row['image']."\" style=\"width: 300px;\"><br><br>";
    echo "Promo Text: <br><br><textarea type=\"text\" name=\"text\" id=\"text\" rows\"5\" cols=\"75\">".@$row['text']."</textarea><br><br>";
    echo "Enabled: <input type=\"checkbox\" name=\"enable\" id=\"enable\" value=\"".@$row['enable']."\" ";
        if (@$row['enable'] == 1) { echo "checked"; }
    echo "><br><br>";
    echo "<input type=\"submit\" name=\"submit".@$row['id']."\" value=\"Update\">";
    echo "</form></div>";

    echo "<div style=\"width: 250px; float: right; margin-left:auto; margin-right:0; margin-top: 50px;\"><a href=\"".$row['url']."\"><img src=\"images/promo/".$row['image']."\"></a><br>".$row['text']."<br>";
    echo "</div></div>";
}

    echo "<div id=\"promodiv99\" class=\"promoselectbox\"><h1>Add New Promo</h1></br>";
    echo "<div style=\"float: left; width:600px; margin-left:0;  margin-top: 25px; margin-right:auto;\">";
    echo "<form action=\"scripts/updatepromos.php?id=99\" method=\"POST\">";
    echo "Promo Name: <input type=\"text\" name=\"name\" id=\"name\"  style=\"width: 300px;\"><br><br>";
    echo "Promo Links to: <input type=\"text\" name=\"url\" id=\"url\"  style=\"width: 300px;\"><br><br>";
    echo "Promo Image: <input type=\"text\" name=\"image\" id=\"image\"  style=\"width: 300px;\"><br><br>";
    echo "Promo Text: <br><br><textarea type=\"text\" name=\"text\" id=\"text\" rows\"5\" cols=\"75\"></textarea><br><br>";
    echo "Enabled: <input type=\"checkbox\" name=\"enable\" id=\"enable\"  ";
    echo "><br><br>";
    echo "<input type=\"submit\" name=\"submit\" value=\"Update\">";
    echo "</form></div>";

    echo "<div style=\"width: 250px; float: right; margin-left:auto; margin-right:0; margin-top: 50px;\"><a href=\"".$row['url']."\"><img src=\"images/promo/".$row['image']."\"></a><br>".$row['text']."<br>";
    echo "</div></div>";

echo "</div>";
sqlsrv_close($thedb);
?>


<script type="text/javascript">

function changepromo(select){
    $('.promoselectbox').hide();
    $('#promodiv'+select).show();
}
</script>

getpromos.php

<?php 

$connectionInfo = array( "Database"=>"userdb", "UID"=>$_SESSION['sqlsrvuser'], "PWD"=>$_SESSION['sqlsrvpass']);
$thedb = sqlsrv_connect( $_SESSION['sqldb'], $connectionInfo);
if ($thedb->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 


$results = sqlsrv_query ($thedb, "SELECT * FROM userdb.db ORDER BY ordering");

?>

updatepromos.php

<?php 

$connectionInfo = array( "Database"=>"userdb", "UID"=>$_SESSION['sqlsrvuser'], "PWD"=>$_SESSION['sqlsrvpass']);
$thedb = sqlsrv_connect( $_SESSION['sqldb'], $connectionInfo);
if( $thedb ) {
     echo "Connection established.<br />";
}else{
     echo "Connection could not be established.<br />";
     die( print_r( sqlsrv_errors(), true));
}


echo "<br><br>Updating....";

if ($_GET['id'] == 99) {
    echo "Add a new Promo";
    }
else {
    $sql = "UPDATE thedb.db SET name='".$_POST['name']."', enable='".$_POST['enable']."' WHERE id='".$_GET['id']."'";
    echo $sql;
    $result = sqlsrv_query($thedb, $sql);
    if( $result === false ) {
     die( print_r( sqlsrv_errors(), true));  }
    }

sqlsrv_close($thedb);
# header("Location: {$_SERVER["HTTP_REFERER"]}");
?>

编辑:发生的特定错误。

Array ( [0] => Array ( [0] => 08001 [SQLSTATE] => 08001 [1] => 2 [code] => 2 [2] => [Microsoft][SQL Server Native Client 11.0]Named Pipes Provider: Could not open a connection to SQL Server [2]. [message] => [Microsoft][SQL Server Native Client 11.0]Named Pipes Provider: Could not open a connection to SQL Server [2]. ) [1] => Array ( [0] => HYT00 [SQLSTATE] => HYT00 [1] => 0 [code] => 0 [2] => [Microsoft][SQL Server Native Client 11.0]Login timeout expired [message] => [Microsoft][SQL Server Native Client 11.0]Login timeout expired ) [2] => Array ( [0] => 08001 [SQLSTATE] => 08001 [1] => 2 [code] => 2 [2] => [Microsoft][SQL Server Native Client 11.0]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online. [message] => [Microsoft][SQL Server Native Client 11.0]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online. ) ) 

1 个答案:

答案 0 :(得分:0)

我发现了问题。

所有页面都在页面,索引,标题,右栏等小框架内加载。

被称为进行更新的页面不会,它不会独立加载,然后回滚到上一页。因此它没有收到带有数据库用户名和密码的全局变量。