以 PHP 形式编辑 MYSQL 行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6913492/
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-26 01:36:01  来源:igfitidea点击:

Edit MYSQL Row in PHP form

phpmysqlformsrowedit

提问by Alex Wiseman

I have done this before and for some reason I just can't get it to work this time! I'm pulling my hair out over this! Because there is no errors, it just wont update the database.

我以前做过这个,但出于某种原因,这次我无法让它工作!我要把我的头发拉出来!因为没有错误,它只是不会更新数据库。

Basically I Have a Table with student data in....

基本上我有一个包含学生数据的表格......

ID | IMGNU | Firstname | Surname | FBID

身 | IMGNU | 名字 | 姓氏 | 联邦调查局

Let's use row 233 for an example.

我们以第 233 行为例。

I can view a specific row by going to view.php?ID=233

我可以通过转到 view.php?ID=233 查看特定行

Then that works, but now i want to be able to go to edit.php?ID=233 and it should load a form, that already has the info from row 233. I should then be able to edit the data in the fields and submit the form, which would change the information in the database.

然后就可以了,但现在我希望能够转到 edit.php?ID=233 并且它应该加载一个表单,该表单已经包含第 233 行的信息。然后我应该能够编辑字段中的数据和提交表单,这将更改数据库中的信息。

Here is what i have already.

这是我已经拥有的。

edit.php

编辑.php

<?php
mysql_connect('localhost', 'admin', 'passw0rd') or die(mysql_error());
echo "Tick <p>";
mysql_select_db("students") or die(mysql_error());
echo "Tick"; 

$UID = $_GET['ID'];

$query = mysql_query("SELECT * FROM stokesley_students WHERE id = '$UID'")
or die(mysql_error());  

while($row = mysql_fetch_array($query)) {
echo "";

$firstname = $row['firstname'];
$surname = $row['surname'];
$FBID = $row['FBID'];
$IMGNU = $row['IMGNU'];


};

?>

<form action="update.php?ID=<?php echo "$UID" ?>" method="post">

IMGNU: <input type="text" name="ud_img" value="<?php echo "$IMGNU" ?>"><br>

First Name: <input type="text" name="ud_firstname" value="<?php echo "$firstname" ?>"><br>

Last Name: <input type="text" name="ud_surname" value="<?php echo "$surname" ?>"><br>

FB: <input type="text" name="ud_FBID" value="<?php echo "$FBID" ?>"><br>

<input type="Submit">
</form>

And here is update.php

这是 update.php

<

?php

$ud_ID = $_GET["ID"];

$ud_firstname = $_POST["ud_firstname"];
$ud_surname = $_POST["ud_surname"];
$ud_FBID = $_POST["ud_FBID"];
$ud_IMG = $_POST["ud_IMG"];

mysql_connect('localhost', 'admin', 'passw0rd') or die(mysql_error());
echo "MySQL Connection Established! <br>";

mysql_select_db("students") or die(mysql_error());
echo "Database Found! <br>";


$query="UPDATE * stokesley_students SET firstname = '$ud_firstname', surname = '$ud_surname', 
FBID = '$ud_FBID' WHERE ID ='$ud_IMG'";

mysql_query($query);

echo "<p>Record Updated<p>";

mysql_close();
?>

Any ideas would be much appreciated, maby im just missing something stupid?

任何想法将不胜感激,也许我只是错过了一些愚蠢的东西?

Thanks Alex

谢谢亚历克斯

回答by Lawrence Cherone

edit.php - with some changes

edit.php - 有一些变化

<?php
mysql_connect('localhost', 'admin', 'passw0rd') or die(mysql_error());
mysql_select_db("students") or die(mysql_error());

$UID = (int)$_GET['ID'];
$query = mysql_query("SELECT * FROM stokesley_students WHERE id = '$UID'") or die(mysql_error());

if(mysql_num_rows($query)>=1){
    while($row = mysql_fetch_array($query)) {
        $firstname = $row['firstname'];
        $surname = $row['surname'];
        $FBID = $row['FBID'];
        $IMGNU = $row['IMGNU'];
    }
?>
<form action="update.php" method="post">
<input type="hidden" name="ID" value="<?=$UID;?>">
IMGNU: <input type="text" name="ud_img" value="<?=$IMGNU;?>"><br>
First Name: <input type="text" name="ud_firstname" value="<?=$firstname?>"><br>
Last Name: <input type="text" name="ud_surname" value="<?=$surname?>"><br>
FB: <input type="text" name="ud_FBID" value="<?=$FBID?>"><br>
<input type="Submit">
</form>
<?php
}else{
    echo 'No entry found. <a href="javascript:history.back()">Go back</a>';
}
?>

update.php (Apart from the asterisk, Your query was also matching IDwith the $ud_IMGvariable)

update.php(除了星号,您的查询也ID$ud_IMG变量匹配)

    <?php
    mysql_connect('localhost', 'admin', 'passw0rd') or die(mysql_error());
    mysql_select_db("students") or die(mysql_error());

    $ud_ID = (int)$_POST["ID"];

    $ud_firstname = mysql_real_escape_string($_POST["ud_firstname"]);
    $ud_surname = mysql_real_escape_string($_POST["ud_surname"]);
    $ud_FBID = mysql_real_escape_string($_POST["ud_FBID"]);
    $ud_IMG = mysql_real_escape_string($_POST["ud_IMG"]);


    $query="UPDATE stokesley_students
            SET firstname = '$ud_firstname', surname = '$ud_surname', FBID = '$ud_FBID' 
            WHERE ID='$ud_ID'";


mysql_query($query)or die(mysql_error());
if(mysql_affected_rows()>=1){
    echo "<p>($ud_ID) Record Updated<p>";
}else{
    echo "<p>($ud_ID) Not Updated<p>";
}
?>

回答by Jacob

"UPDATE * stokesley_students SET firstname = '$ud_firstname', surname = '$ud_surname', 
FBID = '$ud_FBID' WHERE ID ='$ud_IMG'"

That query is wrong, remove the asterisk. Also, you don't know if there is an error because you don't check the return type of mysql_queryor use mysql_error.

那个查询是错误的,去掉星号。此外,您不知道是否有错误,因为您没有检查mysql_query或使用mysql_error.

回答by Patrick

The update query is wrong. You need to tell it which table then which fields specifically. The asterisk is only used on select statements.

更新查询错误。您需要告诉它哪个表然后是哪些字段。星号仅用于选择语句。

Also it would be helpful if you checked for whether or not the query was successful. Take a look below. I have rewritten your code a bit. I also allowed for the ID to come from the POST or the GET by using REQUEST. And I removed the mysql_close() call since it is completely unneeded as it will be closed when the script stops running.

如果您检查查询是否成功,这也会很有帮助。看看下面。我已经稍微重写了你的代码。我还通过使用 REQUEST 允许 ID 来自 POST 或 GET。我删除了 mysql_close() 调用,因为它完全不需要,因为它会在脚本停止运行时关闭。

<?php
$ud_ID = $_REQUEST["ID"];
$ud_firstname = $_POST["ud_firstname"];
$ud_surname = $_POST["ud_surname"];
$ud_FBID = $_POST["ud_FBID"];
$ud_IMG = $_POST["ud_IMG"];

mysql_connect('localhost', 'admin', 'passw0rd') or die(mysql_error());
echo "MySQL Connection Established! <br>";

mysql_select_db("students") or die(mysql_error());
echo "Database Found! <br>";

$query = "UPDATE stokesley_students SET firstname = '$ud_firstname', surname = '$ud_surname', 
FBID = '$ud_FBID' WHERE ID = '$ud_ID'";

$res = mysql_query($query);

if ($res)
  echo "<p>Record Updated<p>";
else
  echo "Problem updating record. MySQL Error: " . mysql_error();
?>

Quick little reference on the PHP mysql_query function: http://nl.php.net/manual/en/function.mysql-query.php

PHP mysql_query 函数的快速参考:http: //nl.php.net/manual/en/function.mysql-query.php

Also I bet you would like a few good tutorials to help you out learning PHP and MySQL. Check out this site: http://net.tutsplus.com/category/tutorials/php/

另外我敢打赌你会想要一些好的教程来帮助你学习 PHP 和 MySQL。看看这个网站:http: //net.tutsplus.com/category/tutorials/php/