2014-12-03 67 views
-2

我是新來的PHP所以請耐心解釋給我,就像我五PHP更新新的SQL內容

我有一個頁面,它顯示了一個表從SQL的內容,但每當我更新與PHP我有強制更新我的瀏覽器,使其顯示錶的新內容。我得到了同樣的問題,因爲這傢伙,但我不明白他的解決方案:

Php won't update to show new sql content

這是我更新的表中的PHP代碼:

<!-- html dok --> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html> 
    <head> 
    <title></title> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
    </head> 
    <body> 

    </body> 
</html> 

<?php 
$server = "localhost"; 
$brugernavn = ""; 
$kode = ""; 
$db = ""; 

mysql_connect($server , $brugernavn , $kode) or die(mysql_error()); 

echo "Forbundet til mysql server<br/>"; 

mysql_select_db($db)or die(mysql_error()); 

echo "Forbundet til databasen<br/><br/>"; 

$data = mysql_query("SELECT * FROM nyheder") or die(mysql_error()); 

while ($info = mysql_fetch_array($data)) 
    { 
echo "Nyhed: " . $info['nyhed']. "<br/><br/>"; 
    } 

// Update tabel 

    if (isset($_POST['update'])) { 

     $nyhed = $_POST['nyhed']; 

     $tabeldata = "UPDATE nyheder SET nyhed = '$nyhed' WHERE ID ='1'"; 
     $resultat = mysql_query($tabeldata); 
if($resultat) { 
    echo "Din nyhed blev opdateret" . "<a href=get.php>Videre</a>"; 
    } 
else { 
    echo "FEJL"; 
} 
} 
else { 
    echo "Ingen nyheder er blevet opdateret"; 

} 
mysql_close(); 
?> 

,這是我的顯示代碼表內容:

<!DOCTYPE html> 
<html> 
    <head> 
    <title></title> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
    </head> 
<body> 

<?php 
$servername = ""; 
$username = ""; 
$password = ""; 
$dbname = ""; 

// Create connection 
$conn = new mysqli($servername, $username, $password, $dbname); 
// Check connection 
if ($conn->connect_error) { 
    die("Connection failed: " . $conn->connect_error); 
} 

$sql = "SELECT nyhed FROM nyheder WHERE ID='1'"; 
$result = $conn->query($sql); 
$link_address = 'form.php'; 

if ($result->num_rows > 0) { 
    // output data of each row 
    while($row = $result->fetch_assoc()) { 
     echo $row["nyhed"] . "<br><br>"; 
     echo "<a href='$link_address'>Opdater</a>"; 
    } 
} else { 
    echo "0 results"; 
} 

$conn->close(); 
?> 

</body> 
</html> 

非常感謝你提前! :)

+1

你得表現出你的更新代碼,你如何去「回」到更新的頁面。您顯示的小片段沒有用處。 – 2014-12-03 16:51:03

+0

這就是說,如果你使用mysqli(例子)請注意,num_rows不能正常工作(如php.net所示),如果你使用mysql_ *而不是忽略我的註釋 – 2014-12-03 16:53:58

+1

問:爲什麼你在一箇中使用mysql_'函數一段代碼,然後跳到另一個'mysqli_'? – 2014-12-03 16:57:39

回答

0

如果你想使用下面

if($resultat) { 
    echo "Din nyhed blev opdateret" . "<a href=get.php>Videre</a>"; 
    } 

Php won't update to show new sql content的回答改變

if($resultat) { 
    echo "Din nyhed blev opdateret" . "<a href=get.php?time=".time().">Videre</a>"; 
    } 

這將增加當前時間get.php作爲查詢字符串(會看起來像得到。 PHP?時間= 1417626725)和每次點擊鏈接瀏覽器時再次看到get.php新網址和抓取網頁無負載它從緩存中(如果它緩存)

+0

非常感謝你我愛你我不知道在哪裏查詢字符串去!。 – 2014-12-03 17:32:57