sennetta Posted July 14, 2007 Share Posted July 14, 2007 I'm trying to update entries in the following table: <?php include("lgcreds.php"); //connect to mysql mysql_connect($host,$username,$password); //select database @mysql_select_db($database) or die("Unable to select database"); //set query $query = "CREATE TABLE news (id int(6) NOT NULL auto_increment, postdate int(30) NOT NULL, headline text NOT NULL, newsText text NOT NULL, deleted tinyint(1) NOT NULL, PRIMARY KEY (id),KEY postdate (postdate))"; //run query mysql_query($query); //close mysql_close(); ?> using the following scriptlet: <?php //connect to mysql include("../functions/lgcreds.php"); mysql_connect($host,$username,$password); //get form data $headline = mysql_real_escape_string($_POST['headline']); $newsText = mysql_real_escape_string($_POST['newsText']); //if new news if (isset($_POST['id'])) { $ID = $_POST['id']; $query = "UPDATE news SET headline='$headline' AND newsText='$newsText' WHERE id='$ID'"; } else { //get timestamp $timestamp = time(); $query = "INSERT INTO news VALUES('','$timestamp','$headline','$newsText','')"; } //select db @mysql_select_db($database) or die("Unable to select database"); //query mysql_query($query); echo "<span style=\"color:red;\">".mysql_error()."</span><br />"; //close mysql_close(); echo "$query"; ?> which is called by the following form: <?php if (isset($_GET['id'])) { //find ID $ID = $_GET['id']; //DB include("../functions/lgcreds.php"); //set query $query = "SELECT * FROM news WHERE id=$ID"; //connect mysql_connect($host,$username,$password); //select database @mysql_select_db($database) or die( "Unable to select database"); //query db $result = mysql_query($query); //close mysql_close(); //find info $head = mysql_result($result,0,"headline"); $story = mysql_result($result,0,"newsText"); } else { $head = ""; $story = ""; } ?> <html> <head> <style type="text/css"> strong { color: f00; } </style> <title>News update form - Tyger Style</title> </head> <body> <strong>Build in password functions before uploading!!</strong> <form action="newsUpdate.php" method="post"> <table> <!-- timestamp --> <?php if (isset($ID)) echo "<input type=\"hidden\" name=\"id\" value=\"$ID\" />"; ?> <tr><td>Headline:</td><td><input type="text" style="width: 100%" onclick="this.select();" name="headline" value="<?php echo "$head"?>"/></td></tr> <tr><td>Story:</td><td><textarea onclick="this.select();" name="newsText" rows="30" cols="100"><?php echo "$story"?></textarea></td></tr> </table> <input type="submit" value="Submit news" /> </form> </body> </html> If the form is called with no ID specified, the script assumes a new news item and creates a timestamp etc. This scriptlet generates no errors, but does not update the table. What's going on? Cheers, Anthony Quote Link to comment https://forums.phpfreaks.com/topic/59948-solved-table-wont-update/ Share on other sites More sharing options...
Wildbug Posted July 14, 2007 Share Posted July 14, 2007 Try: $query = "INSERT INTO news VALUES(NULL,'$timestamp','$headline','$newsText','')"; Quote Link to comment https://forums.phpfreaks.com/topic/59948-solved-table-wont-update/#findComment-298303 Share on other sites More sharing options...
sennetta Posted July 14, 2007 Author Share Posted July 14, 2007 Sorry should have been more specific. The news gets added fine (as a new news item) if id is not set in the URL, but the code won't change an item of news: $query = "UPDATE news SET headline='$headline' AND newsText='$newsText' WHERE id='$ID'"; doesn't work: it generates no errors, but it does not update the table. I've tried casting $ID to an int and removing quotes and everything. Nothing seems to work! Quote Link to comment https://forums.phpfreaks.com/topic/59948-solved-table-wont-update/#findComment-298311 Share on other sites More sharing options...
Wildbug Posted July 14, 2007 Share Posted July 14, 2007 Oh. $query = "UPDATE news SET headline='$headline', newsText='$newsText' WHERE id=$ID"; Quote Link to comment https://forums.phpfreaks.com/topic/59948-solved-table-wont-update/#findComment-298314 Share on other sites More sharing options...
sennetta Posted July 14, 2007 Author Share Posted July 14, 2007 Ah the comma! Many thanks. Quote Link to comment https://forums.phpfreaks.com/topic/59948-solved-table-wont-update/#findComment-298316 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.