The SitePoint Forums have moved.

You can now find them here.
This forum is now closed to new posts, but you can browse existing content.
You can find out more information about the move and how to open a new account (if necessary) here.
If you get stuck you can get support by emailing forums@sitepoint.com

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

A few PHP/MySQL issues

Hey,

Just a few issues, I'd like to resolve with the help of willing fellow SitePoint members

Issue 1

I am working on a content management system to make it easy to manage the websites in my new Icelandic free webmaster resources and tips website. So I made up a form that collects information about a website and posts it into the database.

The problem is, that when I make an entry, sometimes the page or MySQL or whatever it is, decides to make 1-3 empty rows in my database and then post the data.

How can I prevent that?
This is the code for the form, and I am using MySQL.

Issue 2 mainly consist of my lack of knowledge in PHP and MySQL. I'd like to know how I can select invidual rows and make 1 page display all information from a row, so here is my attempt, that doesn't really work:

The first, and the reason you are seeing the empty rows problem, is that the insert code is running every time the page is loaded, not every time the form is submitted.

An "if" condition will fix that problem.

PHP Code:

if ($_POST['Submit'])
{
// put your php for dealing with the form data here
}

The second problem is that there is nothing keeping incomplete forms from being added. Even if you add the if condition above, if someone were to hit the submit button without filling out anything you'd get the empty row.

So, you might consider testing each variable for minimum requirements:

By the way the nonsense (erwer, and so on) is what was entered into row nr. 2 when testing.

So basically, that half-way does it, it shows me the content, but it won't print anything in "Title: $title". So, it's reading the database, but not showing up correctly. That makes me wonder, I got that "$i" in the code from a tutorial website that used loops, do you think removing that unnecessary $i for every attribute would solve the problem?

But first, all of those functions do the same thing. They take a row (as requested by your query) and place it into an array where it is available for you to use however. Once you get the data from mysql this way, there is no need to additionally use mysql_fetch().

You can then access the information in the array as $array_name['key_name'].

So, picking up in your code from:
$r = mysql_fetch_assoc($result);

After that point, you can access that row as $r['column-name'].
If you feel more comfy with $column-name, you could do this:
$column-name = $r['column-name'];
... but the difference is mostly superficial.

Does that make sense?

Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?