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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple field update???

Status
Not open for further replies.

bigbird3156

Programmer
Feb 20, 2001
183
0
0
AU
Hi...

If I pass a variable from another page and want it to replace the contents of a field from the database is there a simple way to do it???...

what would it look like??

i.e I want the contents of the variable "var2" to replace the contents of "$row_Recordset1['prod_cat']" automatically...

The idea is that there will be multiple records with the same field needing to be replaced and I want to repeat the function on the one page until all have been updated...

All I need here is the replace code the rest I have under control

The Bird from Down Under- Bigbird 3156 [upsidedown]
 
assuming that the variable is passed in the query string

Code:
//after data seek
$row_Recordset1['prod_cat'] = empty($_GET['var2']) ? $row_Recordset1['prod_cat'] : $_GET['var2'];
 
I can't get this to work...

maybe it is the setup of the rest of the page... can you have a quick look...

Code:
<body>
<p>Changing category <?php echo $_GET['val1']?> to <?php echo $_GET['val2']?></p>
<p>Updated Records</p>

<?php do { ?>
  <table width="650" border="0" align="center">
    <tr>
      <td width="50" align="left"><?php echo $row_Recordset1['prod_num']; ?></td>
      <td width="174" align="left"><?php echo $row_Recordset1['prod_name']; ?></td>
      <td width="188" align="left"><?php echo $row_Recordset1['designer']; ?></td>
      <td width="119" align="left"><?php $row_Recordset1['prod_cat'] = empty($_GET['var2']) ? $row_Recordset1['prod_cat'] : $_GET['var2']; ?>Updated!</td>
    </tr>
  </table>
  <?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>

The Bird from Down Under- Bigbird 3156 [upsidedown]
 
your do ... while loop is inverted. and you are using a return function in a write context. and your table tags are inside the loop.

Code:
<?php 
if (mysql_num_rows($Recordset1) > 0){
	$data = '';
	while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)){
		$row_Recordset1['prod_cat'] = empty($_GET['var2']) ? $row_Recordset1['prod_cat'] : $_GET['var2']; 
		$data .= <<<HTML
		
	<tr>
      <td width="50" align="left">{$row_Recordset1['prod_num']}</td>
      <td width="174" align="left">{$row_Recordset1['prod_name']}</td>
      <td width="188" align="left">{$row_Recordset1['designer']}</td>
      <td width="119" align="left">{$row_Recordset1['prod_cat']} Updated!</td>
    </tr>

HTML;
	}	//end while
	echo <<<HTML
<table width="650" border="0" align="center">
	<tbody>
	$data
	</tbody>    
</table>
HTML;
} else { 	//no records returned
	echo "No records were updated";
}
 
Thanks for that Jpadie...

I tried your code and it did not work...

firstly it did not loop, only showing the first record and secondly it did not actually update the record it showed either...

I assume that the
Code:
{$row_Recordset1['prod_cat']} Updated!

inside the frame should display the updated prod_cat however it does not nor does it actually update...


The Bird from Down Under- Bigbird 3156 [upsidedown]
 
are you intending to update the actual database with this code? this is data RETRIEVAL scripting, not data manipulation. changing a value in a retrieved does not change the actual recordset. to do that you must execute SQL UPDATE statements.

the table is created inside a while loop. if there is only one record shown then only one record is retrieved by your sql code.

if the text ' ... Updated!' does not display any prefix it is because there is no data within $row_Recordset1['prod_cat'] retrieved from the database.

 
Ok, thanks for that... I have another look into it and see if i can work it out before I ask for the inevitable help...

having said that however I did choose a category that had multiple records associated with it ... yet only got the one record actually showing up

The Bird from Down Under- Bigbird 3156 [upsidedown]
 
My apologies... the loop does work however it misses 1 record... I was using a category that had only 2 records and only 1 was coming up - but I tried again with a cat that had 36 records and 35 came up...

The Bird from Down Under- Bigbird 3156 [upsidedown]
 
that cannot happen. unless you are using a do while loop rather than a while loop.
 
OK... so I need to change the following line of code...

Code:
while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)){
        $row_Recordset1['prod_cat'] = empty($_GET['var2']) ? $row_Recordset1['prod_cat'] : $_GET['var2'];

to something with an UPDATE command in it... however I am struggling to make the passed variables work in this... I have written it like this...
Code:
mysql_query("UPDATE products SET prod_cat = $_GET['val2'] WHERE prod_cat = $_GET['val1']");

which obviously does not work... but how do I make it work???

If I change it to something like...
Code:
mysql_query("UPDATE products SET prod_cat = 'buttons' WHERE prod_cat = 'books'");
it works...but obviously I need to use the passed variables...

The Bird from Down Under- Bigbird 3156 [upsidedown]
 
Code:
if (!empty($_GET['val1'])){
 $val2 = mysql_real_escape_string($_GET['val2']);
 $val1 = mysql_real_escape_string($_GET['val1']);
mysql_query("UPDATE products SET prod_cat = '$val2' WHERE prod_cat = '$val1');
}

this will update ALL instances of one product category with the new. i.e. NOT ON A RECORD BY RECORD BASIS.

if you want to be selective you need to add the primary key to the where clause.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top