使用 HTML 表单和 PHP 更新 MySQL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18994960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 18:37:55  来源:igfitidea点击:

Update MySQL using HTML Form and PHP

phphtmlmysqlformssql-update

提问by user2052849

I've been looking through dozens of threads on here about this but have yet to find a solution.

我已经在这里浏览了数十个线程,但尚未找到解决方案。

I've created a form that is supposed to show database table contents in input boxes, and when the content of the input boxes are changed and submitted the database is supposed to update.

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

<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>

The forms show the content of the database fine, but upon updating I get this message:

表单很好地显示了数据库的内容,但在更新时我收到以下消息:

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

您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以获取在第 1 行的“1”附近使用的正确语法

I appreciate any help I can get on this.

我很感激我能得到的任何帮助。

回答by Undefined Behavior

Use mysqli instead of mysql, and you need to pass the database name or schema:

使用 mysqli 而不是 mysql,并且需要传递数据库名称或模式:

before:

前:

$conn = mysql_connect($dbhost, $dbuser, $dbpass);

after:

后:

$conn = mysql_connect($dbhost, $dbuser, $dbpass, $myDBname);

回答by Shafeeque

Update query may have some issues

更新查询可能有一些问题

$query = "UPDATE anstalld SET mandag = '$mandag', tisdag = '$tisdag', onsdag = '$onsdag', torsdag = '$torsdag', fredag = '$fredag' WHERE namn = '$namn' ";
echo $query;

Please make sure that, your variable not having values with qoutes ( ' ), May be the query is breaking somewhere.

请确保您的变量没有带有 qoutes ( ' ) 的值,可能是查询在某处中断。

echo the query and try to execute in phpmyadmin itself. Then you can find the issues.

回显查询并尝试在 phpmyadmin 本身中执行。然后你就可以找到问题了。

回答by Konsole

You have already executed your query here

您已经在此处执行了您的查询

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

So this line has the problem

所以这条线有问题

$retval = mysql_query( $sql, $conn ); //$sql is not a query its a result set here

Try something like this:

尝试这样的事情:

$sql = "UPDATE anstalld SET mandag = '$mandag', tisdag = '$tisdag', onsdag = '$onsdag', torsdag = '$torsdag', fredag = '$fredag' WHERE namn = '$namn'";    
$retval = mysql_query( $sql, $conn ); //execute your query

As a sidenote: MySQL_* extension is deprecated use MySQLi_* or PDO instead.

作为旁注:不推荐使用 MySQL_* 扩展,而是使用 MySQLi_* 或 PDO。

回答by user3510431

Your sql is incorrect.

你的sql不正确。

$sql = mysql_query("UPDATE anstalld....

only

只要

$sql = "UPDATE anstalld...

回答by Neo

First of all use mysqli_connect($dbhost,$dbuser,$dbpass,$dbname)

首先使用 mysqli_connect($dbhost,$dbuser,$dbpass,$dbname)

Second - put mysqli_everywhere instead of mysql_

第二 -mysqli_无处不在,而不是mysql_

Third - use this $sql = "UPDATE anstalld SET mandag = '.$mandag.', tisdag = '.$tisdag.', onsdag = '.$onsdag.', torsdag = '.$torsdag.', fredag = '.$fredag.' WHERE namn = '.$namn.'";
$retval = mysqli_query( $conn, $sql ); //execute your query

第三 - 使用这个 $sql = "UPDATE anstalld SET mandag = '.$mandag.', tisdag = '.$tisdag.', onsdag = '.$onsdag.', torsdag = '.$torsdag.', fredag = '.$fredag.' WHERE namn = '.$namn.'";
$retval = mysqli_query( $conn, $sql ); //execute your query

if Your data is being updated in your database but not in your table its because when you will click on update button, the request is made to the same file. It first selects the data from the database when it is not updated prints it in the table and then update it according to the flow. If you have to update it as you click on update button then put this section

如果您的数据正在您的数据库中更新但不在您的表中,因为当您单击更新按钮时,请求将发送到同一个文件。它首先从数据库中选择未更新时的数据将其打印在表中,然后根据流程进行更新。如果您必须在单击更新按钮时更新它,请放置此部分

<?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";

}


?>`

after connecting with database.

连接数据库后。

回答by hassan gul

you have error in your sql syntax.

你的 sql 语法有错误。

please use this query and checkout.

请使用此查询和结帐。

$query = mysql_query("UPDATE `anstalld` SET mandag = '$mandag', tisdag = '$tisdag', onsdag = '$onsdag', torsdag = '$torsdag', fredag = '$fredag' WHERE namn = '$namn' ");

回答by Nirav Patel

Try it this way. Put the table name in quotes (``). beside put variable '".$xyz."'.

试试这个方法。将表名放在引号 (``) 中。旁边放变量'".$xyz."'。

So your sql query will be like:

所以你的 sql 查询将是这样的:

$sql = mysql_query("UPDATE `anstalld` SET mandag = "'.$mandag.'" WHERE namn =".$name)or die(mysql_error());