首页 >后端开发 >php教程 >php小白求教form display and update mysql的问题, 可以正常dispaly,无法update

php小白求教form display and update mysql的问题, 可以正常dispaly,无法update

WBOY
WBOY原创
2016-06-06 20:47:29959浏览

<code class="lang-php"><?php define('DB_NAME', 'form');
define('DB_USER', 'root');
define('DB_PASSWORD', '123456');
define('DB_HOST', 'localhost');


$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);

if (!$link)
{
die('could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db(DB_NAME, $link);

if (!$db_selected)
{
die('Can\'t use ' .DB_NAME .':' . mysql_error());
}
$query = "SELECT * FROM articles";
$result = mysql_query($query) or die(mysql_error());
?></code>
<code><div class="content-holder">
<form action="" method="post">
<table border="1" cellpadding="10" id="ViewTable">
<tr>
<th>title</th>
<th>id</th>
</tr>

<?php while($row = mysql_fetch_array($result))
{
$id = $row['id'];
$title = $row['title'];
?>

<tr>
<td>
<input type="textbox" class="TextAreaTitle" name="title" value="<?=$title?>">
<input type="hidden" name="id" value="<?=$title?>">
</td>
<td>=$id?></td>
</tr>

<?php }
?>
</table>
<input type="submit" name="update" class="submitlink" value="update">
</form>
</div>

<?php if(isset($_POST['update']))
{
for ($i=count($_POST['id']); $i--;) 
{
$id = $_POST['title'][$i];
$title = $_POST['id'][$i];
mysql_query("UPDATE articles SET title= $title WHERE id= $id ");
}
}
?></code>

php小白求教form display and update mysql的问题, 可以正常dispaly,无法update

php小白求教form display and update mysql的问题, 可以正常dispaly,无法update

回复内容:

<code class="lang-php"><?php define('DB_NAME', 'form');
define('DB_USER', 'root');
define('DB_PASSWORD', '123456');
define('DB_HOST', 'localhost');


$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);

if (!$link)
{
die('could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db(DB_NAME, $link);

if (!$db_selected)
{
die('Can\'t use ' .DB_NAME .':' . mysql_error());
}
$query = "SELECT * FROM articles";
$result = mysql_query($query) or die(mysql_error());
?></code>
<code><div class="content-holder">
<form action="" method="post">
<table border="1" cellpadding="10" id="ViewTable">
<tr>
<th>title</th>
<th>id</th>
</tr>

<?php while($row = mysql_fetch_array($result))
{
$id = $row['id'];
$title = $row['title'];
?>

<tr>
<td>
<input type="textbox" class="TextAreaTitle" name="title" value="<?=$title?>">
<input type="hidden" name="id" value="<?=$title?>">
</td>
<td>=$id?></td>
</tr>

<?php }
?>
</table>
<input type="submit" name="update" class="submitlink" value="update">
</form>
</div>

<?php if(isset($_POST['update']))
{
for ($i=count($_POST['id']); $i--;) 
{
$id = $_POST['title'][$i];
$title = $_POST['id'][$i];
mysql_query("UPDATE articles SET title= $title WHERE id= $id ");
}
}
?></code>

php小白求教form display and update mysql的问题, 可以正常dispaly,无法update

php小白求教form display and update mysql的问题, 可以正常dispaly,无法update

是不是数据库查询用户权限不够呢?打开phpmyadmin看看~

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn