使用HTML表单和PHP更新MySQL

前端之家收集整理的这篇文章主要介绍了使用HTML表单和PHP更新MySQL前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我一直在寻找关于这方面的几十个主题,但尚未找到解决方案.

我创建了一个应该在输入框中显示数据库内容的表单,当输入框的内容被更改并提交时,数据库应该更新.

<html>
<head>
</head>
<body>

<?PHP

$dbhost = '****';
$dbuser = '****';
$dbpass = '****';
$conn = MysqL_connect($dbhost,$dbuser,$dbpass);
if(! $conn )
{
  die('Could not connect: ' . MysqL_error());
}
MysqL_select_db('****');

$query = "SELECT * FROM anstalld";
$result = MysqL_query($query) or die(MysqL_error());

?>


<form method="post" action="<?PHP $_PHP_SELF ?>">
<table width="400" border="0" cellspacing="1" cellpadding="2">
<tr>

<?PHP 

while($row = MysqL_fetch_array($result))
            {

$namn = $row['namn'];
$mandag = $row['mandag'];
$tisdag = $row['tisdag'];
$onsdag = $row['onsdag'];
$torsdag = $row['torsdag'];
$fredag = $row['fredag'];
?>


<td width="100"></td>
<td><?=$namn?></td>
</tr>
<tr>
<td width="100">Mandag</td>
<td><input name="mandagid" type="text" value="<?=$mandag?>"></td>
</tr>
<tr>
<td width="100">Tisdag</td>
<td><input name="tisdagid" type="text" value="<?=$tisdag?>"></td>
</tr>
<tr>
<td width="100">Onsdag</td>
<td><input name="onsdagid" type="text" value="<?=$onsdag?>"></td>
</tr>
<tr>
<td width="100">Torsdag</td>
<td><input name="torsdagid" type="text" value="<?=$torsdag?>"></td>
</tr>
<tr>
<td width="100">Fredag</td>
<td><input name="fredagid" type="text" value="<?=$fredag?>"></td>
</tr>
<?PHP } ?>
<tr>
<td width="100"> </td>
<td> </td>
</tr>
<tr>
<td width="100"> </td>
<td>
<input name="update" type="submit" id="update" value="Update">
</td>
</tr>
</table>
</form>



<?PHP

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

$namn = $_POST['namnid'];
$mandag = $_POST['mandagid'];
$tisdag = $_POST['tisdagid'];
$onsdag = $_POST['onsdagid'];
$torsdag = $_POST['torsdagid'];
$fredag = $_POST['fredagid'];

$sql = MysqL_query("UPDATE anstalld SET mandag = '$mandag',tisdag = '$tisdag',onsdag = '$onsdag',torsdag = '$torsdag',fredag = '$fredag' WHERE namn = '$namn'");

$retval = MysqL_query( $sql,$conn );
if(! $retval )
{
  die('Could not update data: ' . MysqL_error());
}
echo "Updated data successfully\n";

}


?>
</body>
</html>

表单显示数据库内容很好,但更新后我收到此消息:

You have an error in your sql Syntax; check the manual that
corresponds to your MysqL server version for the right Syntax to use
near ‘1’ at line 1

我感谢任何帮助.

解决方法

使用MysqLi而不是MysqL,您需要传递数据库名称或模式:

之前:

$conn = MysqL_connect($dbhost,$dbpass);

后:

$conn = MysqL_connect($dbhost,$dbpass,$myDBname);

猜你在找的HTML相关文章