Jump to content

any easy way to list UPDATE columns


phppup

Recommended Posts

  • Replies 52
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

http://www.phpfreaks.com/forums/index.php?topic=354422.msg1674005#msg1674005

 

Yes, there's a way to loop through them all without listing them separately.

 

First of all though, avoid manually creating variables that only hold the value of another.

$roastturkey = $_POST['roastturkey'];

Has no benefit whatsoever other than aesthetics.  Gee, now I only have to type $broccoli instead of $_POST['broccoli']... how l337!!

That is a pointless concept unless you have calculations involved.  You wouldn't pour your coffee from your coffee cup into another coffee cup just to add cream to it would you.... and then again for sugar?

 

To loop through your inputs, you need to know two things.

1 - Do you want everything in $_POST?

2 - What don't you need from $_POST?

... Yes I realize those are the same, but a little repetition never hurt anyone.

 

Using a foreach loop, you can grab EVERYTHING from $_POST and append it to a variable.  For example, assuming you wanted EVERYTHING inside $_POST

$myUpdateList = array();
foreach($_POST as $indexName => $value) {
     $myUpdateList[] = "$indexName = \"$value\"";
}

This will yeild an array containing all of your fields and their new values..Everything in that array will look something like this

broccoli = "burnt"

 

Now, the task is to take that array and implode it into a comma separated list..

$fields = implode(",", $myUpdateList);

 

All that's left is the actual UPDATE statement now... and you have everything you need for it inside $fields

UPDATE yourTable SET $fields WHERE id = $id

 

Hope this helps

 

Link to comment
Share on other sites

well, I'm glad you're here, because I'm doing something wrong.  I posted it as written:

$testtable = array();foreach($_POST as $indexName => $value) {    $myUpdateList[] = "$indexName = \"$value\"";}

$fields = implode(",", $testtable);

$sql=("UPDATE testtable SET $fields WHERE id = $record_id ");

 

but I was getting a T-string error

Link to comment
Share on other sites

The only way there could be a T_STRING error is if you have quotes/apostrophes in your input.  Otherwise, I have no idea why there is a T_STRING error.  The double quotes are escaped just fine.

 

If you do have apostrophes in your inputs, then take a look at the addslashes function... that should fix it.

 

 

ALSO, you are not populating your $testtable array.  So the $fields variable holds literally nothing.

replace $myUpdateList with $testtable

Link to comment
Share on other sites

Got rid of the T-string error.  There are no MySQL errors, but the page takes me to MY own ERROR message rather than SUCCESS after submission.  SO it's missing something.

I changed the table name to MY testtable name.  Are all others AS IS?

Post the code that you have..  My crystal ball is broken :psychic:

Link to comment
Share on other sites

if(isset($_POST['action']) && $_POST['action'] == 'submitform')
{
//recieve the variables

$roastturkey = $_POST['roastturkey'];
$broccoli = $_POST['broccoli'];
$brisket = $_POST['brisket'];
$carrots = $_POST['carrots'];       

$record_id = (isset($_POST['record_id'])) ? $_POST['record_id'] : '';
if(!empty($record_id)){



$testtable = array();foreach($_POST as $indexName => $value) {     $myUpdateList[] = "$indexName = \"$value\"";}
$fields = implode(",", $testtable);
$sql=("UPDATE testtable SET $fields WHERE id = $record_id ");


$result=mysql_query($sql);				

}

if($result){
echo "Successful";

}

else {
echo "ERROR";
}

EDITed for CODE tags.

Link to comment
Share on other sites

1.) You did not do as I suggested earlier

you are not populating your $testtable array.  So the $fields variable holds literally nothing.

replace $myUpdateList with $testtable

$testtable = array();foreach($_POST as $indexName => $value) {     $myUpdateList[] = "$indexName = \"$value\"";}

2.) You receive YOUR "ERROR" message because the query failed..

3.) Why is your SQL query inside parenthesis?

4.) I'll bet you all of my life savings that your query failed because you used every single element in your POST array

 

 

Put this code underneath your $sql line... and post what it returns here

echo "", print_r($_POST), "";

 

While you're at it....

echo your $sql variable and post that here as well.

Link to comment
Share on other sites

AFTER CONNECTION:

 

//save the data on the DB and send the email

if(isset($_POST['action']) && $_POST['action'] == 'submitform')
{
//recieve the variables

$roastturkey = $_POST['roastturkey'];
$broccoli = $_POST['broccoli'];
$brisket = $_POST['brisket'];
$carrots = $_POST['carrots'];   }


$record_id = (isset($_POST['record_id'])) ? $_POST['record_id'] : '';//check for $record_id emptiness
if(!empty($record_id)){

echo "<pre>", print_r($_POST), "</pre>";


$testtable = array();foreach($_POST as $indexName => $value) {     $testtable[] = "$indexName = \"$value\"";}
$fields = implode(",", $testtable);
$sql=("UPDATE testtable SET $fields WHERE id = $record_id ");

echo "<pre>", print_r($_POST), "</pre";

echo "$sql";

$result=mysql_query($sql);	

}
if($result){
echo "Successful";
}

else {
echo "ERROR";
}

 

 

Link to comment
Share on other sites

Ok, so you have the snippet in there... albeit in the right spot.  I can't begin to fathom why it doesn't output ANYTHING.. This is beyond me now.

 

One thing you can try it to put this

error_reporting(448191);
ini_set("display_errors", true);

at the VERY TOP of your script... RIGHT below the <?php tag

Link to comment
Share on other sites

OKAY, I pulled out the conditional and it printed:

Array

(

    [roastturkey] => 2.00

    [broccoli] => 0.00

    [brisket] => 6.00

    [carrots] => 0.00

    [submitpass] => Submit Order

)

1

 

so now the question is, How will it know which row ID to post the new data to?

Link to comment
Share on other sites

Exactly. How will it know if you don't tell it?

 

Oh, and what is the purpose of this? It appears to do precisely nothing.

//recieve the variables
$roastturkey = $_POST['roastturkey'];
$broccoli = $_POST['broccoli'];
$brisket = $_POST['brisket'];
$carrots = $_POST['carrots'];

 

And the closing curly brace that immediately follows that code block should be moved to the end; after the query execution.

Link to comment
Share on other sites

Seems like what you are trying to do is being over complicated by creating an array from a post array and I see no filters for record_id or action posted values.  This is assuming of course that there are not any other hidden values being passed with POST and all form names match table field names.

<?php
if(isset($_POST['action']) && $_POST['action']=="submitform"){
//recieve the variables
$roastturkey = $_POST['roastturkey'];
$broccoli = $_POST['broccoli'];
$brisket = $_POST['brisket'];
$carrots = $_POST['carrots'];       

if(isset($_POST['record_id']) && !empty($_POST['record_id'])){
$record_id=$_POST['record_id'];
foreach($_POST as $indexName => $value){
if ($indexName!="record_id" && $indexName!="action"){
$sql=("UPDATE testtable SET $indexName='$value' WHERE id=$record_id");
$result=mysql_query($sql);				
}//if ($indexName!="record_id" && $indexName!="action")
}//foreach($_POST as $indexName => $value)
}//if(isset($_POST['record_id']) && !empty($_POST['record_id']))
}//if(isset($_POST['action']) && $_POST['action']=="submitform")
?>

Link to comment
Share on other sites

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.