Printable Version of Topic

Click here to view this topic in its original format

HTMLHelp Forums _ Server-side Scripting _ upload multiple textboxes?

Posted by: xxkasperxx Jun 27 2012, 11:30 AM

How can i upload more than 1 text box into my database? i want to have it to where they are seperrate fields..

EX: |--------------------------------------------|
| FIELD 1.. so text box 1 goes here |
|--------------------------------------------|
|--------------------------------------------|
| FIELD 2.. so text box 2 goes here |
|--------------------------------------------|

But the user is going to be able to add more text boxes. It is for an announcements page.

So the big question is how do i upload the text-boxes into the correct field?

CODE
<?php
session_start();
include("config.php");
$query = mysql_query("SELECT * FROM home");
$query_row=mysql_fetch_array($query) or die (mysql_error());
if (isset($_SESSION['loggedin'])&&isset($_SESSION['username']))
{
echo "Welcome, " . $_SESSION['username'] . "!" . "<a href='logout.php'> Log-out!</a>";

?>
<!-- EDIT FOR CMS -->

<!DOCTYPE html>
<html>
<head>
<link href="css/main.css" type="text/css" rel="stylesheet" >
    <title>ConroeGoAway</title>
<script src="http://js.nicedit.com/nicEdit-latest.js" type="text/javascript"></script>
<script type="text/javascript">bkLib.onDomLoaded(nicEditors.allTextAreas);</script>

</head>

<body>
    <div id="wrapper">
        <div id="header">
            <ul>
                <li><a href="index.php" class="active">Home</a></li>
                <li><a href="articles.php">Articles</a></li>
                <li><a href="email.php">Email</a></li>
                <li><a href="suggestions.php">Suggestions</a></li>
                <li><a href="#">Blog</a></li>
        </div>
        
        <div id="picture">
            <img src="images/home.png"width="80%">
        </div>
        <div id="bgAnnouncments">
        <form action="" method="POST">
        <?php
        $counter =0;
        while($row = mysql_fetch_array($query)){
        $counter++;
            echo '<textarea id="text.$counter" name="text" rows="15" cols="80" style="width: 100%">';
                echo $row['content'];
            echo '</textarea>';
            
            
    
            }
        ?>
        <input type="submit" value="Submit" name="submit">
        </form>
        </div>
        <footer>
          <p>Copyright © 2012 ConroeGoAway</p>
        </footer>
        
    </div>
</body>

</html>
<?php
}else
{
?>

<!-- REGULAR -->
<!DOCTYPE html>
<html>
<head>
<link href="css/main.css" type="text/css" rel="stylesheet" >
    <title>ConroeGoAway</title>
</head>

<body>
    <div id="wrapper">
        <div id="header">
            <ul>
                <li><a href="index.php" class="active">Home</a></li>
                <li><a href="articles.php">Articles</a></li>
                <li><a href="email.php">Email</a></li>
                <li><a href="suggestions.php">Suggestions</a></li>
                <li><a href="#">Blog</a></li>
        </div>
        
        <div id="picture">
            <img src="images/home.png"width="80%">
        </div>
        <div id="bgAnnouncments">
        <?php
        while($row = mysql_fetch_array($query)){
            echo $row['content'];
            }
        ?>
        </div>
        <footer>
          <p>Copyright © 2012 ConroeGoAway</p>
        </footer>
        
    </div>
</body>

</html>
<?php
}

    if ($_POST)
    {
    /*
        foreach ($text as $text){
        $text = $_POST['text'];
        echo $text;
        echo "data is posted";
        if (isset($text))
        {
            echo 'text is set';
            $result = strip_tags(mysql_query("UPDATE home SET content = '$text'"));
            
            
        }
    }*/
    

        

    }
    
    
    
    


    

?>

Posted by: xxkasperxx Jun 28 2012, 05:36 PM

i have tryed to de-bug this... and it seems like it should work because it is getting the text from the textarea with the name of $counter...

CODE
<?php
session_start();
include("config.php");
$query = mysql_query("SELECT * FROM home");
$query_row=mysql_fetch_array($query) or die (mysql_error());
if (isset($_SESSION['loggedin'])&&isset($_SESSION['username']))
{
echo "Welcome, " . $_SESSION['username'] . "!" . "<a href='logout.php'> Log-out!</a>";

?>
<!-- EDIT FOR CMS -->

<!DOCTYPE html>
<html>
<head>
<link href="css/main.css" type="text/css" rel="stylesheet" >
    <title>ConroeGoAway</title>
<script src="http://js.nicedit.com/nicEdit-latest.js" type="text/javascript"></script>
<script type="text/javascript">bkLib.onDomLoaded(nicEditors.allTextAreas);</script>

</head>

<body>
    <div id="wrapper">
        <div id="header">
            <ul>
                <li><a href="index.php" class="active">Home</a></li>
                <li><a href="articles.php">Articles</a></li>
                <li><a href="email.php">Email</a></li>
                <li><a href="suggestions.php">Suggestions</a></li>
                <li><a href="#">Blog</a></li>
            </ul>
        </div>
        
        <div id="picture">
            <img src="images/home.png"width="80%">
        </div>
        <div id="bgAnnouncments">
        <form action="" method="POST">
        <?php
        $counter =1;
        while($row = mysql_fetch_array($query)){
        $counter++;
        
                
                    // IF WE DONT POST SOMTHING BRING UP THE OLD TEXT FROM DB
                    echo "<br>".$counter."<br>";
            echo '<textarea id="text.$counter" name="$counter" rows="15" cols="80" style="width: 100%">';
                echo $row['content'];
            echo '</textarea>';
            
            if ($_POST)
            {
    // DO UPDATE
                // need a while statement to say while the counter is lower than the id in DB then update..
                    
                    $tbNum = $row['id'];
                    echo "name: ". $counter ."<br>";
                    echo "id num: ".$tbNum ."<br>";
                    if ($counter==$tbNum)
                    {
                        echo "<br>Counter: ".$counter."<br>";
                        $text = $_POST['$counter'];
                        echo $text ." <br>";
                        echo $counter.$tbNum;
                    }
                
                    //$result = strip_tags(mysql_query("UPDATE home SET content = '$text' WHERE id = '$tbNum'"));
        
    
        }
            }
        ?>
            <input type="submit" value="Submit" name="submit">
        </form>
        </div>
        <footer>
          <p>Copyright © 2012 ConroeGoAway</p>
        </footer>
        
    </div>
</body>

</html>
<?php
}else
{
?>

<!-- REGULAR -->
<!DOCTYPE html>
<html>
<head>
<link href="css/main.css" type="text/css" rel="stylesheet" >
    <title>ConroeGoAway</title>
</head>

<body>
    <div id="wrapper">
        <div id="header">
            <ul>
                <li><a href="index.php" class="active">Home</a></li>
                <li><a href="articles.php">Articles</a></li>
                <li><a href="email.php">Email</a></li>
                <li><a href="suggestions.php">Suggestions</a></li>
                <li><a href="#">Blog</a></li>
            </ul>
        </div>
        
        <div id="picture">
            <img src="images/home.png"width="80%">
        </div>
        <div id="bgAnnouncments">
        <?php
        while($row = mysql_fetch_array($query)){
            echo $row['content'];
            }
        ?>
        </div>
        <footer>
          <p>Copyright © 2012 ConroeGoAway</p>
        </footer>
        
    </div>
</body>

</html>
<?php
}
?>

Posted by: Brian Chandler Jun 29 2012, 12:07 AM

QUOTE
i have tryed to de-bug this


That's a good start. (Though you should know 'I' is a capital, and tryed -> tried.)

But please don't just post the whole program and expect people to wade through working out what's what.

First thing I notice is that you are 'echo'ing the logon info before starting the html document ("<html>"). You shouldn't echo any text as part of the output page until you get to "<body>".

CODE

<form action="" method="POST">


Second: why does the form have no action?

Third: you are looping through outputting textareas, but giving them all the same name. This obviously won't work.

HTH

Posted by: xxkasperxx Jun 29 2012, 11:54 AM

The text areas are named "text" and have a number after them. and it starts from 2 and goes until there are no more. so the text boxes would be like "text2" "text3" but i don't get how i can take all of their text from each separate box, because the user will be adding more boxes. So i don't get how i can pull the text from each box, upload it then move onto another box.

Please help.

Posted by: xxkasperxx Jun 29 2012, 01:50 PM

QUOTE

Second: why does the form have no action?



Because it is being processed on the same page.

Posted by: Christian J Jun 29 2012, 02:02 PM

QUOTE(Brian Chandler @ Jun 29 2012, 07:07 AM) *

why does the form have no action?

If it submits to the same URL it shouldn't be necessary in HTML4, according to http://www.thefutureoftheweb.com/blog/use-empty-form-action-submit-to-current

But it appears HTML5 requires a non-empty value, even though the ACTION attribute itself appears to be optional:
"The action and formaction content attributes, if specified, must have a value that is a valid non-empty URL potentially surrounded by spaces."
http://www.w3.org/TR/html5/attributes-common-to-form-controls.html#attr-fs-formaction

Posted by: Christian J Jun 29 2012, 02:29 PM

QUOTE(xxkasperxx @ Jun 29 2012, 06:54 PM) *

The text areas are named "text" and have a number after them. and it starts from 2 and goes until there are no more. so the text boxes would be like "text2" "text3"

In your second example the NAME uses nothing but the $counter variable, which is just an integer:

CODE
echo '<textarea id="text.$counter" name="$counter"

Also note that variables are not expanded in single quoted strings: http://www.php.net/manual/en/language.types.string.php

Instead I believe you want something like this:

CODE
echo '<textarea id="text'.$counter.'" name="text'.$counter.'"


QUOTE
but i don't get how i can take all of their text from each separate box, because the user will be adding more boxes. So i don't get how i can pull the text from each box, upload it then move onto another box.

I'd use the square bracket method suggested here: http://www.php.net/manual/en/faq.html.php#faq.html.arrays

If you instead add a number after each NAME value, you'll need to let the form handling script increment the number values until the last one is found. Maybe you can use isset() to test that. unsure.gif

Posted by: xxkasperxx Jun 29 2012, 02:51 PM

It keeps give me a error message of

"Notice: Undefined index: text'.1.' in C:\wamp\www\ConroeGoAway\index.php on line 64"

and this is line 64

"$text = $_POST["text'.$counter.'"];"

I have no clue what to do.. To me it all makes sense because the names should match up.

Posted by: Christian J Jun 29 2012, 03:57 PM

CODE
"$text = $_POST["text'.$counter.'"];"

The above should be

CODE
$text = $_POST["text$counter"];

or
CODE
$text = $_POST['text'.$counter];

See the page I linked to on how variables interact with single- or double quote string syntax.

Don't forget to sanitize the SQL query with mysql_real_escape_string().

Posted by: xxkasperxx Jun 29 2012, 04:09 PM

So now I get to press submit with no errors, but it still wont change the data for each box individually.. it still does them as a whole(every box with the same content)

CODE

$counter =0;
        while($row = mysql_fetch_array($query)){
        $counter++;
        
                
                    // IF WE DONT POST SOMTHING BRING UP THE OLD TEXT FROM DB
                //    echo "<br>".$counter."<br>";
                echo $counter;
            echo '<textarea id="text'.$counter.'" name="text'.$counter.'" rows="15" cols="80" style="width: 100%">';
                echo $row['content'];
            echo '</textarea>';
            echo $counter;
            if ($_POST)
            {
    // DO UPDATE
                // need a while statement to say while the counter is lower than the id in DB then update..
                    
                    $tbNum = $row['id'];
                    echo "counter: ". $counter ."<br>";
                //    echo "id num: ".$tbNum ."<br>";
                    //$text = $_POST['text'.$counter];
                    if ($counter==$tbNum)
                    {
                //        echo "<br>Counter: ".$counter."<br>";
                
                        $text = $_POST["text$counter"];
                        echo $text ." <br>";
                        //echo $counter.$tbNum;
                        $result = strip_tags(mysql_query("UPDATE home SET content = '$text' WHERE $tbNum == $counter  "));
                    }
                
                    
        
    
        }
            }

Posted by: Brian Chandler Jun 30 2012, 01:44 AM

Sorry, I forgot about the empty action thing.

QUOTE(Christian J @ Jun 30 2012, 04:29 AM) *


Instead I believe you want something like this:

CODE
echo '<textarea id="text'.$counter.'" name="text'.$counter.'"



Yes. Or:
CODE
echo "<textarea id=\"text$counter\" name=\"text$counter\"...


Which way to go is the Really Big Decision of PHP programming happy.gif

QUOTE

QUOTE
but i don't get how i can take all of their text from each separate box, because the user will be adding more boxes. So i don't get how i can pull the text from each box, upload it then move onto another box.

I'd use the square bracket method suggested here: http://www.php.net/manual/en/faq.html.php#faq.html.arrays
If you instead add a number after each NAME value, you'll need to let the form handling script increment the number values until the last one is found. Maybe you can use isset() to test that. unsure.gif


The "square bracket trick" is a *very* ugly hack. Much neater and simpler to loop through all the $_POST variables. (imo)

Posted by: Ephraim F. Moya Jun 30 2012, 08:30 AM

How does the user add more text boxes?

The only way I can think of is to interact with the server for each new text box. That means that the server can count the text boxes before it sends another page.

The problem then goes away, doesn't it?

Another way is to allow the user to enter more than one 'text box's worth of data in one text box. Then the problem is to parse one text box worth of multiple textual info. This is a bad way. Every user will use a different mental pattern to make multi 'text box's pattern. This can turn into a parsing nightmare.

FWIW

Posted by: xxkasperxx Jun 30 2012, 12:30 PM

There is only one user, and there would be a textbox that is empty and they type their text in there and press upload. It will then send the info to the database with the insert function. And then work just like the other textboxes.

Posted by: Christian J Jun 30 2012, 01:43 PM

QUOTE(Brian Chandler @ Jun 30 2012, 08:44 AM) *

Which way to go is the Really Big Decision of PHP programming happy.gif

I prefer single quote syntax when there are more double quotes than variables in the string, otherwise double quote syntax. Occasionally I split a string so I can use different syntax for each part.

There's also Heredoc and Nowdoc syntaxes, but I haven't tried them yet.

QUOTE
The "square bracket trick" is a *very* ugly hack.

What's ugly and hacky with it? sad.gif

Posted by: Christian J Jun 30 2012, 01:44 PM

QUOTE(Ephraim F. Moya @ Jun 30 2012, 03:30 PM) *

How does the user add more text boxes?

The only way I can think of is to interact with the server for each new text box.

You can generate them with javascript as well.

In any case you may want to impose an upper limit, to prevent flooding.

Posted by: xxkasperxx Jun 30 2012, 02:47 PM

so i still havent figured out how to update what is in each textbox yet. i can update them. BUT it only takes the text from the last box.. how do i get it to take the text from each box and update it to the correct box?
This is what i have so far..

CODE

        $counter =0;
        while($row = mysql_fetch_array($query)){
        $counter++;
        
                
                    // IF WE DONT POST SOMTHING BRING UP THE OLD TEXT FROM DB
                //    echo "<br>".$counter."<br>";
                //echo $counter;
            echo '<textarea id="text'.$counter.'" name="text'.$counter.'" rows="15" cols="80" style="width: 100%">';
                echo $row['content'];
            echo '</textarea>';
            //echo $counter;
            if ($_POST)
            {
    // DO UPDATE
                // need a while statement to say while the counter is lower than the id in DB then update..
                    
                    $tbNum = $row['id'];
                    echo "counter: ". $counter ."<br>";
                    echo "id num: ".$tbNum ."<br>";
                    //$text = $_POST['text'.$counter];
                    if ($counter==$tbNum)
                    {
                //        echo "<br>Counter: ".$counter."<br>";
                
                        $text = $_POST["text$counter"];
                        echo $text ." <br>";
                        //echo $counter.$tbNum;
                        $result = strip_tags(mysql_query("UPDATE home SET content = '$text' WHERE '$tbNum' = '$counter'"));
                    }
                
                    
        
    
        }
            }

Posted by: Ephraim F. Moya Jun 30 2012, 03:29 PM

QUOTE(Christian J @ Jun 30 2012, 12:44 PM) *

QUOTE(Ephraim F. Moya @ Jun 30 2012, 03:30 PM) *

How does the user add more text boxes?

The only way I can think of is to interact with the server for each new text box.

You can generate them with javascript as well.

In any case you may want to impose an upper limit, to prevent flooding.


Either php or Javascript means that there's a computer involved. Then every textarea can have a unique name. That's all that's required.

Javascript SUCKS -- probably the WORST computer language ever devised.

Posted by: xxkasperxx Jun 30 2012, 05:48 PM

QUOTE(xxkasperxx @ Jun 30 2012, 02:47 PM) *

so i still havent figured out how to update what is in each textbox yet. i can update them. BUT it only takes the text from the last box.. how do i get it to take the text from each box and update it to the correct box?
This is what i have so far..

CODE

        $counter =0;
        while($row = mysql_fetch_array($query)){
        $counter++;
        
                
                    // IF WE DONT POST SOMTHING BRING UP THE OLD TEXT FROM DB
                //    echo "<br>".$counter."<br>";
                //echo $counter;
            echo '<textarea id="text'.$counter.'" name="text'.$counter.'" rows="15" cols="80" style="width: 100%">';
                echo $row['content'];
            echo '</textarea>';
            //echo $counter;
            if ($_POST)
            {
    // DO UPDATE
                // need a while statement to say while the counter is lower than the id in DB then update..
                    
                    $tbNum = $row['id'];
                    echo "counter: ". $counter ."<br>";
                    echo "id num: ".$tbNum ."<br>";
                    //$text = $_POST['text'.$counter];
                    if ($counter==$tbNum)
                    {
                //        echo "<br>Counter: ".$counter."<br>";
                
                        $text = $_POST["text$counter"];
                        echo $text ." <br>";
                        //echo $counter.$tbNum;
                        $result = strip_tags(mysql_query("UPDATE home SET content = '$text' WHERE '$tbNum' = '$counter'"));
                    }
                
                    
        
    
        }
            }


still havent figured it out

Posted by: Christian J Jul 1 2012, 03:14 PM

QUOTE(xxkasperxx @ Jul 1 2012, 12:48 AM) *

still havent figured it out

My SQL is a bit rusty so I can't help, sorry. However it sounds like the content of the TEXTAREAs are overwriting each other. Test if it happens already in the PHP or in the SQL query.

You may want to provide neater code examples, as a courtesy to others viewing it. For example, why include both these lines:

CODE
//    echo "<br>".$counter."<br>";
//echo $counter;

when one would be enough for debugging? Also the current line indentation of the code example is pretty confusing to follow.

And again, you need to sanitize the content sent to the DB to avoid SQL injection exploits. strip_tags() will not help with that.

Posted by: xxkasperxx Jul 2 2012, 09:03 AM

so i tryed to see where stuff was going wrong but i keep getting this error:

Undefined index: text'.1.' in C:\wamp\www\ConroeGoAway\index.php on line 69
Undefined index: text'.2.' in C:\wamp\www\ConroeGoAway\index.php on line 69

but when i echo stuff out with this code:

CODE
echo '<textarea id="text'.$counter.'" name="text'.$counter.'" rows="15" cols="80" style="width: 100%">';
                echo $row['content'];
            echo '</textarea>';
            //echo "counter: ".$counter;
            echo "text'.$counter.'<br>";
            if ($_POST)
            {
    // DO UPDATE
                // need a while statement to say while the counter is lower than the id in DB then update..

                    //echo $text.$counter;
                    $tbNum = $row['id'];
                    echo "counter: ". $counter ."<br>";
                    echo "id num: ".$tbNum ."<br>";
                    //$text = $_POST['text'.$counter];
                    if ($counter==$tbNum)
                    {
                //        echo "<br>Counter: ".$counter."<br>";
                        echo "text'.$counter.'";
                        $text = $_POST["text'.$counter.'"];
                        echo $text ." <br>";
                        //echo $counter.$tbNum;
                        $result = strip_tags(mysql_query("UPDATE home SET content = '$text' WHERE '$tbNum' = '$counter'"));
                    }
                
                    
        
    
        }


They match up. So why is my $text = $_POST["text'.$counter.'"]; not getting the text?
Please help

Posted by: Christian J Jul 2 2012, 12:35 PM

QUOTE
CODE
echo "text'.$counter.'";
$text = $_POST["text'.$counter.'"];

You're mixing up single- and double quote syntax again. Because of that, the above code returns e.g.

CODE
text'.1.'

instead of the
CODE
text1

you really want.

If a string begins with double quotes, single quotes inside it will not end the string, only another double quote will (unless it's escaped with a backslash).

With double quote syntax there's no need for the concatenation operator (the period sign), so you can simply write

CODE
"text$counter"

even though it's also correct to write something like

CODE
"text".$counter

Posted by: xxkasperxx Jul 2 2012, 12:43 PM

okay. But why does it still only take the last text box, and insert that for every box? I guess i dont know where it is overwriting the first text box. How would i figure that out?

Posted by: Ephraim F. Moya Jul 2 2012, 01:53 PM

QUOTE(xxkasperxx @ Jul 2 2012, 11:43 AM) *

okay. But why does it still only take the last text box, and insert that for every box? I guess i dont know where it is overwriting the first text box. How would i figure that out?


Because the names are not understandable.

Make the names text1, text2, text3, ... etc.

Not text.1, text.2, ... etc. or some variant.


BTW, How are you going to use this code multiple times? Your counter starts at 0.

Use the {$var} method in double quotes. Its more understandable.

Instead of "word'.$var2.'" say "word{$var2}" which will cleanly concatenate the two instead of using the .(dot) operator.


Posted by: xxkasperxx Jul 2 2012, 04:22 PM

the counter is increased each time it goes through..

CODE

$counter =1;
        while($row = mysql_fetch_array($query)){
        $counter++;

and i can not get it to work out with the text{$counter} method.

Posted by: Ephraim F. Moya Jul 3 2012, 09:58 AM

QUOTE(xxkasperxx @ Jul 2 2012, 03:22 PM) *

the counter is increased each time it goes through..

CODE

$counter =1;
        while($row = mysql_fetch_array($query)){
        $counter++;

and i can not get it to work out with the text{$counter} method.


There's something you're not telling us.

Do you have access to phpMyAdmin? If so use it to look at the database just after one use. This is very important to see whether the problem is in writing or reading the database.

Have you dumped $row to see if it is what you expect. use the print_r() instruction. Like this:
CODE

        while( $row = mysql_fetch_array( $query ))
        {
              echo '<pre>'; print_r( $row ); echo '</pre>';
              $counter++;
              ...
        }

The $counter may be incremented during the time the while() is running. But the next time you run the program $counter is set to 1 before the while() routine runs.

The "text{$counter}" construct works! It is your program that does not work.

What version of php are you running?

Posted by: xxkasperxx Jul 3 2012, 11:14 AM

QUOTE(Ephraim F. Moya @ Jul 3 2012, 09:58 AM) *



There's something you're not telling us.

Do you have access to phpMyAdmin? If so use it to look at the database just after one use. This is very important to see whether the problem is in writing or reading the database.

Yes i do.

QUOTE

Have you dumped $row to see if it is what you expect. use the print_r() instruction. Like this:
CODE

        while( $row = mysql_fetch_array( $query ))
        {
              echo '<pre>'; print_r( $row ); echo '</pre>';
              $counter++;
              ...
        }



I get this when i use what you have posted.

Array
(
[0] => 2
[id] => 2
[1] => Array
[content] => Array
)

Array
(
[0] => 3
[id] => 3
[1] => Array
[content] => Array
)

QUOTE

The $counter may be incremented during the time the while() is running. But the next time you run the program $counter is set to 1 before the while() routine runs.

The "text{$counter}" construct works! It is your program that does not work.

What version of php are you running?

I am running PHP Version 5.3.10

So how do I fix my program? I am so lost... Sorry

Posted by: Ephraim F. Moya Jul 3 2012, 12:50 PM

CODE

I get this when i use what you have posted.

Array
(
    [0] => 2
    [id] => 2
    [1] => Array
    [content] => Array
)

Array
(
    [0] => 3
    [id] => 3
    [1] => Array
    [content] => Array
)
    


Why does the ['id'] start with 2? It should start with 0. This happens in your write routine.

Also, where is the textarea name? The part that identifies what text you're saving in the ['content'] field.

Why is the ['content'] an array? This also happens when you write.

Check these by writing different ways until you get what you want. ['id'] starting with 0 and ['content'] being a text field not an array. Plus add a field for the ['name'].



Posted by: xxkasperxx Jul 3 2012, 07:04 PM

id is starting at 2 because thats what it starts at in the DB?

Posted by: Ephraim F. Moya Jul 3 2012, 07:24 PM

QUOTE(xxkasperxx @ Jul 3 2012, 06:04 PM) *

id is starting at 2 because thats what it starts at in the DB?


Where in the manual does it say that?

Posted by: xxkasperxx Jul 3 2012, 08:09 PM

it said array because tats waht was put in the DB for some reason. i changed the DB to say "12" and "qq" and this is what i got back

Array
(
[0] => 2
[id] => 2
[1] => 12
[content] => 12
)

Array
(
[0] => 3
[id] => 3
[1] => qq
[content] => qq
)

Posted by: Ephraim F. Moya Jul 3 2012, 09:14 PM

QUOTE(xxkasperxx @ Jul 3 2012, 07:09 PM) *

it said array because tats waht was put in the DB for some reason. i changed the DB to say "12" and "qq" and this is what i got back

Array
(
[0] => 2
[id] => 2
[1] => 12
[content] => 12
)

Array
(
[0] => 3
[id] => 3
[1] => qq
[content] => qq
)


QUOTE

it said array because tats waht was put in the DB for some reason.


Good! You've identified one of the problems I posed. Now identify the other two and You'll be a long way into debugging your write routines.

Posted by: xxkasperxx Jul 3 2012, 09:23 PM

How do i stop it from over writing? i cant figure that out

Posted by: Ephraim F. Moya Jul 3 2012, 11:13 PM

QUOTE(xxkasperxx @ Jul 3 2012, 08:23 PM) *

How do i stop it from over writing? i cant figure that out


You write the code. Write it so it doesn't overwrite.

Besides, how do you know it's overwriting?

Posted by: xxkasperxx Jul 4 2012, 01:55 AM

Because it shows the second text box as the same text inputted as the first.

Posted by: Ephraim F. Moya Jul 4 2012, 07:59 AM

QUOTE(xxkasperxx @ Jul 4 2012, 12:55 AM) *

Because it shows the second text box as the same text inputted as the first.


No, I meant what in your code is writing twice. hint: you have to RTFM! then debug.

You're assuming that your code is perfect. Even in the face of evidence to the contrary.

Posted by: xxkasperxx Jul 4 2012, 10:10 AM

It is not getting the second box's text. Because it shows the first box's text for both. So i have to figure out why and how to fix it. Correct?

Posted by: xxkasperxx Jul 4 2012, 11:31 AM

So this is what i have so far. Can you please help? I cant figure out how to get the counter to start at 1, when its outside of the while statement.

CODE
<?php
session_start();
include("config.php");
$query = mysql_query("SELECT * FROM home");
$query_row=mysql_fetch_array($query) or die (mysql_error());
    
    if($_POST['save'])
    {

            $counter = 1;
            $passorfail = false;
            foreach ( $_POST['mytext'] as $c=> $k)
            {
                while($row = mysql_fetch_array($query)){
                    $tbNum = $row['id'];
                    echo 'ID: '.$tbNum.'<br>';
                    //echo 'input text:'.$k.'<br>';
                    //echo 'exiting while statement<br>';
                    //echo 'K: '.$k.'<br>';
                    $counter++;
                    echo 'Counter:'.$counter.'<br>';
                }
            
            
                
                //echo 'input text: '.implode($_POST['mytext']).'<br>';
                echo '<br>input text:'.$k.'<br>';
                
                //echo '<pre>'; print_r( $row ); echo '</pre>';
                 //echo "its on<br>";
                
                //execute the sql
                // set allow = true where id = $c
                $allow = "false";
                //echo "ID: ".$tbNum."<br>";
                $text = implode($_POST['mytext']);
            
                //echo '<br>they are equal<br>';
                //$text = $row['id'];
                //echo "".$text."";
                //echo "$c";
                
                echo 'Counter: '.$counter.'<br>';
                $result = strip_tags(mysql_query("UPDATE home SET content = '$k' WHERE id=$counter"));
            
                
                $passorfail = true;
                
                if ($passorfail==true)
                {
                    //echo "passed";
                }
                
                if ($passorfail=false)
                {
                        echo "Error: Please contact levi at leviwurtz2622@yahoo.com. Thank you!";
                }
                
            
                
                
            //}
            }
    }
    
?>

This prints out:

ID: 2
Counter:2
ID: 3
Counter:3
ID: 4
Counter:4
ID: 5
Counter:5

input text:test2

Counter: 5

input text:test1

Counter: 5

input text:test3

Counter: 5

input text:test4

Counter: 5

But if i put everything inside of the while statement it prints out:

ID: 2
Counter:2

input text:test2

Counter: 2
ID: 3
Counter:3

input text:test2

Counter: 3
ID: 4
Counter:4

input text:test2

Counter: 4
ID: 5
Counter:5

input text:test2

Counter: 5


Every text box is filled with text and then the number box it is.. Ex textbox 1 has "test1" textbox 2 = "test2"

I feel like im getting closer. What do i need to finish it off? Or am i way off from what i want to accomplish?

Posted by: xxkasperxx Jul 4 2012, 11:47 AM

I got it to work!!!!!!!!!!!!!!!!!!!!!

This is my code:

CODE
<?php
session_start();
include("config.php");
$query = mysql_query("SELECT * FROM home");
$query_row=mysql_fetch_array($query) or die (mysql_error());
    
    if($_POST['save'])
    {

            $counter = 1;
            $passorfail = false;
            foreach ( $_POST['mytext'] as $c=> $k)
            {
                while($row = mysql_fetch_array($query)){
                    $tbNum = $row['id'];
                    echo 'ID: '.$tbNum.'<br>';
                    //echo 'input text:'.$k.'<br>';
                    //echo 'exiting while statement<br>';
                    //echo 'K: '.$k.'<br>';
                    $counter++;
                    echo 'Counter:'.$counter.'<br>';
                }
            
                
                
                //echo 'input text: '.implode($_POST['mytext']).'<br>';
                echo '<br>input text:'.$k.'<br>';    // Outputs all text like it should.
                
                //echo '<pre>'; print_r( $row ); echo '</pre>';
                 //echo "its on<br>";
                
                //execute the sql
                // set allow = true where id = $c
                $allow = "false";
                //echo "ID: ".$tbNum."<br>";
                $text = implode($_POST['mytext']);    // ARRAY of text
            
                //echo '<br>they are equal<br>';
                //$text = $row['id'];
                //echo "".$text."";
                //echo "$c";
                
                
                

        
                $result = strip_tags(mysql_query("UPDATE home SET content = '$k' WHERE id=$counter"));
                    
                
                $passorfail = true;
                
                if ($passorfail==true)
                {
                    //echo "passed";
                }
                
                if ($passorfail=false)
                {
                        echo "Error: Please contact levi at leviwurtz2622@yahoo.com. Thank you!";
                }
                
            
                $counter++;
                    echo 'Counter: '.$counter.'<br>';

                
            //}
            }
    }
    
?>


Tell me if there is anything I can fix to increase security, or make it work a little more efficient?

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)