vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=15)
-   -   update all rows at the same time, same field, how? (https://vborg.vbsupport.ru/showthread.php?t=112931)

error_22 04-13-2006 05:20 PM

update all rows at the same time, same field, how?
 
Hi, I have a small problem. I made a form to update serveral id's at the same time. Im looking to update the same field.

PHP Code:

echo "<form action='page.php?action=order' method='POST'>";
$sql "SELECT * FROM `page` ORDER BY `order` DESC";
    
$result mysql_query($sql) or die(mysql_error());
    while (
$row mysql_fetch_object($result))
{
echo 
"<input type='text' name='order' value='$row->order'>";
}
echo 
"<input type='submit' value='save'></form>"

in page.php?action=order:
PHP Code:

$sql "UPDATE `page` SET order = '".$_POST['order']."'";
mysql_query($sql) or die(mysql_error()); 

Im clueless, any suggestions?

Thanks in advance
Niklas

sabret00the 04-14-2006 02:50 PM

what's the error?

error_22 04-14-2006 04:10 PM

right now there are 4 rows in that table, so the "order" field should be updated 4 times. Once for each row. If I use the above code, all rows get the same value, and I cant put in...
PHP Code:

WHERE `id` = '"$_GET['id']"' 

...since im not looking to update a single row. Im looking to update all rows.

That would be the error i guess...

Any suggestions?

Thanks for taking the time to answer! :)

/Niklas

filburt1 04-14-2006 05:02 PM

Without answering the problem, I'll say that you better wrap everything you put in the database with addslashes() lest you want a database injection exploit.

error_22 04-14-2006 06:55 PM

Can you show me what that would look like? Im sorry but I dont understand since this is all new to me.

Thanks

sabret00the 04-14-2006 09:55 PM

addslashes is crap use mysql_escape_string() http://dev.mysql.com/doc/refman/5.0/...pe-string.html

anyway:
PHP Code:

$sql "UPDATE 'page' SET order = '" $_POST['order'] . "' "

that looks right to me, unless you provide an error message, no one can help.

error_22 04-15-2006 01:44 PM

It is working, but now the way i want. I want it to work just like when you set the vB forum order. But thats not possible with my strings of code since all text fields have the same "name", and that gives all rows in the db the same value. for example:

<input type="text" name="order" value="1">
<input type="text" name="order" value="2">
<input type="text" name="order" value="3">
<input type="text" name="order" value="4">

once you hit the submit button the value "4" will be put in all rows in the database. instead of that, i want "1" to be put in the first row, "2" to be put in the second row and so on.....the question is how...

Thanks!

Code Monkey 04-15-2006 04:09 PM

You're creating multiple text fields in that loop and they all have the same name value.

error_22 04-15-2006 05:23 PM

Yes I'm aware of that. Now I'm looking for a solution.....

I tried the following:
PHP Code:

echo "<form action='page.php?action=order' method='POST'>"
$sql "SELECT * FROM `page` ORDER BY `id` DESC"
    
$result mysql_query($sql) or die(mysql_error()); 
    while (
$row mysql_fetch_object($result)) 

echo 
"<input type='text' name='order[$row->id]' value='$row->order'>"

echo 
"<input type='submit' value='save'></form>"

In page.php?action=order:
PHP Code:

    $sql "SELECT * FROM `page` ORDER BY `id` DESC";
    
$result mysql_query($sql) or die(mysql_error());
    while (
$row mysql_fetch_object($result))
    {
        
$insert $_POST["order[$row->id]"];
    
        
$sql "UPDATE `page` SET `order` = '$insert' ORDER BY `id` DESC";
        
mysql_query($sql) or die(mysql_error());
    } 

Sadly, it didnt work. Didnt think it would but it was worth a try atleast. For some reason, the mysql query can only store one value, and tha value is stored in every row.

error_22 04-17-2006 06:56 PM

Works, thanks for all the help!


All times are GMT. The time now is 11:39 AM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01893 seconds
  • Memory Usage 1,746KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (6)bbcode_php_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (10)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete