Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
need help with that code
Old 10-01-2008, 10:42 AM need help with that code
Average Talker

Posts: 20
Trades: 0
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('"''&quot;'$_POST['tytul']); $data=str_replace(","""$_POST['data']); $text=str_replace('"''&quot;'$_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($result00);
                        
$d=@mysql_result($result01);
                        
$text=@mysql_result($result02);
                        
$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);">&nbsp;&nbsp;
                                    <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>
eazy24 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 10-01-2008, 10:50 AM Re: need help with that code
Sleeping Troll's Avatar
Ultra Talker

Posts: 351
Name: Butch Begy
Trades: 0
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'";
I suppose that the first value for the Insert query is the id_news value, I also suspect that it is an auto increment field, If so then how are you determining 'id' for the update?
Sleeping Troll is offline
Reply With Quote
View Public Profile
 
Old 10-01-2008, 11:18 AM Re: need help with that code
Average Talker

Posts: 20
Trades: 0
Quote:
Originally Posted by Sleeping Troll View Post
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'";
I suppose that the first value for the Insert query is the id_news value, I also suspect that it is an auto increment field, If so then how are you determining 'id' for the update?
i finally figured it out: That code here was wrong,$id&=
PHP Code:
if (($_GET['mode']=='edit') && (strlen($_GET['id'])))
                    {
                    
$id&=$_GET['id']; 
needs to be:
PHP Code:
if (($_GET['mode']=='edit') && (strlen($_GET['id'])))
                    {
                    
$id=$_GET['id'

Last edited by eazy24; 10-01-2008 at 12:03 PM..
eazy24 is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to need help with that code
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.19175 seconds with 12 queries