以 PHP 形式编辑 MYSQL 行

发布于 2024-11-27 21:39:12 字数 2071 浏览 2 评论 0原文

我以前曾这样做过,但由于某种原因,这次我无法让它发挥作用! 我为此抓狂!因为没有错误,所以它只是不会更新数据库。

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

ID | IMGNU |名字 |姓氏 | FBID

我们以第 233 行为例。

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

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

这是我已经拥有的。

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

这是 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();
?>

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

谢谢 亚历克斯

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

Let's use row 233 for an example.

I can view a specific row by going to 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.

Here is what i have already.

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

<

?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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

终遇你 2024-12-04 21:39:13

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(除了星号之外,您的查询还将 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>";
}
?>

edit.php - with some changes

<?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 ID with the $ud_IMG variable)

    <?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>";
}
?>
清音悠歌 2024-12-04 21:39:13
"UPDATE * stokesley_students SET firstname = '$ud_firstname', surname = '$ud_surname', 
FBID = '$ud_FBID' WHERE ID ='$ud_IMG'"

该查询是错误的,删除星号。另外,你不知道是否有错误,因为你没有检查 mysql_query 或使用 mysql_error

"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_query or use mysql_error.

嘿看小鸭子会跑 2024-12-04 21:39:13

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

如果您检查查询是否成功也会很有帮助。看看下面。我稍微重写了你的代码。我还允许 ID 通过使用 REQUEST 来自 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();
?>

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

另外,我打赌您会想要一些好的教程来帮助您学习 PHP 和 MySQL。查看此网站:http://net.tutsplus.com/category/tutorials/php/< /a>

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.

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

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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文