Jump to content

Check if a field is filled out, then move to the next (php/mysql)


Pr0digy

Recommended Posts

Hi!

 

I am new to PHP but learning the basics.

With the help of samples online i have managed to build a registration form that adds all the filled out info to the database and reads it back when a search field is used.

 

People can register their computers based on the serial number.

The database is made up like this:

id

serial

1firstname

1lastname

1emailaddress

2firstname

2lastname

2emailaddress

etc.

 

I can not figure out how to do the following:

When a serialnumber is already registered but a new owner wants to add his/her information to the database, how can the form see if all the fields starting with '1' have already been filled out, then automatically put all the new information in the fields starting with '2' ?

 

This is the code i use:

$query = "
INSERT INTO `records` (
	`id`,
	`serial`,
	`1firstname`,
	`1lastname`,
	`1emailaddress`,
	)
	VALUES (
	'', '".$_POST['Serial']."', '".$_POST['Name']."', '".$_POST['LastName']."',  '".$_POST['Email']."'
	);

 

Any help would be greatly appreciated :)

 

Jay

Link to comment
Share on other sites

what? You mean if a current user sells his PC to someone lets say, and they want to register the PC on your site, she will not be able to because the serial number will already be taken?

 

How exactly do you plan on handling such a situation?

 

I really can't understand you, could you please reiterate the scenario in a more understandable  way?

 

Thanks! We'd love to help you, its just hard to understand you..

Link to comment
Share on other sites

I'm sorry, let me try again :)

 

People can register anything with a serial number really.

This is so that when an item is purchased, someone can see the history of that product (if previous owners have registered it on the site that is). It will show in what years it was previously owned and in what country etc (down the road) and if it has ever been reported stolen.

 

You are correct, right now if a second owner wants to add their information to a serial number that's already in the database... it doesn't work because it has already been registered. So i want it to 'see' that and then move on to the next set of empty fields (2firstname, 2lastname etc) and put the information in there.

 

I hope this helps :)

 

Jay.

Link to comment
Share on other sites

I would assume that you are not planning to have fields in the same row for multiple registrations.

 

If you are, you would be designing a maintenance nightmare.

 

Yo need 1 row per registration with one set of fields in it. you can then link the rows as necessary to get a history, by searching on serial number

Link to comment
Share on other sites

"I would assume that you are not planning to have fields in the same row for multiple registrations.

If you are, you would be designing a maintenance nightmare."

 

That is exactly how it's currently setup  :-[

 

"Yo need 1 row per registration with one set of fields in it. you can then link the rows as necessary to get a history, by searching on serial number"

 

Sounds much better but that is way above and beyond my capabilities i'm afraid.

Link to comment
Share on other sites

do not do this, just insert new rows, and a date registered.. and then when a user PULLS UP the information, just do

 

SELECT * FROM products WHERE serial = '$serial' ORDER BY register_date DESC

 

^^ that will show the most recent registration FIRST and then reverse-chronologically thereafter.

Link to comment
Share on other sites

Yes, that is the only sensible way to proceed.

 

In addition to showing the history in the way RussellReal suggested, you would check for a serial number already existing by a

 

select * from products where serial = $_POST['serial'] and see if you get any rows. but that is not strictly necessary as you will be inserting a new record in any case.

 

I would add that in my view it is easier to store the registration dates as unix timestamps, but how you do it is optional.

 

Link to comment
Share on other sites

"SELECT * FROM products WHERE serial = '$serial' ORDER BY register_date DESC"

 

This worked but it only shows me the last record of that serial number. (i made 3)

I use this to call up the information after a search:

<?php displayColumn('firstname'); ?>
<?php displayColumn('lastname'); ?>

etc.

I deleted all the 2firstname, 2lastname etc.

with "<?php displayColumn('firstname'); ?>"

should it call up all 3 records from that serial number and display them or am i missing something?

 

Jay

Link to comment
Share on other sites

While i appreciate the feedback i have decided to hire someone and have it all done for me.

Every question i ask generates more questions in return and i can't wrap my head around it lol.

Any code i have so far is copy/pasted together from another friend's website so using second-hand-code, while amazingly i got this far, is not helping.

I need it built from scratch for me and of course i do not expect this to be done for free on a forum :)

 

Thanks :)

 

Jay.

Link to comment
Share on other sites

Just to reiterate, when you do a query you get a result SET.  A set is 0 or more rows.  When the result set is prepared in the sql server, it still has to be FETCHED.  You do this in a loop because you don't know how many rows you might have in your result set --- it could be 0 or it could be a million or more, so you need to do the type of loop that jl5501 describes.

 

In a nutshell, the reason you only see one row is that you are only fetching one row. 

 

 

Link to comment
Share on other sites

While i appreciate the feedback i have decided to hire someone and have it all done for me.

Every question i ask generates more questions in return and i can't wrap my head around it lol.

Any code i have so far is copy/pasted together from another friend's website so using second-hand-code, while amazingly i got this far, is not helping.

I need it built from scratch for me and of course i do not expect this to be done for free on a forum :)

 

Thanks :)

 

Jay.

 

Understandable, and a good choice.  Far too often people come in and get in over their head.  If you're not a programmer or a student who wants to be a programmer, it's far better to get someone who knows what they are doing.  Best of luck with your project.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.