Page 1 of 1

Forms why is it not working?

Posted: Mon Oct 28, 2013 6:19 pm
by hybris
Hi all,

I want an administrator page where I can browse and edit my members. Not all fields will be possible to change (like the id field i use as a key).

I tried to make a page where i read the user database and print the values into input forms and then add a submit button at the end of each line.

When pressing the submit button the idea is to transfer the values to a page called admin.php that will get the submitted values and update the db.

I got it working so far so it fills out the names and info inside the input fields but when i press the submit button I cannot retreive the values inside admin.php

The code from the sending page:

Code: Select all

  $stmt = $mysqli->prepare("SELECT id, username, email, userlevel, useravatar, userrating FROM users ");
                $stmt->execute();
                $stmt->bind_result($u_id, $u_username, $u_email, $u_userlevel, $u_useravatar, $u_userrating);
                $stmt->store_result();
                echo '...ID..........NAMN...........................EMAIL.....................................LEVEL.........AVATAR..............Rating <br>';
                while($row=$stmt->fetch()){
                ?><form id=<?php echo $u_id; ?> name="form1" method="post" action="admin.php"><?php  //EJ KLAR
                echo $u_id;
                ?><input name ="id" type="hidden" id="u_id" value=<?echo $u_id?>><?php
                ?><td><input name="name" type="text" id="name" size="20" value=<? echo $u_username ?> /></td><?php
                ?><td><input name="email" type="text" id="email" size="30" value=<? echo $u_email ?> /></td><?php
                ?><td><input name="level" type="text" id="userlevel" size="2" value=<? echo $u_userlevel ?> /></td><?php
                ?><td><input name="avatar" type="text" id="useravatar" size="20" value=<? echo $u_useravatar ?> /></td><?php
                ?><td><input name="rating" type="text" id="userrating" size="2" value=<? echo $u_userrating ?> /></td>
                <input type="submit" name="Submit" value="Update" /> <br><?php
The code of the receiving page admin.php

Code: Select all

 $u_id=$_POST['u_id'];
    $name=$_POST['name'];
    $email=$_POST['email'];
    $level=$_POST['level'];
    $avatar=$_POST['avatar'];
    $rating=$_POST['rating'];
    
    echo "TEST";
    echo $u_id;
    echo $name;
    echo $email;
    echo $level;
    echo $avatar;
    echo $rating;
(Just echo for debug...)

Also, in the first page I noted if I have a username containing a blank it only fills out the infobox with the part before the blank so "peter pan" will be just peter... I assume i can fix this by replacing the blanks with some special char...but it is quite annoying. Does anyone know why it only inputs the first part of the string up until the blank?

Thanks

Re: Forms why is it not working?

Posted: Mon Oct 28, 2013 9:40 pm
by Christopher
You might be having problems with spaces because you don't have quotes around you values. It should be value="<? echo $u_username ?>"

Re: Forms why is it not working?

Posted: Tue Oct 29, 2013 2:20 am
by hybris
Yes sir you are right,

I missed that, thanks.

Ok so that solved the spacing problem and I retreive the values now in admin.php but no matter which button I press it always submits the last printed user (I was kind of expecting that since the while fetch() loop constantly updates the strings that will be submitted.)

I assume I need to convert the strings to arrays and use the users id number to know where in the array i put the info...

Like if I have 3 users with ID 1, 2, 7 I could create a $name[7] array and store the users in $name[1] $name[2] $name[7] (yeah I know the array will be bigger than nessecary I could do a $name[2] array and store in 0 1 2... but i expect the id to be somewhat in order since users wont be deleted).

I cant figure out how to get the button to send the proper place in the array though... like if i click the button after first user it should send $name[1] and if i click on the third user it should send $name[7].

Any suggestions?

I guess I could use a single submitbutton at the end of the page and update the entire db but i really want to be able to update row for row (1 row = 1 user).

Thanks

Re: Forms why is it not working?

Posted: Tue Oct 29, 2013 9:56 pm
by Christopher
hybris wrote:Ok so that solved the spacing problem and I retreive the values now in admin.php but no matter which button I press it always submits the last printed user (I was kind of expecting that since the while fetch() loop constantly updates the strings that will be submitted.)
Your code cuts off at the end, but I don't see a </form> end tag. If you want each iteration to generate its own form then each needs to be closed. Perhaps it considers it all one big form and just take one value because the <input>s have the same name in each form.

Re: Forms why is it not working?

Posted: Wed Oct 30, 2013 4:33 am
by hybris
Cristopher,

THANK YOU!

Yepp that solved the problem :) I feel like such a noob (well I am..but anyway hehe). Anyway you saved me lots of work, thanks :)