Its news posting script,news are stored do database,everything works fine,exept one part-edit news,wont work,i tried to figure it out myself several times,but i dont understand whats wrong with this code,so please look at that code and say whats wrong!
It gives me: Error while trying to read data. Cannot display information! every time when i try to edit news,also news fields are complately empty
here is code:
PHP Code:
<?php
require_once('../config/config.php');
require_once('../config/func.php');
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>News</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-2">
<link href="../config/style.css" rel="stylesheet" type="text/css">
</head>
<script language="JavaScript">
function check(id)
{
if (confirm('Are you sure?'))
{
window.location='news.php?action=del&id=' + id;
}
}
function clear_form(form)
{
form.elements[0].value='';
form.elements[1].value='';
form.text.value='';
}
</script>
<body>
<div class="admin">NEWS<?php echo (!empty($_GET['form']) ? ' - ' . $_GET['form'] : ''); ?></div>
<?php
$form=$_GET['form'];
if (auth())
{
if (!empty($_GET['action']))
{
$action=$_GET['action'];
if ($action=='add' || ($action=='edit' && strlen($_POST['id'])))
{
if (!empty($_POST['tytul']) && !empty($_POST['data']) && !empty($_POST['text']))
{
$tytul=str_replace('"', '"', $_POST['tytul']); $data=str_replace(",", "", $_POST['data']); $text=str_replace('"', '"', $_POST['text']);
if (preg_match("/[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}/", $data))
{
if ($action=='add')
$sql="INSERT INTO `news` VALUES ('', '$tytul', '$data', '$text')";
else
{
$id=$_POST['id'];
$sql="UPDATE `news` SET `tytul`='$tytul', `data`='$data', `tresc`='$text' WHERE `id_news`='$id'";
}
if (mysql_query($sql))
{
switch_page('news.php?form=' . (($action=='add') ? 'add' : 'edit/delete'), (($action=='add') ? 'Added' : 'Updated') . ' news!');
}
else
error('Error in SQL query! MySQL: ' . mysql_error() . '<br><a href="javascript:history.go(-1)">return</a>');
}
else
error('Wrong date format (yyyy-mm-dd, hh:mm:ss)! <a href="javascript:history.go(-1)">return</a>');
}
else
error('<div class="error">Fill all fields! <a href="javascript:history.go(-1)">return</a>');
}
elseif($action=='del' && strlen($_GET['id']))
{
$id=$_GET['id'];
if(mysql_query("DELETE FROM `news` WHERE `id_news`='$id'"))
{
switch_page('news.php?form=edit/delete', 'Deleted news from database');
}
else
error('Error in SQL query! MySQL: ' . mysql_error() . '<br><a href="javascript:history.go(-1)">return</a>');
}
}
else
{
if ($form=='add')
{
?>
<table style="margin:10px" width="90%">
<?php
if (($_GET['mode']=='edit') && (strlen($_GET['id'])))
{
$id&=$_GET['id'];
if($result=mysql_query("SELECT tytul, data, tresc FROM news WHERE `id_news`='$id'"))
{
$t=@mysql_result($result, 0, 0);
$d=@mysql_result($result, 0, 1);
$text=@mysql_result($result, 0, 2);
$read=false;
if (strlen($t) && strlen($d) && strlen($text))
$read=true;
else
error('Error while trying to read data. Cannot display information!');
}
else
error('Error in SQL query! MySQL: ' . mysql_error() . '<br>Cannot display data!');
}
?>
<form method="POST" style="margin: 0px" name="post" action="news.php?action=<?php echo (($read && ($_GET['mode']=='edit')) ? 'edit' : 'add'); ?>">
<?php if($read && ($_GET['mode']=='edit')) echo '<input type="hidden" name="id" value="'.$id .'">'; ?>
<tr>
<td>Title:</td><td><input type="text" name="tytul" maxlength="100" size="50" value="<?php if($read && ($_GET['mode']=='edit')) echo $t; ?>"></td>
</tr>
<tr>
<td>Date:</td><td><input type="text" name="data" maxlength="20" size="20" value="<?php if($read && ($_GET['mode']=='edit')) echo str_replace(' ', ', ', $d); else print date("Y-m-d, H:i:s", time()); ?>"> <em>(yyyy-mm-dd, hh:mm:ss)</em></td>
</tr>
<tr>
<script language="JavaScript" src="../config/func.js"></script>
<td></td>
<td>
<input type="button" accesskey="b" name="addbbcode0" value=" B " style="font-weight:bold; width: 30px; margin-top: 20px" onClick="bbstyle(0)">
<input type="button" accesskey="i" name="addbbcode2" value=" i " style="font-style:italic; width: 30px" onClick="bbstyle(2)">
<input type="button" accesskey="u" name="addbbcode4" value=" u " style="text-decoration: underline; width: 30px" onClick="bbstyle(4)">
</td>
</tr>
<tr>
<td valign="top">Text:</td><td><textarea name="text" cols="75" rows="18"><?php if($read && ($_GET['mode']=='edit')) echo $text; ?></textarea></td>
</tr>
<tr>
<td></td><td><input <?php echo (($read && ($_GET['mode']=='edit')) ? 'type="button"' : 'type="reset"'); ?> value="RESET" onClick="clear_form(this.form);">
<input type="submit" value="<?php echo (($read && ($_GET['mode']=='edit')) ? 'EDIT NEWS' : 'ADD NEWS'); ?>"></td>
</tr>
</form>
</table>
<?php
}
elseif($form=='edit/delete')
{
?>
<table style="margin:10px">
<tr bgcolor="#ffffff" style="font-weight:bold">
<td width="20" align="center">ID</td>
<td width="130">Date</td>
<td width="250">Title</td>
<td width="50"></td>
</tr>
<?php
if ($result=mysql_query('SELECT `id_news`, `tytul`, `data` FROM `news` ORDER BY `data`'))
{
if (mysql_num_rows($result) > 0)
{
while($row=mysql_fetch_assoc($result))
{
echo '<tr onMouseOver="style.background=\'#eeeeee\'" onMouseOut="style.background=\'#ffffff\'" style="cursor: default">
<td style="color: #666666; text-align:center">' . $row['id_news'] . '</td>
<td>' . str_replace(' ', ', ', $row['data']) . '</td>
<td>' . $row['tytul'] . '</td>
<td style="color:#666666; font-size:10px; text-align: center"><a href="news.php?form=add&mode=edit&id=' . $row['id_news'] . '">edit</a> | <a href="javascript:check(' . $row['id_news'] . ');">del</a></td>
</tr>';
}
}
else
error('<tr><td colspan="3">No news in database table</td></tr>');
}
else
error('<tr><td colspan="3">Error in SQL query! MySQL: ' . mysql_error() . '</td></tr>');
mysql_free_result($result);
?>
</table>
<?php
}
}
}
?>
</body>
</html>