jerry89 Posted April 8, 2009 Share Posted April 8, 2009 Hi, I have problem with UPDATE, when I press update, it just refresh the page, and wont update it. I cant see whats wrong <?php require_once("../includes/connection.php"); ?> <?php // FORM PROCCESS if (isset($_POST['submit'])) { $query = "UPDATE `meta` SET `keys` = '$keys' WHERE keys = '$keys' LIMIT 1 "; $result = mysql_query($query); if (mysql_affected_rows() == 1) { $message = "Update succesfull"; } else { $message = "There is problem"; $message = "<br />" .mysql_error(); } } // END OF PROCCESS ?> <?php $sql = "SELECT * FROM meta"; $result = MYSQL_QUERY($sql); $numberOfRows = mysql_num_rows($result); if ($numberOfRows==0) { echo "Sorry. No records found !!"; } else { $row = mysql_fetch_array($result); $keys = $row['keys']; } ?> <FORM action="meta.php" method="post"> <p>Edit:<br /> <textarea name="content" rows="10" cols="40"><?php echo $keys; ?></textarea> </p> <input type="submit" name="submit" value="Update Page" /> <br /> <?php echo $message; ?> </FORM> Hope you can help me Quote Link to comment https://forums.phpfreaks.com/topic/153161-cant-update-s/ Share on other sites More sharing options...
Maq Posted April 8, 2009 Share Posted April 8, 2009 Change this line and tell me what it outputs: $result = mysql_query($query) or die(msyql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/153161-cant-update-s/#findComment-804525 Share on other sites More sharing options...
premiso Posted April 8, 2009 Share Posted April 8, 2009 You never define $keys, you are trying to update the table where keys = $keys and you are updating keys to $keys, which makes no sense. Quote Link to comment https://forums.phpfreaks.com/topic/153161-cant-update-s/#findComment-804529 Share on other sites More sharing options...
jerry89 Posted April 8, 2009 Author Share Posted April 8, 2009 You never define $keys, you are trying to update the table where keys = $keys and you are updating keys to $keys, which makes no sense. So how do I do it, that it updates the database from the text area ? I just looked on the php admin and try update from there. so its gives me this UPDATE `test` SET `keys` = 'test' WHERE CONVERT( `test`.`keys` USING utf8 ) = 'test' LIMIT 1 ; and when I try it , it wont update after I try do in browser Quote Link to comment https://forums.phpfreaks.com/topic/153161-cant-update-s/#findComment-804531 Share on other sites More sharing options...
premiso Posted April 8, 2009 Share Posted April 8, 2009 First you need to know what row you want to update. You need a unique identifier (a Primary Key is preferred) for the table. So what is your table structure? You will need to pass that with the form. Second, your textarea name is "content" not $keys, so you need to use $_POST['content'] to reference the new value of the text area. Quote Link to comment https://forums.phpfreaks.com/topic/153161-cant-update-s/#findComment-804532 Share on other sites More sharing options...
jerry89 Posted April 8, 2009 Author Share Posted April 8, 2009 First you need to know what row you want to update. You need a unique identifier (a Primary Key is preferred) for the table. So what is your table structure? You will need to pass that with the form. Second, your textarea name is "content" not $keys, so you need to use $_POST['content'] to reference the new value of the text area. The table structure is just database name , and one table " test=> keys" thats all .. so if I put keys as primary key. and where include the $_post['content'] I am very sorry , I am beginer in php Quote Link to comment https://forums.phpfreaks.com/topic/153161-cant-update-s/#findComment-804544 Share on other sites More sharing options...
Maq Posted April 8, 2009 Share Posted April 8, 2009 He said "table structure", which means what are the fields and their types that are in that table, not what tables are in the database. Quote Link to comment https://forums.phpfreaks.com/topic/153161-cant-update-s/#findComment-804547 Share on other sites More sharing options...
jerry89 Posted April 8, 2009 Author Share Posted April 8, 2009 He said "table structure", which means what are the fields and their types that are in that table, not what tables are in the database. only 1 field " that is keys " no other fields Quote Link to comment https://forums.phpfreaks.com/topic/153161-cant-update-s/#findComment-804551 Share on other sites More sharing options...
premiso Posted April 8, 2009 Share Posted April 8, 2009 Learn proper SQL first. If you ask me. A primary key should generally be an auto_incrementing integer, this is to prevent confusion with 2 of the same keys and allow you to update the proper records. Because without your update querying will not run, unless you pass the original keys each time, which is sort of ludicrous. Here would be a working example of your script as is. <?php require_once("../includes/connection.php"); // FORM PROCCESS if (isset($_POST['submit'])) { $query = "UPDATE `meta` SET `keys` = '{$_POST['content']}' WHERE keys = '{$_POST['keys']}' LIMIT 1 "; $result = mysql_query($query); if (mysql_affected_rows() == 1) { $message = "Update succesfull"; } else { $message = "There is problem"; $message .= "<br />" .mysql_error(); // needed a .= for appending } } // END OF PROCCESS $sql = "SELECT * FROM meta"; $result = MYSQL_QUERY($sql); $numberOfRows = mysql_num_rows($result); if ($numberOfRows==0) { echo "Sorry. No records found !!"; } else { $row = mysql_fetch_array($result); $keys = $row['keys']; } ?> <FORM action="meta.php" method="post"> <p>Edit:<br /> <input type="hidden" name="keys" value="<?php echo $keys; ?>" /> <textarea name="content" rows="10" cols="40"><?php echo $keys; ?></textarea> </p> <input type="submit" name="submit" value="Update Page" /> <br /> <?php echo $message; ?> </FORM> Quote Link to comment https://forums.phpfreaks.com/topic/153161-cant-update-s/#findComment-804554 Share on other sites More sharing options...
jerry89 Posted April 8, 2009 Author Share Posted April 8, 2009 Learn proper SQL first. If you ask me. A primary key should generally be an auto_incrementing integer, this is to prevent confusion with 2 of the same keys and allow you to update the proper records. Because without your update querying will not run, unless you pass the original keys each time, which is sort of ludicrous. Here would be a working example of your script as is. <?php require_once("../includes/connection.php"); // FORM PROCCESS if (isset($_POST['submit'])) { $query = "UPDATE `meta` SET `keys` = '{$_POST['content']}' WHERE keys = '{$_POST['keys']}' LIMIT 1 "; $result = mysql_query($query); if (mysql_affected_rows() == 1) { $message = "Update succesfull"; } else { $message = "There is problem"; $message .= "<br />" .mysql_error(); // needed a .= for appending } } // END OF PROCCESS $sql = "SELECT * FROM meta"; $result = MYSQL_QUERY($sql); $numberOfRows = mysql_num_rows($result); if ($numberOfRows==0) { echo "Sorry. No records found !!"; } else { $row = mysql_fetch_array($result); $keys = $row['keys']; } ?> <FORM action="meta.php" method="post"> <p>Edit:<br /> <input type="hidden" name="keys" value="<?php echo $keys; ?>" /> <textarea name="content" rows="10" cols="40"><?php echo $keys; ?></textarea> </p> <input type="submit" name="submit" value="Update Page" /> <br /> <?php echo $message; ?> </FORM> I will look at some SQL tutorials. I tryed the way you send it , says "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'keys = 'test' LIMIT 1' at line 1" Em do I need to put the keys as "auto_incremt with primary key" I am very sorry Quote Link to comment https://forums.phpfreaks.com/topic/153161-cant-update-s/#findComment-804563 Share on other sites More sharing options...
premiso Posted April 8, 2009 Share Posted April 8, 2009 Not sure where/why that is failing (other than maybe keys is a reserved word). $query = "UPDATE `meta` SET `keys` = '{$_POST['content']}' WHERE `keys` = '{$_POST['keys']}' LIMIT 1 "; Try that and see if it fixes the error. Given this statement: Em do I need to put the keys as "auto_incremt with primary key" Learn SQL. EDIT: Once you get the basics down you should learn Normalization of SQL (3rd Normal Form). Here is description about auto_incrementing Quote Link to comment https://forums.phpfreaks.com/topic/153161-cant-update-s/#findComment-804570 Share on other sites More sharing options...
revraz Posted April 8, 2009 Share Posted April 8, 2009 Both KEY and KEYS is reserved. Quote Link to comment https://forums.phpfreaks.com/topic/153161-cant-update-s/#findComment-804573 Share on other sites More sharing options...
jerry89 Posted April 8, 2009 Author Share Posted April 8, 2009 Not sure where/why that is failing (other than maybe keys is a reserved word). $query = "UPDATE `meta` SET `keys` = '{$_POST['content']}' WHERE `keys` = '{$_POST['keys']}' LIMIT 1 "; Try that and see if it fixes the error. Given this statement: Em do I need to put the keys as "auto_incremt with primary key" Learn SQL. EDIT: Once you get the basics down you should learn Normalization of SQL (3rd Normal Form). Here is description about auto_incrementing It works Thank you so much Thank you so much Quote Link to comment https://forums.phpfreaks.com/topic/153161-cant-update-s/#findComment-804579 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.