Jump to content

Recommended Posts

Please could you help me with following problem:

I have profile page with titles (age, country, gendre), values as text fields (23, France, ...) and check boxes for privacy setting. New titles can be added and text fields can be edited. See the image. example2.PNG

 

I did so far everything, I just have no idea how to save into table if check box of each title was checked, so the value is yes.

 

<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="POST">
        <table>
         <tr><td style='text-align: left; font-weight: bold'>
           Title</td> <td style='text-align: left; font-weight: bold'>Value</td></tr>
         <tr><td><input type="text" name="title"</td>
         <td><input type="text" name="value"</td>
         <td><input type="submit" name="submit" value="Submit"></td>
         </tr>
        </table>
    </form>

<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
        <table>
            <?php
            echo "
            <tr>
                <td>Title</td><td>Value</td><td>Privacy</td>
            </tr>";
            if (isset($_POST))  {
            foreach ($_POST as $key => $value )
               {
               mysql_query ("UPDATE titles SET uValue = '$value' WHERE ID ='$key");
               //this line is where I have problem mysql_query ("UPDATE titles SET uPrivacy = 'yes/no' WHERE ID ='$key'"); it is only yes or only no
               }
               }
            $query =  "SELECT * FROM titles WHERE UserID = $ID";
            $result = mysql_query($query);
            $row = mysql_fetch_assoc($result);
            echo "<br /><b>Edit data:</b>  <br /><br />";
            while($row = mysql_fetch_assoc($result))
            {
            extract ($row);
            echo "
            <tr>
                <td><b>$uTitle</b></td>
                <td><input type='text' name='$ID' value='$uValue'></td>
                <td><input type='checkbox' name='$uTitle' value='yes'>
            </tr>";
            }
            ?>

            <tr><td><input type="submit" value="Update"></td>
            </tr>
        </table>
    </form>

 

this is all the code related to form. There can be 3 or more than 10 check boxes. It depends on how many titles user add. For each title there is one check box. Now I don't know how to save the value to the table so I can use it later to check which data user would like to lock.

I see the problem is that I don't receive the value of $_POST[$uTitle]. I tested with inserting echo statement. If I uncomment the 8th line it doesn't echo anything. I have no idea why it doesn't echo $_POST[$uTitle]. Here is also the image of table for better explanation.

table3.png

 

<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
         <table>
             <?php
             echo "
            <tr>
                <td>Title</td><td>Value</td><td>Privacy</td>
            </tr>";
             //echo $_POST[$uTitle];
             if (isset($_POST))  {
             foreach ($_POST as $key => $value )
                {
                mysql_query ("UPDATE titles SET uValue = '$value' WHERE ID ='$key'");
                }
                }
             $query =  "SELECT * FROM titles WHERE UserID = $ID";
             $result = mysql_query($query);
             $row = mysql_fetch_assoc($result);
             echo "<br /><b>Edit data:</b>  <br /><br />";
             while($row = mysql_fetch_assoc($result))
             {
             extract ($row);
             echo "
            <tr>
                <td><b>$uTitle</b></td>
                <td><input type='text' name='$ID' value='$uValue'></td>
                <td><input type='checkbox' name='$uTitle' value='yes'>
            </tr>";
             }
             ?>
             <tr><td><input type="submit" value="Update"></td>
             </tr>
         </table>
     </form>

I just solved the problem:

 

   if (isset($_POST))  {
            foreach ($_POST as $key => $value )
               {
               if ($value=='yes' || $value=='no') {
               mysql_query ("UPDATE titles SET privacy='$value' WHERE uTitle='$key'"); }
               else {
               mysql_query ("UPDATE titles SET uValue = '$value' WHERE ID ='$key'");
               }
               }
               }

 

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.