PHP News Form Help

software development

#1

http://www.uhrebirth.com/test.php

I’m using the following PHP code to test a news form submission on my website:

<?php include("dbconnect.php"); $getnews = mysql_query("select * FROM newz ORDER BY id DESC"); while($r=mysql_fetch_array($getnews)){ extract($r); echo ("$title on $date $news"); } ?>

If you look at my source code (see above link), it should be enclosed in the div that has its ID set to “center”. However, there is nothing in the source code.

The form seems to post fine, it just doesn’t display. What am I doing wrong?


#2

try this little debug script:

<?php include("dbconnect.php"); $getnews = mysql_query("select * FROM newz ORDER BY id DESC"); for($row=0; $row < mysql_num_rows($getnews); $row++){ $rs = mysql_fetch_row($getnews); for($col=0; $col < mysql_num_fields($getnews); $col++){ $arNewsList[$row][mysql_field_name($getnews,$col)] = $rs[$col]; }; }; echo "<pre>"; print_r($arNewsList); echo "</pre>"; ?> That will echo to the screen the contents of the array. You’ll be able to see from that what’s returned from the newz table.

Another basic(er) test is:

<?php include("dbconnect.php"); $getnews = mysql_query("select * FROM newz ORDER BY id DESC"); echo mysql_num_rows($getnews); ?> If either of those look “odd”, you know there’s a problem with the data returned (or NOT returned as the case may be).

Cheers,
Karl

web design, development & seo by DigitalVibe


#3

Okay, I just tried both of those. The first one did absolutely nothing except for inserting the tags

 in the center div. I tried the latter, and now the number zero appears on my test page. I’m still confused?
      

#4

That basically means that your query is returning no records, hence nothing being displayed.

Connect to phpMyAdmin using your database connection info in the include file.

web address is http://<database_host>

user/pass will be those in your database connection include.

Then examine the newz table, check there’s data in it, and try running the “select * from newz order by id desc” statement in phpMyAdmin’s sql query window.

Let me know what you get. Anything returned fromt he sql query? Is there anything in the newz table when you browse to it?

Cheers,
Karl

web design, development & seo by DigitalVibe


#5

Ran the query, and I got an “empty result (zero rows)” message.


#6

In phpMyAdmin, how many rows does it say are in the newz table?

Cheers,
Karl

web design, development & seo by DigitalVibe


#7

It says I have zero rows. Now what?

P.S. Thanks so much for all of your help!


#8

It means you have no news items… the table is empty therefore nothing is returned.

Basically, there doesn’t appear to be any problem. You’re asking it to pull everything from the newz table and display it on the page. It does that… it pulls back everything (which is nothing as the table is empty), and therefore displays nothing on the page.

What i would do is:

<?php include("dbconnect.php"); $getnews = mysql_query("select * FROM newz ORDER BY id DESC"); if(mysql_num_rows($getnews)) { // news items found, so show them while($r=mysql_fetch_array($getnews)){ extract($r); echo ("<b>$title on $date</b><p>$news</p>"); } } else { // no news items found, so tell the user echo "<b>No news items were found</b>"; } ?>
That code will then tell the user there’s nothing to see.

After you’ve done that and tested it, go into phpMyAdmin, and “insert” a row into the newz table.

You should then see “1 row”. If you refresh your news page, you should see the one item you just created.

Add another in phpMyAdmin, then reload the page again… you should have two items.

Let me know the outcome :slight_smile:

Cheers,
Karl

web design, development & seo by DigitalVibe


#9

Does the row I create have to have any specific settings? Sorry; I’m really new to SQL/MySQL.


#10

nope, just anything as long as the data is what you would expect to appear in any “newz” post.

Without me knowing what columns the table contains, i can’t really say what you should have there… all you need to do is fill in the boxes in the “insert new row” page of phpMyAdmin and put in some sensible test data as you would expect it to appear.

So if you’ve got numeric coumns, enter a number. If it’s a text (varchar, text), enter a bit of text. If it’s a date field, select a date.

Just anything really to act as a test. You can always “empty” the table when you’ve finished testing, so don’t worry about putting crap in there - it can always be purged. :slight_smile:

Cheers,
Karl

web design, development & seo by DigitalVibe


#11

Okay, well, I’m finally getting somewhere. If you go to http://www.uhrebirth.com/test.php and view the page, you’ll see that I’ve been playing around with it. However, that’s not at all the type of output I’m trying to get.

See, I’ve got another URL on my site that goes to a form for me to post news. I’m trying to make it where I submit the form, the entry goes into the database, and then it sends that data to my test page as output.


#12

Right, well at least you know it’s working!

Next step:

  1. Let me know all of the columns (fields) in the newz table… i want column (field) names, and what they store.

  2. give me an example of how you want it to look on the page.

If you can tell me that, i’ll have a go at knocking something together for you.

Cheers,
Karl

web design, development & seo by DigitalVibe


#13

Thanks a lot for the offer, but I’d rather just have troubleshooting help if you don’t mind. I’m a programming major in college, and I like to learn all of this stuff myself. I just feel like it needs to be me who hard codes it, if that makes any sense :wink:


#14

Not that anybody asked for, or cares about, my opinion, I think your attitude about that is great (as is Karl’s willingness to help!).

A lot of people nowadays seem to forget that is how we learn. If you keep that attitude, and work your way through your issues with the minimum of help required to get yourself over the rough spots, you will know a lot more at the end of the day than many of your peers!

:wink:

–rlparker


#15

Okay, in that case:

every var that you use in the template should correspond to a column in the table.

You need to use the php date formatting functions (in the php.net manual) to format the date accordingly, and also structure the html as you want it, and build the vars into the html…

so best way is build an example of how you want it to look in html, then replace the text you used in your sample with vars from the newz table.

Easy peasy! :slight_smile:

EDIT: if any of the above doesn’t make sense and you want additional pointers, don’t be afraid to ask - i’ve deliberately made my post “theoratical” so you can do a bit of thinking for yourself. As rlparker says, your attitude is most definitely admirable!

Cheers,
Karl

web design, development & seo by DigitalVibe


#16

I agree 100%

Although int he past i’ve known what i’ve wanted to do in pseudo code, but then not known the in-built functions to achieve what I want, which often meant the need for me to work everything out in my head, then look for a code snippet that serves the purpose.

I’ve pretty much used the same approach for every language (scripting or otherwise)… PHP, ASP, VB, ASP.NET, Javascript, Actionscript…

Joseph, one of the main skills needed in programming is having a logical mind. If you can rationalise how a problem needs solving, you can learn the languages just fine.

In this instance i would say: Write down what needs to be done step by step… once you’ve sorted that, go on to building the code around it.

Cheers,
Karl

web design, development & seo by DigitalVibe


#17

Well, thank you, both of you :slight_smile:

I took programming concepts last semester (barely managed to make an A in it, too :p), and most of the class was just writing pseudocode algorithms. I’ve never used it on a personal project, though. Perhaps it’s time I try…


#18

I used to work for a development team at a company here in England and we always wrote pseudocode when we were working on a new project.

I’m talking about a team of 20 developers aged 25 to 50, so it’s definitely not just a college thing. In college i always refused to write pseudocode because it was boring, but it’s only when you get to the real working life that you realise its value :slight_smile:

Cheers,
Karl

web design, development & seo by DigitalVibe