Forum Moderators: coopster

Message Too Old, No Replies

Updating a Dbase row using $id=$ GET['id']

         

warminster

10:44 am on May 4, 2010 (gmt 0)

10+ Year Member



Hello guys , HELP ! I basically have some individuals in a remembrance database with 6 fields
Primary index , person, inscription, name, email, tribute.

I am using a Form to gather tributes to individuals in my dbase
The link to the Form is within each individuals page
I process that form and hopefully the data is sent to the correct row ID/field in dbase
The tribute data is then retrieved from dbase and displayed in the individuals page

The only fields being UPDATED (by user) are name, email, tribute, the rest are pre populated.

My Problem is ;

I am having difficulty in making my mysql UPDATE statement place the harvested data from a form using $id=$_GET['id'] into the required dbase row / field

Any pointers would be really appreciated , I am fairly new to PHP but it is very addictive and i tend to get lost !


* Each person in dbase has a seperate php page containing code to retrieve their details and display within their page ..this works well.
$sql="SELECT * FROM $db_table WHERE person='person1' ";
$result=mysql_query($sql);
?>


* I then added a link for users to "add a tribute" via an input form , which in theory should place the users tribute in the dbase row referenced by
<a href="Form.php?id=<?php echo $rows['id']; ?>">Add Tribute</a>



* The form displays on screen with the (url/person id) in the browser address bar, but when processed it returns " error 1065 empty query " ?

//Form.php
//
<?php
$id=$_GET['id'];
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Leave a Tribute</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"><link href="style.css" rel="stylesheet" type="text/css">
<script>
</SCRIPT>
</head>
<body>
<div id="mainForm">
<div id="formHeader">
<h2 class="formInfo"><center>Leave a Tribute</center></h2>
</div>
<form method=POST enctype=multipart/form-data action=Process.php onSubmit="return validatePage1();"><ul class=mainForm id="mainForm_1">
<li class="mainForm" id="fieldBox_3">Your Name<label class="formFieldQuestion">
<input class=mainForm type=text name=field_3 id=field_3 size='30' value=''> </label></li>
<li class="mainForm" id="fieldBox_4">Your Email<label class="formFieldQuestion"></label>
<input class=mainForm type=text name=field_4 id=field_4 size='35' value=''> </li>
<li class="mainForm" id="fieldBox_5">Your Message<label class="formFieldQuestion"></label>
<textarea class=mainForm name=field_5 id=field_5 rows=4 cols=50></textarea></li>

<li class="mainForm">
<input id="id" name="id" type="hidden" value="<? echo $rows['id']; ?>">
<input id="saveForm" class="mainForm" type="submit" value="Submit" />
</li>
</form>
</body>
</html>


//Process.php
//

<?php
$where_form_is="http://".$_SERVER['SERVER_NAME'].strrev(strstr(strrev($_SERVER['PHP_SELF']),"/"));
$db_host="localhost"; // Host name
$db_user="user"; // Mysql username
$db_pass="pass"; // Mysql password
$db_name="dbasename"; // Database name
$db_table="tributes"; // Table name
$link = mysql_connect($db_host,$db_user,$db_pass);
if(!$link) die ('Could not connect to database: '.mysql_error());
mysql_select_db($db_name,$link);
mysql_query("UPDATE tributes SET name = '$field_3', email = '$field_4', tribute = '$field_5' WHERE id ='$id'" );
mysql_query($query);
mysql_close($link);
?>


Sorry it's a long piece of code guys :-(

Matthew1980

11:46 am on May 4, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there warminster,

Welcome to the forum!

Just quickly reading your code change this:-

mysql_query("UPDATE tributes SET name = '$field_3', email = '$field_4', tribute = '$field_5' WHERE id ='$id'" );

To:-

mysql_query("UPDATE `tributes` SET `name` = '".$field_3."', `email` = '".$field_4."', `tribute` = '".$field_5."' WHERE `id` = '".$id."' " );

Not sure if that will 'fix' the issue, but thats the thing that sticks out to me :)

Just noticed to that you are relying on using registered globals rather than passing the actual $_POST['element_name']; value, not the best way to go, as not all servers support the use of registered globals, and often have this option turned off in the .ini file.

And as you are putting the var's directly into a sql query - sanitise the user submitted data first, using functions like mysql_real_escape_string() and strip_tags() to clean data.

[EDIT]
How are you generating this:-
<a href="Form.php?id=<?php echo $rows['id']; ?>">Add Tribute</a>

because you need to make sure as the $_GET['id']; actually holds value before you use it in a query, try using:-
echo "<pre>";
print_r($GET);
echo "</pre>";

on the catching page so that you can see there is a value being passed to where you need it. :)

Hope that helps you a little ;-p

Cheers,
MRb

warminster

1:20 pm on May 4, 2010 (gmt 0)

10+ Year Member



Wow ! that was quick ... Thank you Mathew I will scour your post tomorrow when time allows and edit accordingly , I do apologise as I tend to plod along hoping to learn as I go.

mooger35

8:20 pm on May 4, 2010 (gmt 0)

10+ Year Member



and most importantly VALIDATE $_GET['id']

Does it contain a value?
Is the value in the format it should be?
And if the value isn't what you want, then what?