Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help with while() loop to update database

Status
Not open for further replies.

lentildal

Programmer
Sep 24, 2001
25
GB
hi guys

im trying to build a page which will allow me to update a table in my mysql database.

the table has two columns bbml_id (key auto increment integer) and bbml_email (text).

i have created a form which lists each email inside an input text field with a checkbox next to it.

it looks like this:

Code:
// set connection variables
***blah

// connect and select database
***blah

// make da query
$SQL="SELECT * FROM $table_name ORDER BY bbml_id";
$result=@mysql_query($SQL, $connection)
	or die("Couldn't execute query.");
$num_rows=mysql_num_rows($result);

// build a table
print(&quot;<table cellspacing='0' cellpadding='0'><form method='POST' action='delete.php'>&quot;);
for ($a=0; $a < $num_rows; $a++)
{
$array=mysql_fetch_array($result);
print(&quot;<tr><td bgcolor='#FF99FF'>&quot;);
$add_num=$array['bbml_email'];
print(&quot;<input name='eadd['.$add_num.']' size='30' maxlength='70' value='&quot;.$array['bbml_email'].&quot;'>&quot;);
print(&quot;</td><td bgcolor='#FF99FF'>&quot;);
$chck_num=$array['bbml_id'];
print('<input type=&quot;checkbox&quot; name=&quot;echck['.$chck_num.']&quot; value=&quot;none&quot;>');
print(&quot;</td></tr>&quot;);
}
print(&quot;<tr><td><br><input type='submit' value='amend' name='amend'><input type='submit' value='delete' name='delete'></td></tr></form></table>&quot;);

submissions from this form go to the following script:

Code:
// set connection variables
***blah

// connect and select database
***blah


//AMEND ***********************************************
if(isset($HTTP_POST_VARS['amend'])){

//as long as we have elements in our array
while(list($key)=each($HTTP_POST_VARS['echck']))
{
    $SQL.=&quot; or bbml_id=&quot; .$key;
}

//lets cut the first ' or' part
$SQL=substr($SQL, 4);

//lets complete the query
$SQL=&quot;UPDATE LOW_PRIORITY $table_name set bbml_email='&quot;.$HTTP_POST_VARS['eadd'].&quot;' where &quot;. $SQL;

$result=@mysql_query($SQL, $connection)
	or die(&quot;Couldn't execute query because:&quot;.mysql_error());

}

//DELETE ***********************************************
elseif(isset($HTTP_POST_VARS['delete'])){

//as long as we have elements in our array
while(list($key)=each($HTTP_POST_VARS['echck']))
{
    $SQL.=&quot; or bbml_id=&quot; .$key;
}

//lets cut the first ' or' part
$SQL=substr($SQL, 4);

//lets complete the query
$SQL=&quot;DELETE from $table_name where &quot;.$SQL;

$result=@mysql_query($SQL, $connection)
	or die(&quot;Couldn't execute query because:&quot;.mysql_error());
}

i built this code with the help of other people and patching together other scripts that i had. its not quite working at the moment and i dont really have the knowledge to fix it.

the delete part of the script works fine, the problem is with the amend part.

basically the eadd variable from the first script never seems to arrive at the second. i think the problem is that the array is not created peoperly somehow, but im not sure i dont know php well enough to debug this thing ....

also, lets assume that we managed to get the eadd variable into the second script, it somehow needs to go into that while() loop so that i can amend multiple entries at the same time.

if anyone could give me any help i would be most grateful - please bear in mind that im a newbie and need things explained well X-) thanks .

Lentil

give me all your lentils
 
This thing works? is my first question.... i see a lot of errors in the coding.

Example:
The output of this line:
print(&quot;<input name='eadd['.$add_num.']' size='30' maxlength='70' value='&quot;.$array['bbml_email'].&quot;'>&quot;);

should be
<input name='eadd['john@domain.com']' ... and not what you mean to ...

it will put the var only as eadd[ cause the name closes in the second single-quote.

To so some debugging, go to your script (the action one) and in the if that is not working, put something like this:

echo &quot;<h1>HELLO</h1>&quot;;

if you see the Hello string you should be inside the &quot;then&quot; and so you can print some values ... etc.


Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
hey anikin, thanks for your reply.

i see what you mean about the quote marks. that is my first problem. i dont seem to be able to get the syntax right....

i didnt quite understand what you meant when you said :

should be
<input name='eadd['john@domain.com']' ... and not what you mean to ...

that email needs to be created dynamically, that is why i was trying to call it from an array like i did for the echck variable. if im making my form using a for loop how can i write each email address in there seperately?

or am i on the wrong tracks here?

it doesnt even need to be an email address, i suppose i could use the bbml_id column from my table as i did for the echck variable. its just to identify which text to amend.

give me all your lentils
 
ok anikin, ive reworked the code a bit in the first script:

Code:
// set connection variables
$connection = @mysql_connect(&quot;$sqlhost&quot;, &quot;$db_user&quot;, &quot;$db_password&quot;)
or die(&quot;Couldn't connect.&quot;);

// connect and select database
$db = @mysql_select_db ($db_name, $connection)
or die(&quot;Couldn't select database.&quot;);

// make da query
$SQL=&quot;SELECT * FROM $table_name ORDER BY bbml_id&quot;;
$result=@mysql_query($SQL, $connection)
	or die(&quot;Couldn't execute query.&quot;);
$num_rows=mysql_num_rows($result);

// build a table
print(&quot;<table cellspacing='0' cellpadding='0'><form method='POST' action='test2.php'>&quot;);
for ($a=0; $a < $num_rows; $a++)
{
$array=mysql_fetch_array($result);
print(&quot;<tr><td bgcolor='#FF99FF'>&quot;);
$add_num=$array['bbml_email'];
print(&quot;<input name=eadd['$add_num']&quot;);
print(&quot;size=30 maxlength=70 value='$add_num'>&quot;);
print(&quot;</td><td bgcolor='#FF99FF'>&quot;);
$chck_num=$array['bbml_id'];
print('<input type=&quot;checkbox&quot; name=&quot;echck['.$chck_num.']&quot; value=&quot;none&quot;>');
print(&quot;</td></tr>&quot;);
}
print(&quot;<tr><td><br><input type='submit' value='amend' name='amend'><input type='submit' value='delete' name='delete'></td></tr></form></table>&quot;);

that bit is ok now i think.

now i just need to get the array for the emails into a loop in the second script and create an update command that will update more than one entry at a time.

if anyone has any ideas please let me know.

thanks

give me all your lentils
 
I'll try to help you out

for what i saw in the code, it should be something like this...

This is the code:

// set connection variables
$connection = @mysql_connect(&quot;$sqlhost&quot;, &quot;$db_user&quot;, &quot;$db_password&quot;)
or die(&quot;Couldn't connect.&quot;);

// connect and select database
$db = @mysql_select_db ($db_name, $connection)
or die(&quot;Couldn't select database.&quot;);

// make da query
$SQL=&quot;SELECT * FROM $table_name ORDER BY bbml_id&quot;;
$result=@mysql_query($SQL, $connection)
or die(&quot;Couldn't execute query.&quot;);
$num_rows=mysql_num_rows($result);

// build a table
?>
<table cellspacing='0' cellpadding='0'>
<form method='POST' action='test2.php'>
<?
for ($a=0; $a < $num_rows; $a++)
{
$array=mysql_fetch_array($result);
$add_num=$array['bbml_email'];
?>
<tr>
<td bgcolor='#FF99FF'>&quot;
<input name=eadd[] size=30 maxlength=70 value=&quot;<?=$add_num?>&quot;><input type=hidden name=email_addr[] value=<?=$add_num?>>
</td>
<td bgcolor='#FF99FF'>
<?$chck_num=$array['bbml_id'];?>
<input type=&quot;checkbox&quot; name=&quot;echck[]&quot; value=&quot;<?=$chck_num?>&quot;>
</td>
</tr>
<?
} //end of for
?>
<tr>
<td>
<br><input type='submit' value='amend' name='amend'><input type='submit' value='delete' name='delete'>
</td>
</tr>
</form>
</table>





Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
hi anikin, thanks for taking the time to look at my problem. i appreciate it.

my table is like this:

two columns. one called bbml_id (int auto increment primary key) and another called bbml_email (varchar unique)
there are 12 entries in my table.

i used your code, but im afraid it doesnt work.

i added an echo:

echo $query;

just so i could see what it was doing. it outputs this code:

UPDATE SET bbml_email='' WHERE bbml_id=11

no value for bbml_email and bbml_id is always 11 no matter which checkboxes are clicked or even if there is more than one. as i said before there are 12 entries in my table but none of them has bbml_id equal to 11 so im not sure where that number comes from.

thanks for your help
give me all your lentils
 
Stupid me ... of course ... wrong name in a var ...

Replace this:
$email_array=$HTTP_POST_VARS['echck'];
by
$email_array=$HTTP_POST_VARS['eadd'];


Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
hi anikin

i made that change, now the output looks like this:

UPDATE SET bbml_email='test@test.com' WHERE bbml_id=11

test@test.com is the last entry in my table.

it gives this result no matter which checkbox is checked. or even if more than one is checked. same with the number 11. i dont even have a number 11 in my table. there are twelve entries in my table and none of the IDs is 11. like i said before i dont know how this number is being generated.

any ideas?

give me all your lentils
 
first ... the name of the table is missing, cause i don't know it ... i put a var named $table for that.

I'm sorry ... i feel like i was still sleeping when i wrote the code ...

ok only selected are going to be changed?

Easy ... (check var names. It's not yours, are mine)

let's do some changes in the form ...
<input type=hidden name=email_addr[] value=<?=$add_num?>>
changes to:
<input type=hidden name=id[] value=<?=$id?>>

in the update:
$email_array=$HTTP_POST_VARS['echck'];
while(list($index,$id)=each($HTTP_POST_VARS[id])){
if (in_array($email_array,$id)){
$query=&quot;UPDATE $table SET email='$email_array[$index]' WHERE email=$index&quot;;
}
}

Test this changes, please Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
hi anikin

i made those changes.

i get:

Warning: Wrong datatype for first argument in call to in_array() in /**my URL**/test4.php on line 23

x12 - one for every entry in the table i suppose...

should:

$email_array=$HTTP_POST_VARS['echck'];

be echck again or should i change that to eadd?
give me all your lentils
 
ok ... swap the arguments of the call to the in_array function.

that line you should let restless, but you need another var

$email_add=$HTTP_POST_VARS[eadd];

and in the UPDATE,

email=$email_add[$index];

Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
hmmm, now it says the document contains no data!

this is what the first script looks like so far

<table cellspacing='0' cellpadding='0'>
<form method='POST' action='test4.php'>

<?
for ($a=0; $a < $num_rows; $a++)
{
$array=mysql_fetch_array($result);
$add_num=$array['bbml_email'];
?>

<tr>
<td bgcolor='#FF99FF'>
<input name=eadd[] size=30 maxlength=70 value=&quot;<?=$add_num?>&quot;><input type=hidden name=id[] value=<?=$id?>>
</td>
<td bgcolor='#FF99FF'>

<?$chck_num=$array['bbml_id'];?>

<input type=&quot;checkbox&quot; name=&quot;echck[]&quot; value=&quot;<?=$chck_num?>&quot;>
</td>
</tr>

<?
} //end of for
?>

<tr>
<td>
<br><input type='submit' value='amend' name='amend'><input type='submit'
value='delete' name='delete'>
</td>
</tr>
</form>
</table>



this is how the 2nd script looks now



//AMEND ***********************************************
if(isset($HTTP_POST_VARS['amend'])){

$email_array=$HTTP_POST_VARS['echck'];
$email_add=$HTTP_POST_VARS['eadd'];
while(list($index,$id)=each($HTTP_POST_VARS[id])){
if (in_array($id, $email_array)){
$query=&quot;UPDATE $table_nammeee SET bbml_email='$email_array[$index]' WHERE
bbml_email=$email_add[$index]&quot;;
}
}

}

did i copy it right?
give me all your lentils
 
This:
bbml_email=$email_add[$index]&quot;;
change to
bbml_id=$email_add[$index]&quot;;


This piece of code is missing, is it in the script?
// set connection variables
$connection = @mysql_connect(&quot;$sqlhost&quot;, &quot;$db_user&quot;, &quot;$db_password&quot;)
or die(&quot;Couldn't connect.&quot;);

// connect and select database
$db = @mysql_select_db ($db_name, $connection)
or die(&quot;Couldn't select database.&quot;);

// make da query
$SQL=&quot;SELECT * FROM $table_name ORDER BY bbml_id&quot;;
$result=@mysql_query($SQL, $connection)
or die(&quot;Couldn't execute query.&quot;);
$num_rows=mysql_num_rows($result);


I'm leaving work right now, but i'll reply and i check everything from there.
Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
hi anikin

my stupid webserver has gone down now! damnit >:-<

i think your script will work now, but i cant test it. as soon as my server is back online i will test it and post the results here.


give me all your lentils
 
hi anikin

i changed those things but it still says that the document contains no data....

any idea why that might be?
give me all your lentils
 
hi, just for your reference, here are both scripts (everything included!)

first script:


<?php
// database variables
$sqlhost = &quot;localhost&quot;;
$db_user = &quot;username&quot;;
$db_password = &quot;password&quot;;
$db_name = &quot;dbname&quot;;
$table_name = &quot;tablename&quot;;

// set connection variables
$connection = @mysql_connect(&quot;$sqlhost&quot;, &quot;$db_user&quot;, &quot;$db_password&quot;)
or die(&quot;Couldn't connect.&quot;);

// connect and select database
$db = @mysql_select_db ($db_name, $connection)
or die(&quot;Couldn't select database.&quot;);

// make da query
$SQL=&quot;SELECT * FROM $table_name ORDER BY bbml_id&quot;;
$result=@mysql_query($SQL, $connection)
or die(&quot;Couldn't execute query.&quot;);
$num_rows=mysql_num_rows($result);

// build a table
?>

<table cellspacing='0' cellpadding='0'>
<form method='POST' action='test4.php'>

<?
for ($a=0; $a < $num_rows; $a++)
{
$array=mysql_fetch_array($result);
$add_num=$array['bbml_email'];
?>

<tr>
<td bgcolor='#FF99FF'>
<input name=eadd[] size=30 maxlength=70 value=&quot;<?=$add_num?>&quot;><input type=hidden name=id[] value=<?=$id?>>
</td>
<td bgcolor='#FF99FF'>

<?$chck_num=$array['bbml_id'];?>

<input type=&quot;checkbox&quot; name=&quot;echck[]&quot; value=&quot;<?=$chck_num?>&quot;>
</td>
</tr>

<?
} //end of for
?>

<tr>
<td>
<br><input type='submit' value='amend' name='amend'><input type='submit'
value='delete' name='delete'>
</td>
</tr>
</form>
</table>


second script


<?php
// database variables
$sqlhost = &quot;localhost&quot;;
$db_user = &quot;username&quot;;
$db_password = &quot;password&quot;;
$db_name = &quot;dbname&quot;;
$table_name = &quot;tablename&quot;;

// set connection variables
$connection = @mysql_connect(&quot;$sqlhost&quot;, &quot;$db_user&quot;, &quot;$db_password&quot;)
or die(&quot;Couldn't connect.&quot;);

// connect and select database
$db = @mysql_select_db ($db_name, $connection)
or die(&quot;Couldn't select database.&quot;);

//AMEND ***********************************************
if(isset($HTTP_POST_VARS['amend'])){

$email_array=$HTTP_POST_VARS['echck'];
$email_add=$HTTP_POST_VARS['eadd'];
while(list($index,$id)=each($HTTP_POST_VARS[id])){
if (in_array($id, $email_array)){
$query=&quot;UPDATE $table_name SET bbml_email='$email_array[$index]' WHERE
bbml_id=$email_add[$index]&quot;;
}
}
echo $id;
echo $index;
echo $HTTP_POST_VARS['eadd'];
echo $HTTP_POST_VARS[id];
echo $query;
}

?>
give me all your lentils
 
hi anikin

i got it working! thank you so much for all your help. you have been very generous with your time.

thanks thanks thanks thanks thanks thanks thanks thanks

:-D
give me all your lentils
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top