QUERY AND UPDATE PROBLEM

This is a discussion on QUERY AND UPDATE PROBLEM within the PHP Language forums, part of the PHP Programming Forums category; <?php /*---CONNECT TO DATABASE---*/ $conn = mysql_connect("localhost", "******", "******") or die(mysql_error()); mysql_select_db("******",$...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #21 (permalink)  
Old 02-27-2006
Lag
 
Posts: n/a
Default Re: QUERY AND UPDATE PROBLEM

<?php

/*---CONNECT TO DATABASE---*/

$conn = mysql_connect("localhost", "******", "******") or
die(mysql_error());
mysql_select_db("******",$conn) or die(mysql_error());

$event = $_GET["id"];
$title=$_POST['title'];
$date=$_POST['datetime'];
$desc=$_POST['desc'];

//---UPDATE ENTRY IN DATABASE---
$result = mysql_query("SELECT * FROM calendar_events WHERE
id='.$event'",$conn);
$count = mysql_num_rows($result);
$query = "UPDATE calendar_events SET event_title = '" . $title . "',
event_start = '" . $date . "', event_shortdesc = '".$desc."' WHERE id =
$event";
mysql_query($query) or die(mysql_error());

//LIST ITEMS
echo "$event $title $date $desc";

echo "<br>Record Updated! Rows Affected: $count";
mysql_close();

?>

Reply With Quote
  #22 (permalink)  
Old 02-27-2006
David Haynes
 
Posts: n/a
Default Re: QUERY AND UPDATE PROBLEM

Lag wrote:
> <?php
>
> /*---CONNECT TO DATABASE---*/
>
> $conn = mysql_connect("localhost", "******", "******") or
> die(mysql_error());
> mysql_select_db("******",$conn) or die(mysql_error());
>
> $event = $_GET["id"];
> $title=$_POST['title'];
> $date=$_POST['datetime'];
> $desc=$_POST['desc'];
>
> //---UPDATE ENTRY IN DATABASE---
> $result = mysql_query("SELECT * FROM calendar_events WHERE
> id='.$event'",$conn);
> $count = mysql_num_rows($result);
> $query = "UPDATE calendar_events SET event_title = '" . $title . "',
> event_start = '" . $date . "', event_shortdesc = '".$desc."' WHERE id =
> $event";
> mysql_query($query) or die(mysql_error());
>
> //LIST ITEMS
> echo "$event $title $date $desc";
>
> echo "<br>Record Updated! Rows Affected: $count";
> mysql_close();
>
> ?>
>

Why the double query? Why not:

//---UPDATE ENTRY IN DATABASE---
$query = "UPDATE calendar_events SET event_title = '$title',
event_start = '$date', event_shortdesc = '$desc' WHERE id = $event";
mysql_query($query) or die(mysql_error());
$count = mysql_affected_rows($result);
....

or, to make it more readable/maintainable:
//---UPDATE ENTRY IN DATABASE---
$query = <<<SQL
UPDATE calendar_events
SET
event_title = '$title',
event_start = '$date',
event_shortdesc = '$desc'
WHERE id = $event
SQL;
mysql_query($query) or die(mysql_error());
$count = mysql_affected_rows($result);

or if you don't like the use of <<<
//---UPDATE ENTRY IN DATABASE---
$sql = "UPDATE "
."calendar_events "
."SET "
."event_title = '$title', "
."event_start = '$date', "
."event_shortdesc = '$desc' "
."WHERE id = $event ";
mysql_query($query) or die(mysql_error());
$count = mysql_affected_rows($result);

Either of these last two methods allows you to add/subtract/edit the SQL
without having to walk a huge line of code.

-david-

Reply With Quote
  #23 (permalink)  
Old 02-27-2006
David Haynes
 
Posts: n/a
Default Re: QUERY AND UPDATE PROBLEM

David Haynes wrote:
>
> or if you don't like the use of <<<
> //---UPDATE ENTRY IN DATABASE---
> $sql = "UPDATE "
> ."calendar_events "
> ."SET "
> ."event_title = '$title', "
> ."event_start = '$date', "
> ."event_shortdesc = '$desc' "
> ."WHERE id = $event ";
> mysql_query($query) or die(mysql_error());
> $count = mysql_affected_rows($result);
>
> Either of these last two methods allows you to add/subtract/edit the SQL
> without having to walk a huge line of code.
>
> -david-
>


.... of course the query should be
mysql_query($sql) or die(mysql_error());

-david-

Reply With Quote
  #24 (permalink)  
Old 02-27-2006
samudasu
 
Posts: n/a
Default Re: QUERY AND UPDATE PROBLEM

You are making this so difficult when it doesn't have to be.
Run this line of code and post the results! Don't include your own
code.

<?php
$event = $_GET['id'];
print "id for query is: $event<br />";
mysql_connect("localhost", "******", "******") or die("can't connect");
mysql_select_db("******") or die("can't select database");
$query = "select * from calendar_events where id = '$event'";
$result = mysql_query($query);
$data = mysql_fetch_assoc($result);
print "<pre>";
print_r($data);
print "</pre>";
?>

Reply With Quote
  #25 (permalink)  
Old 02-27-2006
Lag
 
Posts: n/a
Default Re: QUERY AND UPDATE PROBLEM

Sorry............thanks for the help.

This is the output...

id for query is:

Reply With Quote
  #26 (permalink)  
Old 02-27-2006
Lag
 
Posts: n/a
Default Re: QUERY AND UPDATE PROBLEM

I'm trying to pass the id via a button.........this is the code.....
<td colspan="2"><p><br>
<a href="update.php?id=<? echo $id; ?>">Update
Event?</a>
<input name="update" type="submit" id="update"
value="Update Event">
</p>
</td>

Using a text link, so I can see the id in the status bar.......I get
the result.
109
Record Updated! Rows Affected: 0

Where 109 is the id.

Reply With Quote
  #27 (permalink)  
Old 02-27-2006
samudasu
 
Posts: n/a
Default Re: QUERY AND UPDATE PROBLEM

Were you opening my script from your form submission page?

And one more thing i just noticed in your code is this:
$event = $_GET["id"];
$title=$_POST['title'];
$date=$_POST['datetime'];
$desc=$_POST['desc'];


Why do you have $_GET["id"] and not $_POST["id"] ???
Is your from method set to "post" or set to "get"? This could be the
source of all your troubles.

Reply With Quote
  #28 (permalink)  
Old 02-27-2006
samudasu
 
Posts: n/a
Default Re: QUERY AND UPDATE PROBLEM

Think about this. If you click on your link
"<a href="update.php?id=<? echo $id; ?>">Update Event?</a>", the only
thing passed to update.php will be the id. You can't do an update with
an id alone. The same thing goes for the form. The values "title",
"datetime", and "desc" are passed to update.php but id isn't unless you
specify a hidden field in your form with the value of id.

<input type="hidden" name="id" value="<? echo $id; ?>" />

Reply With Quote
  #29 (permalink)  
Old 02-27-2006
Lag
 
Posts: n/a
Default Re: QUERY AND UPDATE PROBLEM

Thanks for the example.....that makes sense.....if I use a form how do
I get the data, like this? The form is listed first!

<td><form name="edit" method="post" action="updated.php?id=<?php echo
$id; ?>&title=title&datetime=datetime&desc=desc">
<table width="300" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="100" align="left" valign="top">Event Title</td>
<td width="298"><input name="title" type="text" id="title"
value="<? echo $title; ?>" size="30"></td>
</tr>
<tr>
<td width="100">Date &amp; Time</td>
<td><input name="datetime" type="text" id="datetime"
value="<? echo $date; ?>" size="30"></td>
</tr>
<tr>
<td width="100">Description</td>
<td><textarea name="desc" cols="30" rows="5" wrap="VIRTUAL"
id="desc"><? echo $desc; ?></textarea></td>
</tr>
<tr>
<td colspan="2">
<p>&nbsp;</p><p>
<input name="update" type="submit" id="update"
value="Update Event">
</p>
</td>
</tr>
</table>
</form></td>

---------------------updated.php----------------------------

//id, title, datetime, desc are all names of the text fields in the
form on the previous page

$ud_id=$_POST['id'];
$ud_title=$_POST['title'];
$ud_date=$_POST['datetime'];
$ud_desc=$_POST['desc'];
//$ud_web=$_POST['ud_web'];

$query="UPDATE calendar_events SET event_title='$ud_title',
event_start='$ud_date', event_shortdesc='$ud_desc' WHERE id='$ud_id'";
mysql_query($query);
echo "Record Updated";
mysql_close();
?>

Reply With Quote
  #30 (permalink)  
Old 02-27-2006
Lag
 
Posts: n/a
Default Re: QUERY AND UPDATE PROBLEM

or is the data actually pulled with _POST?

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 12:01 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0