用html表单和php更新mysql行 [英] update mysql row with html form and php
问题描述
我一直在浏览这里的很多主题,但没有找到解决我的问题的方法。
我创建了一个表单,用于显示输入框中的数据库内容,当我更改内容时,它应该在数据库中更新。
<?php
$ con = mysqli_connect(localhost , 根, , 头版);
//检查连接
if(mysqli_connect_errno()){
echo无法连接到MySQL:。 mysqli_connect_error();
$ result = mysqli_query($ con,SELECT * FROM frontpage_left_links)
或die(Error:.mysqli_error($ con));
while($ row = mysqli_fetch_array($ result)){
echo'< form action =method =post>';
echo'< div style =float:left>';
echo'< table border =1bordercolor =#000000>';
echo'< tr>';
echo'< td> link< / td>';
echo'< td>< input type =textname =linkidvalue ='。$ row ['link']。'>< / td>';
echo'< / tr>';
echo'< tr>';
echo'< td> img< / td>';
echo'< td>< input type =textname =imgidvalue ='。$ row ['img']。'>< / td>';
echo'< / tr>';
echo'< tr>';
echo'< td> tekst< / td>';
echo'< td>< input type =textname =imgidvalue ='。$ row ['name']。'>< / td>';
echo'< / tr>';
echo'< tr>';
echo'< td>< input type =submitid =updatename =gemvalue =Gem< / td>< / td>';
echo'< td>< input type =hiddenname =idvalue ='。$ row ['id']。'>< / td>';
echo'< / tr>';
echo'< / table>< / div>';
echo'< div style =float:left>< a href ='。$ row ['link']。'>< center>< img src =img / '。$ row ['img']。'>< br />'.$row['name'].'</center></a></div>';
echo'< / form>< br />< br />< br />< br>< br />< br />< br />';
}
if(isset($ _ POST ['update'])){
$ id = $ _POST ['id'];
$ link = $ _POST ['linkid'];
$ img = $ _POST ['imgid'];
$ name = $ _POST ['nameid'];
$ sql = mysqli_query(UPDATE frontpage_left_links SET link ='$ link',img ='$ img',name ='$ name'WHERE id ='$ id');
$ retval = mysqli_query($ sql,$ con);
if(!$ retval){
die('Could not update data:'。mysql_error());
}
echo已成功更新数据\;
}
mysqli_close($ con);
?>
表单显示数据库内容正常,但更改后没有任何反应。
感谢您的帮助。
这就是现在的样子。
<?php
$ con = mysqli_connect(localhost,root,,frontpage );
//检查连接
if(mysqli_connect_errno())
{
echo无法连接到MySQL:。 mysqli_connect_error();
}
if(isset($ _ POST ['gem']))
{
$ id = $ _POST ['id'];
$ link = $ _POST ['linkid'];
$ img = $ _POST ['imgid'];
$ name = $ _POST ['nameid'];
$ sql = mysqli_query(UPDATE frontpage_left_links SET link ='$ link',img ='$ img',name ='$ name'WHERE id ='$ id');
$ retval = mysqli_query($ con,$ sql);
if(!$ retval)
{
die('Could not update data:'。mysql_error());
}
echo已成功更新数据\;
$ b $ result = mysqli_query($ con,SELECT * FROM frontpage_left_links)
或者die(Error:.mysqli_error( $ CON));
while($ row = mysqli_fetch_array($ result))
{
echo'< form action =method =post>';
echo'< div style =float:left>';
echo'< table border =1bordercolor =#000000>';
echo'< tr>';
echo'< td> link< / td>';
echo'< td>< input type =textname =linkidvalue ='。$ row ['link']。'>< / td>';
echo'< / tr>';
echo'< tr>';
echo'< td> img< / td>';
echo'< td>< input type =textname =imgidvalue ='。$ row ['img']。'>< / td>';
echo'< / tr>';
echo'< tr>';
echo'< td> tekst< / td>';
echo'< td>< input type =textname =nameidvalue ='。$ row ['name']。'>< / td>';
echo'< / tr>';
echo'< tr>';
echo'< td>< input type =submitid =updatename =gemvalue =Gem< / td>< / td>';
echo'< td>< input type =hiddenname =idvalue ='。$ row ['id']。'>< / td>';
echo'< / tr>';
echo'< / table>< / div>';
echo'< div style =float:left>< a href ='。$ row ['link']。'>< center>< img src =img / '。$ row ['img']。'>< br />'.$row['name'].'</center></a></div>';
echo'< / form>< br />< br />< br />< br>< br />< br />< br />';
}
mysqli_close($ con);
?>
现在我收到这个错误消息。
警告:mysqli_query()需要至少2个参数,1在第17行的/Applications/XAMPP/xamppfiles/htdocs/page/admin.php中给出
警告:mysqli_query ):在第19行中的/Applications/XAMPP/xamppfiles/htdocs/page/admin.php中的空查询
无法更新数据:
因为你的更新是在页面的最后,所以把它放在其余的上面。
还要更改isset($ _ POST ['update'] isset($ _ POST ['gem']
<?php
$ con = mysqli_connect(localhost, root,,frontpage);
//检查连接
if(mysqli_connect_errno())
{
echo无法连接到MySQL:mysqli_connect_error ();
}
if(isset($ _ POST ['gem']))
{
$ id = $ _POST ['id '];
$ link = $ _POST ['linkid'];
$ img = $ _POST ['imgid'];
$ name = $ _POST ['nameid'];
$ sql =UPDATE frontpage_left_links SET link ='$ link',img ='$ img',name ='$ name'WHERE id ='$ id';
$ retval = mysqli_query($ con,$ sql);
if(!$ retval)
{
die('Could not update data:'。mysql_error());
}
echo已成功更新数据\;
$ b $ result = mysqli_query($ con,SELECT * FROM frontpage_left_links)
或者die(Error:.mysqli_error( $ CON));
while($ row = mysqli_fetch_array($ result))
{
echo'< form action =method =post>';
echo'< div style =float:left>';
echo'< table border =1bordercolor =#000000>';
echo'< tr>';
echo'< td> link< / td>';
echo'< td>< input type =textname =linkidvalue ='。$ row ['link']。'>< / td>';
echo'< / tr>';
echo'< tr>';
echo'< td> img< / td>';
echo'< td>< input type =textname =imgidvalue ='。$ row ['img']。'>< / td>';
echo'< / tr>';
echo'< tr>';
echo'< td> tekst< / td>';
echo'< td>< input type =textname =imgidvalue ='。$ row ['name']。'>< / td>';
echo'< / tr>';
echo'< tr>';
echo'< td>< input type =submitid =updatename =gemvalue =Gem< / td>< / td>';
echo'< td>< input type =hiddenname =idvalue ='。$ row ['id']。'>< / td>';
echo'< / tr>';
echo'< / table>< / div>';
echo'< div style =float:left>< a href ='。$ row ['link']。'>< center>< img src =img / '。$ row ['img']。'>< br />'.$row['name'].'</center></a></div>';
echo'< / form>< br />< br />< br />< br>< br />< br />< br />';
}
mysqli_close($ con);
?>
I've been looking through many threads on here without finding a solution to my problem. I've created a form that is supposed to show content of a database in input boxes, and when i change the content, it should be updated in the database.
No errors, nothing gets changed.
<?php
$con=mysqli_connect("localhost","root","","frontpage");
// Check connection
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM frontpage_left_links")
or die("Error: ".mysqli_error($con));
while($row = mysqli_fetch_array($result)){
echo '<form action="" method="post">';
echo '<div style="float:left">';
echo '<table border="1" bordercolor="#000000">';
echo '<tr>';
echo '<td>link</td>';
echo '<td><input type="text" name="linkid" value="'.$row['link'].'"></td>';
echo '</tr>';
echo '<tr>';
echo '<td>img</td>';
echo '<td><input type="text" name="imgid" value="'.$row['img'].'"></td>';
echo '</tr>';
echo '<tr>';
echo '<td>tekst</td>';
echo '<td><input type="text" name="imgid" value="'.$row['name'].'"></td>';
echo '</tr>';
echo '<tr>';
echo '<td><input type="submit" id="update" name="gem" value="Gem"</td></td>';
echo '<td><input type="hidden" name="id" value="'.$row['id'].'"></td>';
echo '</tr>';
echo '</table></div>';
echo '<div style="float:left"><a href="'.$row['link'].'"><center><img src="img/'.$row['img'].'"><br />'.$row['name'].'</center></a></div>';
echo '</form><br /><br /><br /><br /><br /><br /><br /><br />';
}
if(isset($_POST['update'])){
$id = $_POST['id'];
$link = $_POST['linkid'];
$img = $_POST['imgid'];
$name = $_POST['nameid'];
$sql = mysqli_query("UPDATE frontpage_left_links SET link = '$link', img = '$img', name = '$name' WHERE id = '$id'");
$retval = mysqli_query( $sql, $con );
if(! $retval ){
die('Could not update data: ' . mysql_error());
}
echo "Updated data successfully\n";
}
mysqli_close($con);
?>
The form show the database content fine, but nothing happens when changed.
I appreciate any help I can get.
This is what it looks like now.
<?php
$con=mysqli_connect("localhost","root","","frontpage");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if(isset($_POST['gem']))
{
$id = $_POST['id'];
$link = $_POST['linkid'];
$img = $_POST['imgid'];
$name = $_POST['nameid'];
$sql = mysqli_query("UPDATE frontpage_left_links SET link = '$link', img = '$img', name = '$name' WHERE id = '$id'");
$retval = mysqli_query( $con, $sql );
if(! $retval )
{
die('Could not update data: ' . mysql_error());
}
echo "Updated data successfully\n";
}
$result = mysqli_query($con,"SELECT * FROM frontpage_left_links")
or die("Error: ".mysqli_error($con));
while($row = mysqli_fetch_array($result))
{
echo '<form action="" method="post">';
echo '<div style="float:left">';
echo '<table border="1" bordercolor="#000000">';
echo '<tr>';
echo '<td>link</td>';
echo '<td><input type="text" name="linkid" value="'.$row['link'].'"></td>';
echo '</tr>';
echo '<tr>';
echo '<td>img</td>';
echo '<td><input type="text" name="imgid" value="'.$row['img'].'"></td>';
echo '</tr>';
echo '<tr>';
echo '<td>tekst</td>';
echo '<td><input type="text" name="nameid" value="'.$row['name'].'"></td>';
echo '</tr>';
echo '<tr>';
echo '<td><input type="submit" id="update" name="gem" value="Gem"</td></td>';
echo '<td><input type="hidden" name="id" value="'.$row['id'].'"></td>';
echo '</tr>';
echo '</table></div>';
echo '<div style="float:left"><a href="'.$row['link'].'"><center><img src="img/'.$row['img'].'"><br />'.$row['name'].'</center></a></div>';
echo '</form><br /><br /><br /><br /><br /><br /><br /><br />';
}
mysqli_close($con);
?>
Now i get this error.
Warning: mysqli_query() expects at least 2 parameters, 1 given in /Applications/XAMPP/xamppfiles/htdocs/page/admin.php on line 17
Warning: mysqli_query(): Empty query in /Applications/XAMPP/xamppfiles/htdocs/page/admin.php on line 19 Could not update data:
Because your udate is at the end of the page put it above the rest.
And also change isset($_POST['update'] to isset($_POST['gem']
<?php
$con=mysqli_connect("localhost","root","","frontpage");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if(isset($_POST['gem']))
{
$id = $_POST['id'];
$link = $_POST['linkid'];
$img = $_POST['imgid'];
$name = $_POST['nameid'];
$sql = "UPDATE frontpage_left_links SET link = '$link', img = '$img', name = '$name' WHERE id = '$id'";
$retval = mysqli_query($con,$sql );
if(! $retval )
{
die('Could not update data: ' . mysql_error());
}
echo "Updated data successfully\n";
}
$result = mysqli_query($con,"SELECT * FROM frontpage_left_links")
or die("Error: ".mysqli_error($con));
while($row = mysqli_fetch_array($result))
{
echo '<form action="" method="post">';
echo '<div style="float:left">';
echo '<table border="1" bordercolor="#000000">';
echo '<tr>';
echo '<td>link</td>';
echo '<td><input type="text" name="linkid" value="'.$row['link'].'"></td>';
echo '</tr>';
echo '<tr>';
echo '<td>img</td>';
echo '<td><input type="text" name="imgid" value="'.$row['img'].'"></td>';
echo '</tr>';
echo '<tr>';
echo '<td>tekst</td>';
echo '<td><input type="text" name="imgid" value="'.$row['name'].'"></td>';
echo '</tr>';
echo '<tr>';
echo '<td><input type="submit" id="update" name="gem" value="Gem"</td></td>';
echo '<td><input type="hidden" name="id" value="'.$row['id'].'"></td>';
echo '</tr>';
echo '</table></div>';
echo '<div style="float:left"><a href="'.$row['link'].'"><center><img src="img/'.$row['img'].'"><br />'.$row['name'].'</center></a></div>';
echo '</form><br /><br /><br /><br /><br /><br /><br /><br />';
}
mysqli_close($con);
?>
这篇关于用html表单和php更新mysql行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!