Heim >Backend-Entwicklung >PHP-Tutorial >php小白求教form display and update mysql的问题, 可以正常dispaly,无法update

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

WBOY
WBOYOriginal
2016-06-06 20:47:29959Durchsuche

<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看看~

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn