Page 1 of 1

Updating a field in a DB with PHP.

Posted: Wed Jan 21, 2004 8:27 am
by dmt
I have a DB that I am pulling information from. The information is tied to someone's name, and has a specific id with each entry. I have added a modify button because I would like to modify certain fields on ONE specific entry found. I make my changes, and would like for the changes to append to the existing field with a date stamp. I am having a problem with the UPDATE command, and cannot get it to write back to the DB. When I assign a variable to use in the UPDATE command it kicks back a message saying it is an undefined variable, even though I declared as a global variable. Any suggestions?

Posted: Wed Jan 21, 2004 8:59 am
by pinehead18
$var = "hiya";

$con = mysql_connect("localhost","user","pass") or die(mysql_error());
$db = mysql_select_db("db",$con) or die(mysql_error());
$sql = "UPDATE tablename SET field='$var' WHERE user='$user'";
$result = mysql_query($sql,$con) or die(mysql_error());

mysql_close($con);

Posted: Wed Jan 21, 2004 10:29 am
by dmt
"$sql = "UPDATE tablename SET field='$var' WHERE user='$user'";" The problem I am having is stemming from the variable "$var" not being sent from the previous page.
I have two fields that need to come over from the previous page. I put my UPDATE command just as you have it before and it tells me that "$var" is an undefined variable. When I declare it at the top (or as a glodal variable) it still tells me that it is undefined. Why?

Posted: Thu Jan 29, 2004 7:11 am
by bluenote
Hi,

the prob could be that - if you're using PHP > 4.2 and REGISTER_GLOBALS is off - $var needs to be redeclared like this:

step 1:

Code: Select all

<?php
echo "input type="text" name="var" value=\someVal">";

?>
step 2:

Code: Select all

<?php

$var = $_POST["var"]; or if using GET
$var = $_GET["var"]; or if unknown
$var = $_REQUEST["var"]; or if input type field was file
$var = $_FILES["var"];

?>
I hope this helps.
-bluenote

Posted: Thu Jan 29, 2004 7:24 am
by McGruff
Bluenote: could you start a new topic if you have a question of your own?

Ta.

Posted: Thu Jan 29, 2004 7:32 am
by bluenote
:roll: Sorry for the cross-post