TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
Advertisement
Associates
Associates
techtuts Darkmindz
CSS Tutorials Tutorialsphere.com - Free Online Tutorials
Boston PHP SurfnLearn
Reply
 
LinkBack Thread Tools Display Modes
Old 06-30-2008, 02:54 PM   #1 (permalink)
The Frequenter
Inquisitive 
 
codefreek's Avatar
 
Join Date: Sep 2007
Location: Near you.
Posts: 404
Thanks: 219
codefreek is on a distinguished road
Help I need of some help here

Sorry i misspell the topic name xD

Is the problem in this script
that i am not calling out the news part
with
PHP Code:
while($website_row mysql_fetch_array($web_result))
{
print 
$website_row['rest'];

? or something else it just wont work :S

It is suposed to switch around to news cat 1 news cat 2
so all the post with cat id 1 gets that in cat 1 etc..

it worked with my links but not on this so whats
wrong ??..

Thank you in advance




PHP Code:
<?php
include('db_connect.php');
$webq "select id, name, des from news ";
if(isset(
$_GET['cat']))
 {
   
$webq .= sprintf("where cat_id = '%s'"mysql_real_escape_string($_GET['cat']));
 }
 
$web_result mysql_query($webq);
 if(!
cat_result)
{
echo 
mysql_error();
}
$catq "select id, name from cat";
$cat_result mysql_query($catq);
 
?>
 
<?php
while($cat_row mysql_fetch_array($cat_result))
{
?>
<a href="test.php?cat=<?php echo $cat_row['id'];?>"><?php echo $cat_row['name'];?></a>
<?php
}
?>




My cat table,

Code:
CREATE TABLE `cat` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(100) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

--
-- Data in table `cat`
--

INSERT INTO `cat` (`id`, `name`) VALUES
(1, 'news one'),
(2, 'news two');


my news table

Code:
CREATE TABLE `news` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(100) NOT NULL,
  `des` varchar(150) NOT NULL,
  `rest` text NOT NULL,
  `tim` date NOT NULL,
  `valid` int(11) NOT NULL,
  `users_id` int(11) NOT NULL,
  `cat_id` int(11) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;

--
-- Data in table `news`
--

INSERT INTO `news` (`id`, `name`, `des`, `rest`, `tim`, `valid`, `users_id`, `cat_id`) VALUES
(1, 'lol', 'omg this is the edit title', '<p>Lorem ipsum dolor sit amet beatae iste aperiam vitae magni aperiam omnis omnis, doloremque ut odit illo aperiam inventore inventore illo natus unde voluptas consequuntur aperiam sunt dicta iste dolores eos aspernatur aspernatur, perspiciatis dicta aut sit </p>\r\n<p>Lorem ipsum dolor sit amet beatae iste aperiam vitae magni aperiam omnis omnis, doloremque ut odit illo aperiam inventore inventore illo natus unde voluptas consequuntur aperiam sunt dicta iste dolores eos aspernatur aspernatur, perspiciatis dicta aut sit </p>\r\n<p>Lorem ipsum dolor sit amet beatae iste aperiam vitae magni aperiam omnis omnis, doloremque ut odit illo aperiam inventore inventore illo natus unde voluptas consequuntur aperiam sunt dicta iste dolores eos aspernatur aspernatur, perspiciatis dicta aut sit </p>', '2008-06-22', 1, 0, 1),
(5, 'admin', 'hello', '<p>Lorem ipsum dolor sit amet beatae iste aperiam vitae magni aperiam omnis omnis, doloremque ut odit illo aperiam inventore inventore illo natus unde voluptas consequuntur aperiam sunt dicta iste dolores eos aspernatur aspernatur, perspiciatis dicta aut sit </p>\r\n<p>Lorem ipsum dolor sit amet beatae iste aperiam vitae magni aperiam omnis omnis, doloremque ut odit illo aperiam inventore inventore illo natus unde voluptas consequuntur aperiam sunt dicta iste dolores eos aspernatur aspernatur, perspiciatis dicta aut sit </p>\r\n', '2008-06-22', 1, 0, 1),
(6, 'roland', 'hello sir', 'mertius del miro candiburia smorjamertius del miro candiburia smorjamertius del miro candiburia smorjamertius del miro candiburia smorjamertius del miro candiburia smorjamertius del miro candiburia smorjamertius del miro candiburia smorjamertius del miro candiburia smorjamertius del miro candiburia smorjamertius del miro candiburia smorjamertius del miro candiburia smorjamertius del miro candiburia smorjamertius del miro candiburia smorjamertius del miro candiburia smorjamertius del miro candiburia smorjamertius del miro candiburia smorjamertius del miro candiburia', '2008-06-28', 1, 0, 2);
my users table

Code:
CREATE TABLE `users` (
  `id` int(11) NOT NULL auto_increment,
  `username` varchar(30) NOT NULL,
  `password` varchar(40) NOT NULL,
  `email` text NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=16 ;

--
-- Data in table `users`
--

INSERT INTO `users` (`id`, `username`, `password`, `email`) VALUES
(1, 'user', 'password', 'lol@info.com'),
Thank you!
__________________
inquisitive
1. Eager to acquire knowledge.
2. Too curious; overly interested; nosy.
codefreek is offline  
Reply With Quote
Old 06-30-2008, 03:10 PM   #2 (permalink)
The Frequenter
Inquisitive 
 
codefreek's Avatar
 
Join Date: Sep 2007
Location: Near you.
Posts: 404
Thanks: 219
codefreek is on a distinguished road
Default

sql table in pastebin!
PasteBin.be
code in pastebin
PasteBin.be
__________________
inquisitive
1. Eager to acquire knowledge.
2. Too curious; overly interested; nosy.
codefreek is offline  
Reply With Quote
Old 06-30-2008, 05:24 PM   #3 (permalink)
The Frequenter
Inquisitive 
 
codefreek's Avatar
 
Join Date: Sep 2007
Location: Near you.
Posts: 404
Thanks: 219
codefreek is on a distinguished road
Application Error

i found a bug i had 2 "where" changed it to and but still not working
PHP Code:
$webq "select id, name, des from news valid = 1 ";
if(isset(
$_GET['cat']))
 {
   
$webq .= sprintf("and cat_id = '%s'"mysql_real_escape_string($_GET['cat']));
 } 
__________________
inquisitive
1. Eager to acquire knowledge.
2. Too curious; overly interested; nosy.
codefreek is offline  
Reply With Quote
Old 06-30-2008, 05:40 PM   #4 (permalink)
The Frequenter
Inquisitive 
 
codefreek's Avatar
 
Join Date: Sep 2007
Location: Near you.
Posts: 404
Thanks: 219
codefreek is on a distinguished road
Default

this is the complete code
PasteBin.be
__________________
inquisitive
1. Eager to acquire knowledge.
2. Too curious; overly interested; nosy.
codefreek is offline  
Reply With Quote
Old 06-30-2008, 07:50 PM   #5 (permalink)
The Frequenter
Inquisitive 
 
codefreek's Avatar
 
Join Date: Sep 2007
Location: Near you.
Posts: 404
Thanks: 219
codefreek is on a distinguished road
Default

like 100 php Guru's here and no one can help me? :S
__________________
inquisitive
1. Eager to acquire knowledge.
2. Too curious; overly interested; nosy.
codefreek is offline  
Reply With Quote
Old 06-30-2008, 07:58 PM   #6 (permalink)
The Visitor
 
Join Date: Jun 2008
Posts: 4
Thanks: 3
webid is on a distinguished road
Default

while($website_row = mysql_fetch_array($web_result))

check $web_result before that line and post it here please.

---

sorry, $cat_result

Last edited by webid : 07-01-2008 at 02:36 AM.
webid is offline  
Reply With Quote
Old 06-30-2008, 08:24 PM   #7 (permalink)
The Frequenter
Inquisitive 
 
codefreek's Avatar
 
Join Date: Sep 2007
Location: Near you.
Posts: 404
Thanks: 219
codefreek is on a distinguished road
Default

PHP Code:
<style type="text/css">
.style4 {
    font-size: xx-large;
    color: #FF9900;
}
</style>
<?php
error_reporting
(E_ALL & ~E_NOTICE);
include(
"db_connect.php");
session_start(); // Starts the session.

if ($_SESSION[‘logged’] != 1) { // There was no session found!

        
header("Location: users.php"); // Goes to login page.

        
exit(); // Stops the rest of the script.

}

echo 
"This is the main page!";


$res "SELECT `id`, `name`, `des`, `rest`, `tim` FROM `news` WHERE `valid` = 1";

$web_result mysql_query($res);

if(!
$web_result) {
echo 
mysql_error();
}

else {
   
}


?>
<?php 

 
while($website_row mysql_fetch_array($web_result))
{


?>

<font size='3' color='CC0000'><strong>
<h1 class="style4">
<?php print $website_row['des'];?></br>
</h1>
</strong></font>


<table border = "0">
<tr>
<th>NEWS:</th>
<td>
<?php print $website_row['rest']; ?>
</div>

</td>

</tr>
</table>


<?php ?>
<?php
echo "<a href=\"logout.php\">Logout</a>";
?>




<?php
$webq 
"select id, name, des from news where valid = 1 ";
if(isset(
$_GET['cat']))
 {
   
$webq .= sprintf("and cat_id = '%s'"mysql_real_escape_string($_GET['cat']));
 }
 
$web_result mysql_query($webq);
 if(!
$cat_result)
{
echo 
mysql_error();
}
$catq "select id, name from cat";
$cat_result mysql_query($catq);

?>

<?php
while($cat_row mysql_fetch_array($cat_result))
{
?>
<a href="index.php?cat=<?php echo $cat_row['id'];?>"><?php echo $cat_row['name'];?></a>
<?php

?>
this is the complete code.
__________________
inquisitive
1. Eager to acquire knowledge.
2. Too curious; overly interested; nosy.
codefreek is offline  
Reply With Quote
Old 06-30-2008, 10:37 PM   #8 (permalink)
The Gregarious
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 562
Thanks: 15
Village Idiot is on a distinguished road
Default

I am not fully understaing what is not working, but here are some things I have noticed that may be messing with things:
-Is your query returning with anything? Because your sprintf is looking for the string while you appear to be needing an integer.
-You check cat_result before your assign it, you also dont use a $ sign.

Your program will most likely die nothing (mysql_error will be blank) every time since you are checking a blank variable for being blank. It could also be of error that sprintf may be canceling the value out (this is, among other reasons, why I don't use sprintf).
__________________
There are two ways to write bug-free code, only the third one works.
Village Idiot is offline  
Reply With Quote
Old 06-30-2008, 10:46 PM   #9 (permalink)
The Frequenter
Inquisitive 
 
codefreek's Avatar
 
Join Date: Sep 2007
Location: Near you.
Posts: 404
Thanks: 219
codefreek is on a distinguished road
Default

hum how can i still do the thing i am doing with the spaces,
with out sprintf?


EDIT:
while($cat_row = mysql_fetch_array($wes))
{
is causing problems :S

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '= '2'' at line 1

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource on line 90



Aaron: noting of that helped.
and PS, i did not want to have all the code on the same post to much better spreed it out,
PS,
Free Wordpress Themes
for wordpress styles.
__________________
inquisitive
1. Eager to acquire knowledge.
2. Too curious; overly interested; nosy.

Last edited by codefreek : 07-01-2008 at 12:35 AM.
codefreek is offline  
Reply With Quote
Old 07-01-2008, 12:31 AM   #10 (permalink)
The Frequenter
 
Join Date: Dec 2007
Location: In my basement
Posts: 368
Thanks: 44
Aaron is on a distinguished road
Default

You need to get rid of the spaces around the equal sign... and your quotation marks seem weirdly placed.

Also, there is an edit button right next to quote post, so you don't need to keep replying :/
__________________

I feel better hating IE.
Send a message via MSN to Aaron
Aaron is offline  
Reply With Quote
Old 07-01-2008, 12:39 AM   #11 (permalink)
The Frequenter
Advanced Programmer Top Contributor Good Samaritan 
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 469
Thanks: 26
sketchMedia is on a distinguished road
Default

From what i can gather, you want the news to be displayed depending on the cat GET request value, i.e. filtering if its specified?

Your code was abit of a mess, alot of breaking in and out of PHP cycles for no reason, i attempted to clean it up abit but it still needs work, but that said it now filters the news, i.e. index.php?cat=1 will display all news with cat_id of 1:

PHP Code:
<style type="text/css">
.style4 {
    font-size: xx-large;
    color: #FF9900;
}
</style>
<?php
error_reporting
(E_ALL & ~E_NOTICE);
include(
"db_connect.php");
session_start(); // Starts the session.

 
if ($_SESSION['logged'] != 1) { // There was no session found!

 
header("Location: users.php"); // Goes to login page.

 
exit(); // Stops the rest of the script.

 
}

echo 
'This is the main page!';
$res 'SELECT 
            `id`, `name`, `des`, `rest`, `tim` 
        FROM 
            `news`
        WHERE 
            `valid` = 1'
;

if(isset(
$_GET['cat']))
{
    
$res .= sprintf(" AND cat_id = '%d'"mysql_real_escape_string($_GET['cat']));
}
if(!
$web_result mysql_query($res)) 
{
    echo 
mysql_error();
}

while(
$website_row mysql_fetch_array($web_result)):
?>
<font size='3' color='CC0000'><strong>
<h1 class="style4">
    <?php print $website_row['des'];?> <br />
</h1>
</strong></font>


<table border="0">
    <tr>
        <th>NEWS:</th>
        <td><?php print $website_row['rest']; ?>
        </td>
    </tr>
</table>


<?php
endwhile;

echo 
'<a href="logout.php">Logout</a>';

$webq 'SELECT 
            `id`, `name`, `des` 
        FROM 
            `news` 
        WHERE 
            `valid` = 1'
;

if(isset(
$_GET['cat']))
{
    
$webq .= sprintf(" AND `cat_id` = '%d'"mysql_real_escape_string($_GET['cat']));
}
if(!
$web_result mysql_query($webq))
{
    echo 
mysql_error();
}
$catq 'SELECT 
            `id`, `name` 
        FROM 
            `cat`'
;
if(!
$cat_result mysql_query($catq))
{
    echo 
mysql_error();
}
while(
$cat_row mysql_fetch_array($cat_result)):
?>
<a href="index.php?cat=<?php echo $cat_row['id'];?>"><?php echo $cat_row['name'];?></a>
<?php
endwhile;
Sorry if this doesnt answer your problem, but its a bit vague.
__________________
sketchMedia is offline  
Reply With Quote
The Following User Says Thank You to sketchMedia For This Useful Post:
codefreek (07-01-2008)
Old 07-01-2008, 12:42 AM   #12 (permalink)
The Frequenter
Inquisitive 
 
codefreek's Avatar
 
Join Date: Sep 2007
Location: Near you.
Posts: 404
Thanks: 219
codefreek is on a distinguished road
Default

thank you but i keep it messy because its my way of learning i keep it dirty its my way but thank you for your help but i still want to figure out what the error was..
so i wont use your code :) but thank you so much for your time!

+ when i try to use the script the firefox stops
and prints cookie error :S
__________________
inquisitive
1. Eager to acquire knowledge.
2. Too curious; overly interested; nosy.
codefreek is offline  
Reply With Quote
Old 07-01-2008, 12:46 AM   #13 (permalink)
The Frequenter
Advanced Programmer Top Contributor Good Samaritan 
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 469
Thanks: 26
sketchMedia is on a distinguished road
Default

Quote:
thank you but i keep it messy because its my way of learning i keep it dirty its my way but thank you for your help but i still want to figure out what the error was..
so i wont use your code :) but thank you so much for your time!
Dont worry m8, all the best programmers start off with messy code xD.
__________________
sketchMedia is offline  
Reply With Quote
The Following User Says Thank You to sketchMedia For This Useful Post:
codefreek (07-01-2008)
Old 07-01-2008, 12:54 AM   #14 (permalink)
The Frequenter
Inquisitive 
 
codefreek's Avatar
 
Join Date: Sep 2007
Location: Near you.
Posts: 404
Thanks: 219
codefreek is on a distinguished road
Default

sketchMedia:
have you any idea why i get cookie error?
when i test your script :S
__________________
inquisitive
1. Eager to acquire knowledge.
2. Too curious; overly interested; nosy.
codefreek is offline  
Reply With Quote
Old 07-01-2008, 12:55 AM   #15 (permalink)
The Frequenter
Inquisitive 
 
codefreek's Avatar
 
Join Date: Sep 2007
Location: Near you.
Posts: 404
Thanks: 219
codefreek is on a distinguished road
Default

THIS IS THE USER CODE


PHP Code:
<?php
error_reporting
(E_ALL & ~E_NOTICE);
include(
"db_connect.php");
session_start(); // Starts the session.

if ($_SESSION[‘logged’] == 1) { // User is already logged in.

        
header("Location: index.php"); // Goes to main page.

        
exit(); // Stops the rest of the script.

} else {

if ( ! isset(
$username))
{
    
$username '';
}

if ( ! isset(
$password))
{
    
$password '';
}

$szForm = <<<FORM
<form action="users.php" name="login" method="post"> 
<table> 
    <tr><td>username</td>
        <td><input type="text" name="username" value="
{$username}" /></td>
    </tr> 

    <tr><td>password</td>
        <td><input type="password" name="password" value="
{$password}" /></td>
    </tr>

    <tr><td colspan="2"><input type='submit' name='login' value='login' /></td></tr> 
</table> 
</form>
FORM;

echo 
$szForm;  
   
    


               
$password mysql_real_escape_string($_POST['password']);
           
$username mysql_real_escape_string($_POST['username']);




               

                
$q mysql_query("SELECT * FROM users WHERE username = '$username'
                 AND password = '$password'"
) or die (mysql_error()); // mySQL query

                
$r mysql_num_rows($q); // Checks to see if anything is in the&nbs