SitePoint Sponsor |
|
User Tag List
Results 1 to 8 of 8
Thread: it wont update *sigh*....
-
Feb 13, 2002, 15:31 #1
- Join Date
- Feb 2002
- Location
- Birmingham, UK
- Posts
- 496
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
it wont update *sigh*....
what could be wrong
up.php...
<?php
if ($submit) {
$sql = "UPDATE news SET article=$article, title=$title, date=$date, id=$id, WHERE id=$id";
}
?>
<form method="post" action="up.php">
<?php
$sql = "SELECT * FROM news WHERE id=\"$id\"";
$result = mysql_query($sql);
$myrow = mysql_fetch_array($result);
$title = $myrow["title"];
$article = $myrow["article"];
$date = $myrow["date"];
$id = $myrow["id"];
?>
<input type=hidden name="id" value="<?php echo $id ?>">
idinput type="Text" name="id" value="<?php echo $id ?>"><br>
titleinput type="Text" name="title" value="<?php echo $title ?>"><br>
articleinput type="Text" name="article" value="<?php echo $article ?>"><br>
dateinput type="Text" name="date" value="<?php echo $date ?>"><br>
<input type="Submit" name="submit" value="Enter information">
</form>
anyone got any ideas why its not updating the database?http://redgoals.com ... my site ... nuff said
-
Feb 13, 2002, 16:00 #2
- Join Date
- Jul 2001
- Location
- The Netherlands
- Posts
- 2,617
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I believe the exact syntax for an update query should be this way in your case:
PHP Code:$sql = "UPDATE news SET article='$article', title='$title', date='$date', id=$id WHERE id='$id'";
Last edited by Mark T.; Feb 13, 2002 at 16:08.
-
Feb 13, 2002, 16:04 #3
- Join Date
- Sep 2001
- Location
- Barrie, Ontario
- Posts
- 324
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Assuming that your ID field is an auto increment field..
In SQL, you need apostrophes around strings, but not around numbers. You should only have one ID form field, otherwise the form will submit an array, and the WHERE clause won't work.
The SQL should look more like this...
PHP Code:<?
if( $submit ) {
$sql = "UPDATE news SET article='$article', title='$title', date='$date' WHERE id=$id";
}
?>
PHP Code:<?
$result = mysql_query( "SELECT * FROM news WHERE id=" . $id ) or die( mysql_error() );
$myrow = mysql_fetch_array( $result ) or die( mysql_error() );
$title = $myrow["title"];
$article = $myrow["article"];
$date = $myrow["date"];
$id = $myrow["id"];
?>
<form method="post" action="up.php">
<input type=hidden name="id" value="<? print( $id ); ?>">
title: <input type="Text" name="title" value="<? print( $title ); ?>"><br>
article: <input type="Text" name="article" value="<? print( $article ); ?>"><br>
date: <input type="Text" name="date" value="<? print( $date ); ?>"><br>
<input type="Submit" name="submit" value="Enter information">
</form>Last edited by KodeKrash; Feb 13, 2002 at 16:06.
-
Feb 13, 2002, 16:14 #4
- Join Date
- Feb 2002
- Location
- Birmingham, UK
- Posts
- 496
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
thnx for the help guys but no result yet.
after i pass the id in the address (up.php?id=3) i get the details presented from the row 3 in the database, i change the details and then hit submit but it returns the same form with the same details as they were in the database.
here take a look..linkhttp://redgoals.com ... my site ... nuff said
-
Feb 13, 2002, 16:29 #5
- Join Date
- Sep 2001
- Location
- Barrie, Ontario
- Posts
- 324
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I assume you are populating your variables, but this should work for you...
PHP Code:<?
if( $HTTP_POST_VARS["submit"] ) {
$article = trim( $HTTP_POST_VARS["article"] );
$title = trim( $HTTP_POST_VARS["title"] );
$date = trim( $HTTP_POST_VARS["date"] );
$id = $HTTP_POST_VARS["id"];
mysql_query( "UPDATE news SET article='$article', title='$title', date='$date' WHERE id=$id" ) or die( mysql_error() );
}
?>
-
Feb 13, 2002, 16:40 #6
Re: it wont update *sigh*....
The problem is you have design error in your code. If the the form is submitted the new sql query is set, but further down in your script the select sql query is set. This means the select query will always be executed. To perform the update query use mysql_query($sql) in your "if ($submit)" statement at the top of the script.
Cheers.
-
Feb 13, 2002, 17:24 #7
- Join Date
- Feb 2002
- Location
- Birmingham, UK
- Posts
- 496
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
success
. It seems filmfoto was correct about the query at the top, thanks KodeKrash for providing the code, much appreciated.
code in full...
<?
mysql_pconnect("localhost","username","pass");
mysql_select_db("dbname");
?>
<?
if( $HTTP_POST_VARS["submit"] ) {
$article = trim( $HTTP_POST_VARS["article"] );
$title = trim( $HTTP_POST_VARS["title"] );
$date = trim( $HTTP_POST_VARS["date"] );
$id = $HTTP_POST_VARS["id"];
mysql_query( "UPDATE news SET article='$article', title='$title', date='$date' WHERE id=$id" ) or die( mysql_error() );
}
?>
<?
$result = mysql_query( "SELECT * FROM news WHERE id=" . $id ) or die( mysql_error() );
$myrow = mysql_fetch_array( $result ) or die( mysql_error() );
$title = $myrow["title"];
$article = $myrow["article"];
$date = $myrow["date"];
$id = $myrow["id"];
?>
<form method="post" action="up.php">
<input type=hidden name="id" value="<? print( $id ); ?>">
title: <input type="Text" name="title" value="<? print( $title ); ?>"><br>
article: <input type="Text" name="article" value="<? print( $article ); ?>"><br>
date: <input type="Text" name="date" value="<? print( $date ); ?>"><br>
<input type="Submit" name="submit" value="Enter information">
</form>http://redgoals.com ... my site ... nuff said
-
Feb 14, 2002, 01:59 #8
- Join Date
- Jan 2001
- Location
- buried in the database shell (Washington, DC)
- Posts
- 1,107
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
You should run your text through addslashes before inserting into the DB, BTW.
e.g.
SET article='" . addslashes( $article ) . "'Matt - Sybase DBA / PHP fanatic
Sybase/MySQL/Oracle | I don't like MySQL
Download Sybase | DBForums.com - for all your RDBMS talk
Bookmarks