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("******",$...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
<?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(); ?> |
|
|||
|
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- |
|
|||
|
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- |
|
|||
|
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>"; ?> |
|
|||
|
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. |
|
|||
|
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. |
|
|||
|
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; ?>" /> |
|
|||
|
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 & 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> </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(); ?> |