newbie question - updating trouble

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mmm
Forum Newbie
Posts: 3
Joined: Mon Nov 27, 2006 10:25 am

newbie question - updating trouble

Post by mmm »

I've been struggling with this one for a while now... i am new to php/mysql.

im making a 3x5 grid, each cell of which displays a row of database information. i've successfully created the table, entered data for each cell (15 cells), and displayed it. i have an update button in each cell, which links to a form allowing the user to update/change that information. i pass cell's id in the url: "updateCal.php?id=$id". The trouble is, the database is not updating.

I would greatly appreciate any advice... thanks!

heres the update script im using:

Code: Select all

<?php
include("dbinfo.php");

$ud_id=$_POST['ud_id'];
$ud_date=$_POST['ud_date'];
$ud_event1=$_POST['ud_event1'];
$ud_description1=$_POST['ud_description1'];
$ud_event2=$_POST['ud_event2'];
$ud_description2=$_POST['ud_description2'];
$ud_event3=$_POST['ud_event3'];
$ud_description3=$_POST['ud_description3'];
$ud_event4=$_POST['ud_event4'];
$ud_description4=$_POST['ud_description4'];

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");


$query="UPDATE calendar SET 
date='$ud_date', 
event1='$ud_event1', 
description1='$ud_description1', 
event2='$ud_event2', 
description2='$ud_description2',
event31='$ud_event3', 
description3='$ud_description3',
event4='$ud_event4', 
description4='$ud_description4'
WHERE id='$ud_id'";

mysql_query($query);
echo "Record Updated";
echo $query;
mysql_close();
?> 
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

updateCal.php?id=$id
Then it won't be available using

Code: Select all

$ud_id=$_POST['ud_id'];
Preferably I like to keep my data sources consistent. In this case, I would obviously pass the id like you did to the form, and then have a hidden attribute which will store that value. That way you won't have to mix up $_POST and $_GET.

Furthurmore, you seriously should consider validating your input. As in, at minimum, I can destroy your database content.

isset(),
empty(), and especialyl
mysql_real_escape_string() are your friends or simply search for input validation
mmm
Forum Newbie
Posts: 3
Joined: Mon Nov 27, 2006 10:25 am

Post by mmm »

Thanks for the reply

I don't quite understand what you mean by keeping data sources consistent. what i thought i was doing was:

1) on the data display page: linking to an update form which will correspond to a specific row within the database based upon the id passed in the link (

Code: Select all

updateCal.php?id=$id
)
2) on the form page: using the id value to query the database for the rest of the contents in the row of the passed id.
3) with the update script: assigning the values entered in the update form to variables with an "ud_" prefix
4) and then updating the database with these new variables.

i did include the hidden attribute in the form to store the id value... i thought i would need to use

Code: Select all

$id=$_GET['id'];
as well...

could you point me in the right direction? thanks much.

here's the code for the update form.

Code: Select all

<?php

$id=$_GET['id'];

include("dbinfo.php");
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM calendar WHERE id='$id'";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();

$date=mysql_result($result,"date");
$event1=mysql_result($result,"event1");
$description1=mysql_result($result,"description1");
$event2=mysql_result($result,"event2");
$description2=mysql_result($result,"description2");
$event3=mysql_result($result,"event3");
$description3=mysql_result($result,"description3");
$event4=mysql_result($result,"event4");
$description4=mysql_result($result,"description4");

?>

<form action="updateCalScript.php" method="post">
<input type="hidden" name="ud_id" value="<?php echo $id; ?>">
Date: <input type="text" name="ud_date" value="<?php echo $date; ?>"><br>
Event: <input type="text" name="ud_event1" value="<?php echo $event1; ?>"><br>
Description: <input type="text" name="ud_description1" value="<?php echo $description1; ?>"><br>
Event: <input type="text" name="ud_event2" value="<?php echo $event2; ?>"><br>
Description: <input type="text" name="ud_description2" value="<?php echo $description2; ?>"><br>
Event: <input type="text" name="ud_event3" value="<?php echo $event3; ?>"><br>
Description: <input type="text" name="ud_description3" value="<?php echo $description3; ?>"><br>
Event: <input type="text" name="ud_event4" value="<?php echo $event4; ?>"><br>
Description: <input type="text" name="ud_description4" value="<?php echo $description4; ?>"><br>
<input type="Submit" value="Update">
</form>
User avatar
boo_lolly
Forum Contributor
Posts: 154
Joined: Tue Nov 14, 2006 5:04 pm

Post by boo_lolly »

you don't need parenthesis () around your include file...

Code: Select all

include "dbinfo.php";
Van
Forum Newbie
Posts: 9
Joined: Thu Nov 23, 2006 6:35 am

Post by Van »

i'm not so good but u can write it like this

Code: Select all

include("dbinfo.php"); 

if (!empty($_GET['id'])){
$ud_id=$_GET['id']; 
$ud_date=$_POST['ud_date']; 
$ud_event1=$_POST['ud_event1']; 
$ud_description1=$_POST['ud_description1']; 
$ud_event2=$_POST['ud_event2']; 
$ud_description2=$_POST['ud_description2']; 
$ud_event3=$_POST['ud_event3']; 
$ud_description3=$_POST['ud_description3']; 
$ud_event4=$_POST['ud_event4']; 
$ud_description4=$_POST['ud_description4']; 

mysql_connect(localhost,$username,$password); 
@mysql_select_db($database) or die( "Unable to select database"); 

$query="UPDATE calendar SET 
date='$ud_date', 
event1='$ud_event1', 
description1='$ud_description1', 
event2='$ud_event2', 
description2='$ud_description2', 
event31='$ud_event3', 
description3='$ud_description3', 
event4='$ud_event4', 
description4='$ud_description4' 
WHERE id='$ud_id'"; 

if (mysql_query($query)){ 
echo "Record Updated sucessfully";
}else{ 
echo "update failed"; 

}
mysql_close(); 
}else{
echo "your ERROR message !";
}
?>
mmm
Forum Newbie
Posts: 3
Joined: Mon Nov 27, 2006 10:25 am

Post by mmm »

a friend of mine suggested replacing

Code: Select all

mysql_query($query);
echo "Record Updated";
echo $query;
mysql_close();
with

Code: Select all

$result = mysql_query($query) or die (mysql_error());
echo "Record Updated";
echo $result;
mysql_close();
this made it clear that i had a variable problem: $event31 instead of $event3

Now its time to figure out the data validation part of this...

Thanks for all your help
Post Reply