PHP MySQL的UPDATE命令不会更新 [英] php mysql UPDATE command wont update
本文介绍了PHP MySQL的UPDATE命令不会更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<!DOCTYPE HTML>
< head>
< title>编辑学生< / title>
< / head>
<?php
$ user ='root'; //数据库用户名(xampp的根)
$ pass =''; //数据库密码(empty for exampp)
$ db ='dragondrivingschooldb'; //数据库名称
$ con = new mysqli('localhost',$ user,$ pass,$ db)或die(Unable to connect); //创建新的数据连接('主机/服务器的名称',用户,密码,数据库名称)
if(isset($ _ POST ['btnUpdate'])){//一旦更新按钮被按下执行此代码
$ updatequery = mysqli_query($ con,UPDATE booking SET FirstName ='$ _ POST [txtfirstname]'WHERE BookingID ='$ _ POST [txtid]'); //执行UpDate Query
};
$ sql = mysqli_query($ con,SELECT * FROM booking); //从预订中选择全部
//为表
创建标题echo< table border ='1'>
< tr>
ooking $ ID
First Name
< /< / th>
< TR>中;
//显示编辑表格///////////////////////////////////// ////////////////////////////////////////////////// // $ ////////////
while($ row = mysqli_fetch_array($ sql)){//运行sql代码,直到没有更多的行要导入
echo < form action = EditStudent.php method = post>; //在本页顶部运行更新代码
//用查询填充表(sql)
echo< tr>;
echo< td>< input name = update type = submit value = update />< / td>; //一旦按下更新行,此按钮分开
echo< td>< input type = text value =。 $ row ['BookingID']。 name = txtid />< / td>;
echo< td>< input type = text value =。 $ row ['FirstName']。 name = txtfirstname />< / td>;
echo< / tr>;
}
echo< / table>;
echo< / form>;
mysqli_close($ con); //关闭连接
?>
< / html>
解决方案
我认为BookingID是一个整数,更新行需要:
$ $ p $ $ updatequery = mysqli_query($ con,UPDATE booking SET FirstName ='。$ _POST [ 'txtfirstname']。'WHERE BookingID =。$ _POST ['txtid']。); //执行UpDate查询
编辑:
我测试了您的脚本,问题在于你关闭了while循环外的表单。现在它工作了
<!DOCTYPE html>
< head>
< title>编辑学生< / title>
< / head>
<?php
$ user ='root'; //数据库用户名(xampp的根)
$ pass =''; //数据库密码(empty for exampp)
$ db ='all_tests'; //数据库名称
$ con = new mysqli('localhost',$ user,$ pass,$ db)或die(Unable to connect); //创建新的数据连接('主机/服务器的名称',用户,密码,数据库名称)
if(isset($ _ POST ['btnUpdate'])){//一旦更新按钮被按下执行此代码
$ updatequery = mysqli_query($ con,UPDATE test_1 SET FirstName ='。$ _POST ['txtfirstname']。''WHERE BookingID ='。$ _POST ['txtid' ]。'); //执行UpDate Query
};
$ sql = mysqli_query($ con,SELECT * FROM test_1); //从预订中选择全部
//为表
创建标题echo< table border ='1'>
< tr>
ooking $ ID
First Name
< /< / th>
< TR>中;
//显示编辑表格///////////////////////////////////// ////////////////////////////////////////////////// // $ ////////////
while($ row = mysqli_fetch_array($ sql)){//运行sql代码,直到没有更多的行要导入
echo < form method = post>; //在本页顶部运行更新代码
//用查询填充表(sql)
echo< tr>;
echo< td>< input name ='btnUpdate'type ='submit'value ='update'/>< / td>; //一旦按下更新行,此按钮分开
echo< td>< input type ='text'value =。 $ row ['BookingID']。 name ='txtid'/>< / td>;
echo< td>< input type ='text'value =。 $ row ['FirstName']。 name ='txtfirstname'/>< / td>;
echo< / tr>;
回声< / form>;
}
echo< / table>;
mysqli_close($ con); //关闭连接
?>
All the data is loaded into a table, each row as a button. Once the button has been pressed, the row has to be updated. But when I click on the button, nothing happens. The text box on the rows return to its original value... It is really starting to cheese me off
<!DOCTYPE html>
<head>
<title>Edit Students</title>
</head>
<?php
$user = 'root'; //Database username ("Root for xampp")
$pass = ''; //Database password ("empty for exampp")
$db = 'dragondrivingschooldb'; //Name of database
$con = new mysqli('localhost', $user, $pass, $db) or die("Unable to connect"); //Create new data connection ('name of host/server', user, password, database name)
if (isset($_POST['btnUpdate'])) { //Once Update button pressed perform this code
$updatequery = mysqli_query($con, "UPDATE booking SET FirstName='$_POST[txtfirstname]' WHERE BookingID='$_POST[txtid]' "); //excute UpDate Query
};
$sql = mysqli_query($con, "SELECT * FROM booking"); //Select All from Booking
//Create Headers for table
echo "<table border='1'>
<tr>
<th></th>
<th>Booking ID</th>
<th>First Name</th>
</tr>";
//Show Edit Form///////////////////////////////////////////////////////////////////////////////////////////////////
while($row = mysqli_fetch_array($sql)) { //Run sql code till there are no more rows to import
echo "<form action=EditStudent.php method=post>"; //Run update code at top of this page
//Populate table with query (sql)
echo "<tr>";
echo "<td> <input name=update type=submit value=update /> </td>"; //once press update row this button is apart of
echo "<td> <input type=text value=" . $row['BookingID'] . " name=txtid /> </td>";
echo "<td> <input type=text value=" . $row['FirstName'] . " name=txtfirstname /> </td>";
echo "</tr>";
}
echo "</table>";
echo "</form>";
mysqli_close($con); //Close connection
?>
</html>
解决方案
I think that BookingID it is an integer, so your update line need to be:
$updatequery = mysqli_query($con, "UPDATE booking SET FirstName='" . $_POST['txtfirstname'] . "' WHERE BookingID=" . $_POST['txtid'] . ""); //excute UpDate Query
EDIT: I tested your script and the problem was that you closed the form outside the while loop. Now its working
<!DOCTYPE html>
<head>
<title>Edit Students</title>
</head>
<?php
$user = 'root'; //Database username ("Root for xampp")
$pass = ''; //Database password ("empty for exampp")
$db = 'all_tests'; //Name of database
$con = new mysqli('localhost', $user, $pass, $db) or die("Unable to connect"); //Create new data connection ('name of host/server', user, password, database name)
if (isset($_POST['btnUpdate'])) { //Once Update button pressed perform this code
$updatequery = mysqli_query($con, "UPDATE test_1 SET FirstName='" . $_POST['txtfirstname'] . "' WHERE BookingID='" . $_POST['txtid'] . "'"); //excute UpDate Query
};
$sql = mysqli_query($con, "SELECT *FROM test_1"); //Select All from Booking
//Create Headers for table
echo "<table border='1'>
<tr>
<th></th>
<th>Booking ID</th>
<th>First Name</th>
</tr>";
//Show Edit Form///////////////////////////////////////////////////////////////////////////////////////////////////
while($row = mysqli_fetch_array($sql)) { //Run sql code till there are no more rows to import
echo "<form method=post>"; //Run update code at top of this page
//Populate table with query (sql)
echo "<tr>";
echo "<td> <input name='btnUpdate' type='submit' value='update' /> </td>"; //once press update row this button is apart of
echo "<td> <input type='text' value=" . $row['BookingID'] . " name='txtid' /> </td>";
echo "<td> <input type='text' value=" . $row['FirstName'] . " name='txtfirstname' /> </td>";
echo "</tr>";
echo "</form>";
}
echo "</table>";
mysqli_close($con); //Close connection
?>
这篇关于PHP MySQL的UPDATE命令不会更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文