Hi, I'm trying to update a database with news, but I can't when I hit the submit button the form content gets erase and nothing happens in just a empty form. No errors. I'm getting the $id from the url since is coming from another page. Here's the code I'm using ... <?php $user = '***'; $pass = '***'; $host = 'localhost'; $db = 'test'; // connect to SQL $cid = mysql_connect($host,$user,$pass) or die(mysql_error()); mysql_select_db($db) or die(mysql_error()); //execute SQL statement if (!isset($_POST["submit"])) { $id = $_GET["id"]; $SQL = "SELECT * FROM news WHERE id = '$id' "; $result = mysql_query($SQL) or die(mysql_error().' SQL: '.$SQL); $row = mysql_fetch_array($result); $title= $row["title"]; $news = $row["news"]; ?> <FORM NAME="fa" action="editsave.php" METHOD="POST"> <INPUT TYPE="hidden" NAME="id" VALUE="<?php echo ("$id"); ?>" /> <B>Title:</B><br /><INPUT TYPE="text" name="title" VALUE="<?php echo ("$title"); ?>" SIZE=40 /><br /> <B>News:</B><br /><TEXTAREA name="news" ROWS=5 COLS=40><?php echo ("$news"); ?></TEXTAREA> <P><INPUT TYPE="submit" VALUE="Update" /></P> </FORM> <?php } if (isset($_POST["$submit"])) { $title = $_POST["title"]; $news = $_POST["news"]; //setup SQL statement $SQL= " UPDATE news SET news='$news', title='$title' WHERE id='$id'"; $result = mysql_db_query($db, "$SQL", $cid); //check for errors if (!$result) { echo("ERROR: " . mysql_error() . "\n$SQL\n"); } echo "news Updated"; } ?> PHP: And here's the database table... CREATE TABLE news ( id bigint(11) NOT NULL auto_increment, news mediumtext NOT NULL, title varchar(255) NOT NULL, newsdate varchar(255) NOT NULL, PRIMARY KEY (id) ); Code (markup): Hope anyone can help me. Thanks in advance!
How are you getting id? Are you passing it through a URL as in www.website.co.uk?id=yourvalue. If so, you need a $_GET['id'] rather than a POST.
Thank you for your replies guys. Yes, where do I need to add the $_GET['$id']; I thought I was going that already because it works, the only problem is that is not updating the table. No it didn't work.
if (isset($_POST["$submit"])) { PHP: Should that be if ($_POST["submit"] == 'Update') { PHP: Then I would put an extra id = in below that. $id = $_POST['id']; $title = $_POST["title"]; $news = $_POST["news"]; PHP:
Well what I would do in this situation is to "print $SQL;" at the end of your program. This will then show you exactly what query is being ran. If all variables are printed correctly in the statement, copy the printed SQL statement into your phpMyAdmin or other SQL control panel and see if that paticular query can be passed manually. Normally if it is a bad query, SQL will give you a better error message