G-SHEFF
Новичок
обновление данных с помощью php в базе MySQL не происходит.
Где ошибка? почему при нажатии на кнопку происходит рэсэт страницы, а не обновление данных?
PHP:
<?php
$user = "nahuj";
$pass = "nahuj";
$db = "nahuj";
function untag ($string) {
$string = ereg_replace("<","<",$string);
$string = ereg_replace(">",">",$string);
$string = ereg_replace('\\\"',""",$string);
$string = ereg_replace(":",":",$string);
$string = ereg_replace("!","!",$string);
$string = ereg_replace("\r","",$string);
$string = ereg_replace("\n","<br>",$string);
$string = ereg_replace("%","%",$string);
$string = ereg_replace("^ +","",$string);
$string = ereg_replace(" +$","",$string);
$string = ereg_replace(" +"," ",$string);
$string = ereg_replace("'","\'",$string);
return $string;
}
$link = mysql_connect( "localhost", $user, $pass );
if ( ! $link )
die( "Couldn't connect to MySQL" );
mysql_select_db( $db, $link ) or die ( "couldn't open $db: ".mysql_error() );
if ( isset ( $_POST['ntext'] ) && isset( $_POST['news_id'] ) && isset( $_POST['ntitle'] ) )
{
$news_id=(int)$_POST[news_id];
$ntext=untag($_POST[ntext]);
$ntitle=untag($_POST[ntitle]);
$query = "UPDATE news SET ntitle = '$ntitle', ntext = '$ntext' where news_id = '$news_id'";
$result = mysql_query( $query );
if ( ! $result )
die ("Couldn't update: ".mysql_error());
print "<h1>Table updated: ".mysql_affected_rows() .
"row(s) changed</h1><p>";
}
?>
<?
$result = mysql_query( "SELECT news_id, ntext, ntitle FROM news" );
if ( ! $result )
die ("Tabel empty");
while( $a_row = mysql_fetch_object( $result ) )
{
print "<table><tr><form action=\"$PHP_SELF\" method=\"POST\"><td width=20% align=center valign=top rowspan=2><input type=\"hidden\" name=\"news_id\" value=\"$a_row->news_id\">сообщение №: $a_row->news_id;<br>дата: $a_row[1];</td>";
print "<td width=80% align=justify valign=middle><textarea rows=7 cols=75 name=\"ntext\">$a_row->ntext</textarea></font></td></tr><tr><td width=80% align=justify valign=middle>";
print "<i> тема текста:</i></font><br><font size=\"-1\"><textarea rows=3 cols=75 name=\"ntitle\">$a_row->ntitle</textarea></font><p align=right><input type=\"submit\" name=\"action\" value=\"save\"><input type=\"submit\" name=\"action\" value=\"delete\"></form></p></td></tr></table>";
}
mysql_close( $link );
?>
</form>

