什麼是我想要做的是:PHP:可以從數據庫取回數據,但不能插入
插入數據成兩列,並顯示在同一個PHP頁面更新值的表。我能夠獲取數據並顯示它,但無法插入任何數據。請指導我。
文件被命名爲mypage.php
到目前爲止我的代碼:
<?php
$con=mysqli_connect("localhost","test","test","test");
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to mysqli: " . mysqli_connect_error();
}
else
{
if(isset($_POST['url']) && isset($_POST['desc']))
{
$url=$_POST['url'];
$desc=$_POST['desc'];
$sql= "INSERT INTO urldesc ".
"(url,desc) ".
"VALUES ('".$url."', '".$desc."')";
echo $sql;
echo $url;
echo $desc;
mysqli_query($con, $sql);
//mysqli_query($con,"INSERT INTO urldesc (url, desc) VALUES ('". $_POST['url'] ."', '". $_POST['desc'] ."')");
}
}
?>
<html>
<body>
<form action="mypage.php" method='post'>
<input type="text" name='url' />
<input type="text" name='desc' />
<input type="submit" />
</form>
<table border=1>
<th>URL</th>
<th>Description</th>
<?php
$values=mysqli_query($con,"select * from urldesc");
if(! $values)
{
die('Could not get data: ' . mysqli_error());
}
while($row = mysqli_fetch_array($values))
{
echo "<tr><td width='200px'><center>".$row['url']."</center></td><td width='600px'><center>".$row['desc']."</center></td></tr>";
}
mysqli_close($con);
?>
</table>
</body>
</html>
你嘗試過什麼運行得到由「回聲$ SQL」迴盪在phpMyAdmin查詢? – Jinandra