>백엔드 개발 >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으로 문의하세요.