• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

[PHP] News Manager

Kavvson

Gdy boli cie glowa wez
Joined
Jun 25, 2008
Messages
1,177
Reaction score
72
Location
Poland
This script doesn't delete the news. Can you tell me why?
PHP:
if($_REQUEST['do'] == 'delete')
{
	
 // $result = $mysqli->query("DELETE FROM `z_news_big` WHERE `z_news_big`.`hide_news` = '$hide_news' AND `z_news_big`.`date` = '$date' AND `z_news_big`.`author_id` = '$author_id' AND `z_news_big`.`image_id` = '$image_id' AND `z_news_big`.`topic` = '$topic' AND `z_news_big`.`text` = '$text' LIMIT 1");
 $result = "DELETE FROM `z_news_big` WHERE `topic` = '$topic' LIMIT 1";
 $execute = $mysqli->query($result);

                     echo "<b>News Deleted!<br>You'll be redirected to Home Page after (1) Seconds";
                     //header("location: index.php");
                     echo "<meta http-equiv=Refresh content=1;url=?page=news&do=index>";
}


PHP:
<?PHP
$i=1;
$data = $mysqli->query("SELECT * FROM `z_news_big`");
	while($item = $data->fetch_array()){
	echo "<tr><td>".$i."</td>";
		echo "<td>".$item['topic']."</td>";

		echo "<td>".$item['author']."</td>";
		echo "<td>".$item['image_id']."</td>";
		echo "<td>".$item['hide_news']."</td>";
		echo "<td><textarea class='tinymce'>".$item['text']."</textarea></td>";
		echo "<td>Delete : <a href='?page=news&do=delete'>Delete</a></td>";
		echo"</tr>";
		$i++;
		

		}
		unset ($data);  

?>
 
A post from Topic field :

PHP:
if($_REQUEST['do'] == 'add'){

$array = array(
    1 => 'Topic',
    2 => 'Author',
    3 => 'Text',
    4 => 'Image_Id',
    5 => 'Date',
    6 => 'Hide_News',
    7 => 'Author_Id');
    
    
if ($_POST['Post'])
{
$hide_news =$_POST['hide_news'];
$date =$_POST['date'];
$author =$_POST['author'];
$author_id =$_POST['author_id'];
$image_id =$_POST['image_id'];
$topic =$_POST['topic'];
$text =$_POST['text'];

$p="INSERT INTO z_news_big(hide_news,date,author,author_id,image_id,topic,text) values('$hide_news','$date','$author','$author_id','$image_id','$topic','$text')";
    $execute = $mysqli->query($p);
          echo "<b>Thank you! News added Successfully!<br>You'll be redirected to Home Page after (1) Seconds";
          echo "<meta http-equiv=Refresh content=1;url=?page=news&do=index>";
}
else

{
print "<form action='$PHPSELF' method='POST'>";
print "$array[1] - <input type='text' name='topic'><br />";
print "$array[3] -     <textarea id='elm1' name='text' style='width: 100%' class='tinymce'></textarea><br />";
print "$array[6] -  <input type='text' name='hide_news'><br />";
print "$array[5] -  <input type='text' name='date'><br />";
print "$array[2] -  <input type='text' name='author'><br />";
print "$array[7] -  <input type='text' name='author_id'><br />";
print "$array[4] -  <input type='text' name='image_id'><br />";


print "<input type='submit' name='Post' value='Post News!'><br />";
}

}


if($_REQUEST['do'] == 'delete')
{
    
 // $result = $mysqli->query("DELETE FROM `z_news_big` WHERE `z_news_big`.`hide_news` = '$hide_news' AND `z_news_big`.`date` = '$date' AND `z_news_big`.`author_id` = '$author_id' AND `z_news_big`.`image_id` = '$image_id' AND `z_news_big`.`topic` = '$topic' AND `z_news_big`.`text` = '$text' LIMIT 1");
 $result = "DELETE FROM `z_news_big` WHERE `topic` = '$topic' LIMIT 1";
 $execute = $mysqli->query($result);

                     echo "<b>News Deleted!<br>You'll be redirected to Home Page after (1) Seconds";
                     //header("location: index.php");
                     echo "<meta http-equiv=Refresh content=1;url=?page=news&do=index>";
}
 
You should take another value to execute the DELETE ~ query, like $date (equal to `news` field).

So it would be: $mysqli->query('DELETE FROM `z_news_big` WHERE `date` = '.$date);

I would do this.. o_o (I prefer numeric values)
 
Doesn't work i think there is no connection of there scripts. Full code

PHP:
<!-- Load TinyMCE -->
<script type="text/javascript" src="tiny_mce/jquery.tinymce.js"></script>
<script type="text/javascript">
	$().ready(function() {
		$('textarea.tinymce').tinymce({
			// Location of TinyMCE script
			script_url : 'tiny_mce/tiny_mce.js',

			// General options
			theme : "advanced",
			plugins : "pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,advlist",

			// Theme options
			theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,styleselect,formatselect,fontselect,fontsizeselect",
			theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
			theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",
			theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,pagebreak",
			theme_advanced_toolbar_location : "top",
			theme_advanced_toolbar_align : "left",
			theme_advanced_statusbar_location : "bottom",
			theme_advanced_resizing : true,


			// Replace values for the template plugin
			template_replace_values : {
				username : "Some User",
				staffid : "991234"
			}
		});
	});
</script>



<?php
if($_REQUEST['do'] == 'add'){

$array = array(
	1 => 'Topic',
	2 => 'Author',
	3 => 'Text',
	4 => 'Image_Id',
	5 => 'Date',
	6 => 'Hide_News',
	7 => 'Author_Id');
	
	
if ($_POST['Post'])
{
$hide_news =$_POST['hide_news'];
$date =$_POST['date'];
$author =$_POST['author'];
$author_id =$_POST['author_id'];
$image_id =$_POST['image_id'];
$topic =$_POST['topic'];
$text =$_POST['text'];

$p="INSERT INTO z_news_big(hide_news,date,author,author_id,image_id,topic,text) values('$hide_news','$date','$author','$author_id','$image_id','$topic','$text')";
	$execute = $mysqli->query($p);
          echo "<b>Thank you! News added Successfully!<br>You'll be redirected to Home Page after (1) Seconds";
          echo "<meta http-equiv=Refresh content=1;url=?page=news&do=index>";
}
else

{
print "<form action='$PHPSELF' method='POST'>";
print "$array[1] - <input type='text' name='topic'><br />";
print "$array[3] - 	<textarea id='elm1' name='text' style='width: 100%' class='tinymce'></textarea><br />";
print "$array[6] -  <input type='text' name='hide_news'><br />";
print "$array[5] -  <input type='text' name='date'><br />";
print "$array[2] -  <input type='text' name='author'><br />";
print "$array[7] -  <input type='text' name='author_id'><br />";
print "$array[4] -  <input type='text' name='image_id'><br />";


print "<input type='submit' name='Post' value='Post News!'><br />";
}

}


if($_REQUEST['do'] == 'delete')
{
	
 // $result = $mysqli->query("DELETE FROM `z_news_big` WHERE `z_news_big`.`hide_news` = '$hide_news' AND `z_news_big`.`date` = '$date' AND `z_news_big`.`author_id` = '$author_id' AND `z_news_big`.`image_id` = '$image_id' AND `z_news_big`.`topic` = '$topic' AND `z_news_big`.`text` = '$text' LIMIT 1");
$mysqli->query('DELETE FROM `z_news_big` WHERE `date` = '.$date);

                     echo "<b>News Deleted!<br>You'll be redirected to Home Page after (1) Seconds";
                     //header("location: index.php");
                     echo "<meta http-equiv=Refresh content=1;url=?page=news&do=index>";
}

	$array = array(
	'1' => 'Topic',
	'2' => 'Author',
	'3' => 'Image_Id',
	'4' => 'Hide_News');
	
	
		if($_REQUEST['do'] == 'index')
	{ 
	?>
	
	
	<a href="?page=news&do=add">Add News</a>
	
<style>
._table       { border: 2px solid #98bcdf; background-color:#ffffff; padding:0px; border-collapse:collapse; margin-left: auto; margin-right: auto;}
._th   { background: url(images/header_background.gif) repeat-x right bottom; background-color: #ffffff; border: 1px solid #688caf; padding:4px; padding-right: 5px; padding-left: 5px; font-weight: bold; font-size: 13px; color: #333333; text-align: center; white-space: nowrap; width: 1%; margin-left: auto; margin-right: auto;}
._td          { border-right: #d0d7e5 1px solid; border-left: #d0d7e5 1px solid; border-bottom: #d0d7e5 1px solid; padding-right: 4px; padding-left: 5px; padding-bottom: 2px; padding-top: 3px; white-space:nowrap; text-align:center; margin-left: auto;margin-right: auto; }
._tr {background: none repeat scroll 0% 0% rgb(247, 249, 251);}
</style>


<table align="center" width="95%" class="_table" id="prd__contentTable" dir="ltr">
<thead>
<tr class="dg_tr">
<th class="_th">#</th>
<th class="_th">Title</th>
<th class="_th">Author</th>
<th class="_th">Image_Id</th>
<th class="_th">Hide_News</th>
<th class="_th">Text</th>
<th class="_th">Date</th>
<th class="_th">Action</th>
</tr>
</thead>

<?PHP
$i=1;
$data = $mysqli->query("SELECT * FROM `z_news_big`");
	while($item = $data->fetch_array()){
	echo "<tr class='_tr'><td class='_td'>".$i."</td>";
		echo "<td class='_td'>".$item['topic']."</td>";
		echo "<td class='_td'>".$item['author']."</td>";
		echo "<td class='_td'>".$item['image_id']."</td>";
		echo "<td class='_td'>".$item['hide_news']."</td>";
		echo "<td class='_td'><textarea cols=30 rows=5>".$item['text']."</textarea></td>";
		echo "<td class='_td'>".$item['date']."</td>";
		echo "<td class='_td'>Delete : <a href='?page=news&do=delete'>Delete</a></td></tr>";
		$i++;
		

		}
		unset ($data);  

	}
?>

</table>
 
Please do "print_r( $mysqli );" in order to see whether you have a valid database connection.
 
Output

mysqli Object ( [affected_rows] => 0 [client_info] => 5.1.41 [client_version] => 50141 [connect_errno] => 0 [connect_error] => [errno] => 0 [error] => [field_count] => 78 [host_info] => localhost via TCP/IP [info] => [insert_id] => 0 [server_info] => 5.1.41 [server_version] => 50141 [sqlstate] => 00000 [protocol_version] => 10 [thread_id] => 3 [warning_count] => 0 )
 
Ah, I see the reason why it doesn't work. You have assigned the post values to variables within the ?do=add part. I would advise you to move the part where you assign the values to above any of the ?do parts.
 
Hmm still doesn't work even when its at the site begging in no ?do=

Maybe want check it via TeamViewer?
 
Last edited:
I made it!

PHP:
<?PHP
$i=1;
$data = $mysqli->query("SELECT * FROM `z_news_big`");
    while($item = $data->fetch_array()){
    echo "<tr class='_tr'><td class='_td'>".$i."</td>";
        echo "<td class='_td'>".$item['topic']."</td>";
        echo "<td class='_td'>".$item['author']."</td>";
        echo "<td class='_td'>".$item['image_id']."</td>";
        echo "<td class='_td'>".$item['hide_news']."</td>";
        echo "<td class='_td'><textarea cols=30 rows=5>".$item['text']."</textarea></td>";
        echo "<td class='_td'>".$item['date']."</td>";
        echo "<td class='_td'>Delete : <a href='?page=news&action=delete&date=$item[date]'>Delete</a></td></tr>";
        $i++;
        

        }
        
    unset ($data); 


    }
            if($_REQUEST['action'] == 'delete')
{
     $date = $_GET['date'];
 // $result = $mysqli->query("DELETE FROM `z_news_big` WHERE `z_news_big`.`hide_news` = '$hide_news' AND `z_news_big`.`date` = '$date' AND `z_news_big`.`author_id` = '$author_id' AND `z_news_big`.`image_id` = '$image_id' AND `z_news_big`.`topic` = '$topic' AND `z_news_big`.`text` = '$text' LIMIT 1");
$mysqli->query('DELETE FROM `z_news_big` WHERE `date` = '.$date.' LIMIT 1');


                     echo "<b>News Deleted!<br>You'll be redirected to Home Page after (1) Seconds";
                     //header("location: index.php");
                     echo "<meta http-equiv=Refresh content=1;url=?page=news&do=index>";
}
 
?>

It was about the
  • HTML:
    <a href='?page=news&action=delete&date=$item[date]'>Delete</a>
    Correct link
  • PHP:
    $date = $_GET['date'];
    A GET value
  • PHP:
    $mysqli->query('DELETE FROM `z_news_big` WHERE `date` = '.$date.' LIMIT 1');
    The value into db

But 2 problems.
a) Security
b) If the date = 1 & there are 2 dates id delete both values with the same date number LIMIT 1 doesn't change anything.

Regards
 
I made it!

PHP:
<?PHP
$i=1;
$data = $mysqli->query("SELECT * FROM `z_news_big`");
    while($item = $data->fetch_array()){
    echo "<tr class='_tr'><td class='_td'>".$i."</td>";
        echo "<td class='_td'>".$item['topic']."</td>";
        echo "<td class='_td'>".$item['author']."</td>";
        echo "<td class='_td'>".$item['image_id']."</td>";
        echo "<td class='_td'>".$item['hide_news']."</td>";
        echo "<td class='_td'><textarea cols=30 rows=5>".$item['text']."</textarea></td>";
        echo "<td class='_td'>".$item['date']."</td>";
        echo "<td class='_td'>Delete : <a rel='nofollow' href='?page=news&amp;action=delete&amp;date=$item[date]'>Delete</a></td></tr>";
        $i++;
        

        }
        
    unset ($data); 


    }
            if($_REQUEST['action'] == 'delete')
{
     $date = $_GET['date'];
 // $result = $mysqli->query("DELETE FROM `z_news_big` WHERE `z_news_big`.`hide_news` = '$hide_news' AND `z_news_big`.`date` = '$date' AND `z_news_big`.`author_id` = '$author_id' AND `z_news_big`.`image_id` = '$image_id' AND `z_news_big`.`topic` = '$topic' AND `z_news_big`.`text` = '$text' LIMIT 1");
$mysqli->query('DELETE FROM `z_news_big` WHERE `date` = '.$date.' LIMIT 1');


                     echo "<b>News Deleted!<br>You'll be redirected to Home Page after (1) Seconds";
                     //header("location: index.php");
                     echo "<meta http-equiv=Refresh content=1;url=?page=news&do=index>";
}
 
?>

It was about the
  • HTML:
    <a rel='nofollow' href='?page=news&amp;action=delete&amp;date=$item[date]'>Delete</a>
    Correct link
  • PHP:
    $date = $_GET['date'];
    A GET value
  • PHP:
    $mysqli->query('DELETE FROM `z_news_big` WHERE `date` = '.$date.' LIMIT 1');
    The value into db

But 2 problems.
a) Security
b) If the date = 1 & there are 2 dates id delete both values with the same date number LIMIT 1 doesn't change anything.

Regards

And the possibilities of getting 2 rows with the same `date` value are...?

Also:
PHP:
if($var->isAdmin && is_numeric($_GET['date']))
{
execute if date is a numeric value and the user is logged on admin acc
}
else
{
// execute if date is not a numeric value or is not an administrator
}
 
Atm there is no login system so :| i need to fix it.. 2 date values.. Dunno how Gesior store DATA numbers but its like 123925043 etc you know. And if you have add'ed f.e 2 news in the same date both will be deleted.


2.

...
PHP:
<?PHP
$i=1;
$data = $mysqli->query("SELECT * FROM `z_news_big`");
    while($item = $data->fetch_array()){
    echo "<tr class='_tr'><td class='_td'>".$i."</td>";
        echo "<td class='_td'>".$item['topic']."</td>";
        echo "<td class='_td'>".$item['author']."</td>";
        echo "<td class='_td'>".$item['image_id']."</td>";
        echo "<td class='_td'>".$item['hide_news']."</td>";
        echo "<td class='_td'><textarea cols=30 rows=5>".$item['text']."</textarea></td>";
        echo "<td class='_td'>".$item['date']."</td>";
        echo "<td class='_td'>Delete : <a href='?page=news&action=delete&date=$item[date]'>Delete</a></td></tr>";
        $i++;
        }
        
        IF (!empty($data)){
        echo '</table><table align="center" width="95%" class="_table" id="prd__contentTable" dir="ltr"><tr align="center" class=\'_tr\'><td>There are no news!<br>You can add one here - <a href="?page=news&do=add">Add News</a></td></tr></table>';
        }else{
        echo "";
        }
    unset ($data);
...

Why does the !empty always shows the echo? Even when its not empty :D
 
1286738357 - October, same day.

1286734757 - October, same day, 1 hour difference.

# About EMPTY:

Cuz' you are doing if(!empty($var)), that means: if $var IS NOT empty.
 
Empty issue:

PHP:
	if($_REQUEST['do'] == 'index')
	{ 
	?>
	
	

	
<style>
._table       { border: 2px solid #98bcdf; background-color:#ffffff; padding:0px; border-collapse:collapse; margin-left: auto; margin-right: auto;}
._th   { background: url(images/header_background.gif) repeat-x right bottom; background-color: #ffffff; border: 1px solid #688caf; padding:4px; padding-right: 5px; padding-left: 5px; font-weight: bold; font-size: 13px; color: #333333; text-align: center; white-space: nowrap; width: 1%; margin-left: auto; margin-right: auto;}
._td          { border-right: #d0d7e5 1px solid; border-left: #d0d7e5 1px solid; border-bottom: #d0d7e5 1px solid; padding-right: 4px; padding-left: 5px; padding-bottom: 2px; padding-top: 3px; white-space:nowrap; text-align:center; margin-left: auto;margin-right: auto; }
._tr {background: none repeat scroll 0% 0% rgb(247, 249, 251);}
</style>


<table align="center" width="95%" class="_table" id="prd__contentTable" dir="ltr">
<thead>
<tr class="dg_tr">
<th class="_th">#</th>
<th class="_th">Title</th>
<th class="_th">Author</th>
<th class="_th">Image_Id</th>
<th class="_th">Hide_News</th>
<th class="_th">Text</th>
<th class="_th">Date</th>
<th class="_th">Action</th>
</tr>
</thead>

<?PHP
$i=1;
$data = $mysqli->query("SELECT * FROM `z_news_big`");
	while($item = $data->fetch_array()){
	echo "<tr class='_tr'><td class='_td'>".$i."</td>";
		echo "<td class='_td'>".$item['topic']."</td>";
		echo "<td class='_td'>".$item['author']."</td>";
		echo "<td class='_td'>".$item['image_id']."</td>";
		echo "<td class='_td'>".$item['hide_news']."</td>";
		echo "<td class='_td'><textarea cols=30 rows=5>".$item['text']."</textarea></td>";
		echo "<td class='_td'>".$item['date']."</td>";
		echo "<td class='_td'>Delete : <a href='?page=news&action=delete&date=$item[date]'>Delete</a></td></tr>";
		$i++;
		}
		
		if (empty($data)) {
   echo '</table><table align="center" width="95%" class="_table" id="prd__contentTable" dir="ltr"><tr align="center" class=\'_tr\'><td>There are no news!<br>You can add one here - <a href="?page=news&do=add">Add News</a></td></tr></table>';
		
} else {
    echo "NOT EMPTY!";
} 
	unset ($data); 


	}

It always shows wrong
 
Edit/Modify part error - No result?

PHP:
if($_REQUEST['action'] == 'edit'){
$gdate = $_GET['date'];
$array = array(
    1 => 'Topic - ',
    2 => 'Author - ',
    3 => 'Text - ',
    4 => 'Image_Id - ',
    5 => 'Date - ',
    6 => 'Hide_News - ',
    7 => 'Author_Id - ');
    
if ($_POST['Update'])
{
$p="UPDATE z_news_big SET hide_news='$hide_news', date='$date', author='$author', author_id='$author_id', image_id='$image_id', topic='$topic',text='$text' WHERE `date` = '.$gdate.'";
    $execute = $mysqli->query($p);
          echo "<b>Thank you! News edited Successfully!<br>You'll be redirected to Home Page after (1) Seconds";
          echo "<meta http-equiv=Refresh content=1;url=?page=news&do=index>";
          print_r( $mysqli );
}
else

{

$data = $mysqli->query('SELECT * FROM `z_news_big` WHERE `date` = '.$gdate.' LIMIT 1');
    while($item = $data->fetch_array()){
    print "<form action='$PHPSELF' method='POST'>";
        echo "$array[1] <input type='text' name='topic' value=".$item['topic']."><br>";
        echo "$array[2] <input type='text' name='author' value=".$item['author']."><br>";
        echo "$array[4] <input type='text' name='image_id' value=".$item['image_id']."><br>";
        echo "$array[6] <input type='text' name='hide_news' value=".$item['hide_news']."><br>";
        echo "$array[3] <textarea cols=30 rows=5 id='elm1' name='text' >".$item['text']."</textarea><br>";
        echo "$array[5] <input type='text' name='date' value=".$item['date']."><br>";
        }

print "<input type='submit' name='Update' value='Post News!'><br />";
}
}

?page=news&action=edit&date=1212


Thank you! News edited Successfully!
You'll be redirected to Home Page after (1) Secondsmysqli Object ( [affected_rows] => 0 [client_info] => 5.1.41 [client_version] => 50141 [connect_errno] => 0 [connect_error] => [errno] => 0 [error] => [field_count] => 0 [host_info] => localhost via TCP/IP [info] => Rows matched: 0 Changed: 0 Warnings: 0 [insert_id] => 0 [server_info] => 5.1.41 [server_version] => 50141 [sqlstate] => 00000 [protocol_version] => 10 [thread_id] => 571 [warning_count] => 3 )
 
Back
Top