Updating a field in a DB with PHP.

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
dmt
Forum Newbie
Posts: 4
Joined: Wed Jan 21, 2004 8:27 am

Updating a field in a DB with PHP.

Post 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?
pinehead18
Forum Contributor
Posts: 329
Joined: Thu Jul 31, 2003 9:20 pm

Post 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);
dmt
Forum Newbie
Posts: 4
Joined: Wed Jan 21, 2004 8:27 am

Post 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?
User avatar
bluenote
Forum Commoner
Posts: 93
Joined: Sat Mar 01, 2003 4:59 am
Location: Heidelberg, Germany

Post 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
Last edited by bluenote on Thu Jan 29, 2004 7:26 am, edited 1 time in total.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Bluenote: could you start a new topic if you have a question of your own?

Ta.
User avatar
bluenote
Forum Commoner
Posts: 93
Joined: Sat Mar 01, 2003 4:59 am
Location: Heidelberg, Germany

Post by bluenote »

:roll: Sorry for the cross-post
Post Reply