为什么我不能正确回显所有变量?

时间:2014-08-15 01:46:56

标签: php mysql sql mysqli echo

已编辑,请向下滚动
我试图显示3个变量,这些变量由存储在SQL数据库中的数据组成。但是,只有第一个成功回显(topLeftUrl)。值得注意的是,相同的PHP文件也从输入(也在同一PHP文件中)接收数据并将其存储在同一SQL数据库中。此代码是出于测试目的而编写的,可能并不完全安全。

//Connect
$con = mysqli_connect ("localhost","noneedtoknow","noneedtoknow","noneedtoknow");
if (mysqli_connect_errno())
{
    echo "Error: ", mysql_connect_error(), "<br>";
    die ();
}

//Store input in SQL database
$result = mysqli_query ($con, "SELECT * FROM edit");
$message = stripslashes ($_POST ['message']);
if ($message !== '') {
    mysqli_query ($con, "UPDATE edit SET cont='$message' WHERE id='message'"); }
$topLeftNew = ($_POST ['topLeftUrl']);
if ($topLeftNew !== '') {
    mysqli_query ($con, "UPDATE edit SET cont='$topLeftNew' WHERE id='topLeft'"); }
$topRightNew = ($_POST ['topRightUrl']);
if ($topRightNew !== '') {
    mysqli_query ($con, "UPDATE edit SET cont='$topRightNew' WHERE id='topRight'"); }

//First echo
while ($row = mysqli_fetch_array ($result))
{
    if ($row["id"] == "topLeft" && $done2 == 0)
    {
        $topLeftUrl = $row["cont"];
    }
}
echo "<input type=\"text\" name=\"topLeftUrl\" value=\"" . $topLeftUrl . "\">";

//Second echo
while ($row = mysqli_fetch_array ($result))
{
    if ($row["id"] == "topRight" && $done3 == 0)
    {
        $topRightUrl = $row["cont"];
    }
}
echo "<input type=\"text\" name=\"topRightUrl\" value=\"" . $topRightUrl . "\">";

//Third echo
while ($row = mysqli_fetch_array ($result))
{
    if ($row["id"] == "message" && $done == 0)
    {
        echo $row["cont"];
    }
}

修改 我更新了代码,问题似乎已经改变了。出于某种原因,echo $messageCont;显示旧值cont WHERE id='message'。但是,数据库本身已成功更新,一旦刷新页面/重新提交表单,我就会看到cont的新值。为什么在表单提交后我没有立即看到cont的当前值?这是新代码:

/* Before <!DOCTYPE html> */
//Connect
$con = mysqli_connect ("localhost","noneedtoknow","noneedtoknow","noneedtoknow");
if (mysqli_connect_errno())
{
    echo "Error: ", mysql_connect_error(), "<br>";
    die ();
}

//Query and update
$result = mysqli_query ($con, "SELECT * FROM edit");
$message = stripslashes ($_POST ['message']);
if ($message !== '') {
    mysqli_query ($con, "UPDATE edit SET cont='$message' WHERE id='message'"); }
$topLeftNew = ($_POST ['topLeftUrl']);
if ($topLeftNew !== '') {
    mysqli_query ($con, "UPDATE edit SET cont='$topLeftNew' WHERE id='topLeft'"); }
$topRightNew = ($_POST ['topRightUrl']);
if ($topRightNew !== '') {
    mysqli_query ($con, "UPDATE edit SET cont='$topRightNew' WHERE id='topRight'"); }

//Query again and read
$done0 = 0;
$done1 = 0;
$done2 = 0;
mysqli_data_seek ($result, 0);
while ($row = mysqli_fetch_array ($result))
{
    if ($row["id"] == "topLeft" && $done0 == 0)
    {
        $topLeftUrl = $row["cont"];
        $done0 = 1;
    }
    else if ($row["id"] == "topRight" && $done1 == 0)
    {
        $topRightUrl = $row["cont"];
        $done1 = 1;
    }
    else if ($row["id"] == "message" && $done2 == 0)
    {
        $messageCont = $row["cont"];
        $done2 = 1;
    }
    else null;
}

/* After <!DOCTYPE html> */
/* Form code was omitted as it works perfectly. It is in this same file, though. */
echo "<input type=\"text\" name=\"topLeftUrl\" value=\"" . $topLeftUrl . "\">";
echo "<input type=\"text\" name=\"topRightUrl\" value=\"" . $topRightUrl . "\">";
echo $messageCont;

感谢任何帮助。
修改:我只需将mysqli_data_seek ()替换为以$result开头的行(剪切/粘贴)。谢谢。

1 个答案:

答案 0 :(得分:2)

我在我的网站上遇到了同样的问题....你在同一个查询上运行了多个mysql_fetch_array()($ result)...我认为这可以在我的网站上运行但是这个都失败了除了第一个6 while循环在我的网站上都引用了相同的查询(对不起,但我不记得我的error_log中的确切错误消息)。尝试将3 while循环缩小为1循环,如下所示:

while ($row = mysqli_fetch_array ($result)) {
    if ($row["id"] == "topLeft" && $done2 == 0) {
        $topLeftUrl = $row["cont"];
    } else if ($row["id"] == "topRight" && $done3 == 0) {
        $topRightUrl = $row["cont"];
    } else if ($row["id"] == "message" && $done == 0) {
        echo $row["cont"];
    } else null;
}
echo "<input type=\"text\" name=\"topRightUrl\" value=\"" . $topRightUrl . "\">";
echo "<input type=\"text\" name=\"topLeftUrl\" value=\"" . $topLeftUrl . "\">";