stublackett Posted July 28, 2008 Share Posted July 28, 2008 Hi, I've got an "Edit Item" form setup on my website I'm trying to get SQL to Update the item on the site, But when I'm running it through it isnt doint it ??? Any ideas why ? My code is <?php //Collect New Post Vars if(isset($_POST['submit'])) { // Set global variables to easier names // and prevent sql injection and apostrophe to break the db. $title = $_POST['title']; $address = $_POST['address']; $postcode = $_POST['postcode']; $telephone = $_POST['telephone']; $email = $_POST['email']; $website = $_POST['website']; $info = $_POST['info']; $category = $_POST['category']; $id = $_GET['id']; $result = mysql_query("UPDATE directory SET title='$title', address ='$address', postcode ='$postcode', telephone='$telephone', email='$email', website='$website', info='$info', category='$category' WHERE id='$id' "); echo "Thank you! News has modified on the site!<br>You'll be redirected to the content management page in (5) Seconds"; echo "<br><br>"; echo "<meta http-equiv=Refresh content=5;url=index.html>"; } elseif(isset($_GET['id'])) { $result = mysql_query("SELECT * FROM $db_table2 WHERE id='$_GET[id]'"); while($myrow = mysql_fetch_assoc($result)) { $title = $myrow['title']; $address = $myrow['address']; $postcode = $myrow['postcode']; $telephone = $myrow['telephone']; $email = $myrow['email']; $website = $myrow['website']; $info = $myrow['info']; $category = $myrow['category']; $id = $_GET['id']; } ?> Link to comment https://forums.phpfreaks.com/topic/117002-solved-updating-an-sql-table/ Share on other sites More sharing options...
revraz Posted July 28, 2008 Share Posted July 28, 2008 Use mysql_error() after your query. Echo your query This will show you the error. Link to comment https://forums.phpfreaks.com/topic/117002-solved-updating-an-sql-table/#findComment-601741 Share on other sites More sharing options...
samshel Posted July 28, 2008 Share Posted July 28, 2008 2 simple steps to debug mysql queries.. 1) use mysql_error(); $result = mysql_query("UPDATE directory SET title='$title', address ='$address', postcode ='$postcode', telephone='$telephone', email='$email', website='$website', info='$info', category='$category' WHERE id='$id' ") or die (mysql_error()); 2) echo query and see if all values are passed to the query. sorry for duplicate post...but this highlights it is easy to debug Link to comment https://forums.phpfreaks.com/topic/117002-solved-updating-an-sql-table/#findComment-601742 Share on other sites More sharing options...
stublackett Posted July 28, 2008 Author Share Posted July 28, 2008 Cheers for the speedy response chaps Theres no error with the insertion... At least I dont think there is, Its just not updating the Database Table to the Vars set as $_POST I'm checking to see if they are being passed through and they definatley are Link to comment https://forums.phpfreaks.com/topic/117002-solved-updating-an-sql-table/#findComment-601744 Share on other sites More sharing options...
samshel Posted July 28, 2008 Share Posted July 28, 2008 1) if mysql_error() does not show anything. 2) and if echoed query contains all values try 1) copying and pasting the echoed query directly in database. if record is updated. 2) make sure DB connection is working and you are using error_reporting(7) to see all errors and no errors are supressed. Link to comment https://forums.phpfreaks.com/topic/117002-solved-updating-an-sql-table/#findComment-601748 Share on other sites More sharing options...
stublackett Posted July 28, 2008 Author Share Posted July 28, 2008 All I'm getting from the echo $result; is the number 1 no SQL Query showing..... Rather confused at how its just not working ??? Link to comment https://forums.phpfreaks.com/topic/117002-solved-updating-an-sql-table/#findComment-601752 Share on other sites More sharing options...
revraz Posted July 28, 2008 Share Posted July 28, 2008 I'll wager the $id isn't set. $sql = ("UPDATE directory SET title='$title', address ='$address', postcode ='$postcode', telephone='$telephone', email='$email', website='$website', info='$info', category='$category' WHERE id='$id' "); echo $sql; $result = mysql_query($sql) or die (mysql_error()); Link to comment https://forums.phpfreaks.com/topic/117002-solved-updating-an-sql-table/#findComment-601766 Share on other sites More sharing options...
samshel Posted July 28, 2008 Share Posted July 28, 2008 post echoed query, 1) is id passed as revraz mentioned ? 2) is the passed id present in database table ? if yes...then i am running out of reasons that can cause you trouble Link to comment https://forums.phpfreaks.com/topic/117002-solved-updating-an-sql-table/#findComment-601772 Share on other sites More sharing options...
stublackett Posted July 28, 2008 Author Share Posted July 28, 2008 The problem is the ID UPDATE directory SET title='Test', address ='Test', postcode ='TD15 1TB', telephone='01289 334495', email='[email protected]', website='http://www.test.com/', info='*** TEST *** Test Information being put in here', category='' WHERE id='' I have my SQL Query and the item I can see that is non existant is the ID Link to comment https://forums.phpfreaks.com/topic/117002-solved-updating-an-sql-table/#findComment-601774 Share on other sites More sharing options...
samshel Posted July 28, 2008 Share Posted July 28, 2008 thank god...you found the problem..i was about to ask you what direction your computer is facing ?? couldnt find anymore reasons for the problem hahaha Link to comment https://forums.phpfreaks.com/topic/117002-solved-updating-an-sql-table/#findComment-601778 Share on other sites More sharing options...
stublackett Posted July 28, 2008 Author Share Posted July 28, 2008 Haha Many thanks for your help there samshel and just for reference its facing me Link to comment https://forums.phpfreaks.com/topic/117002-solved-updating-an-sql-table/#findComment-601780 Share on other sites More sharing options...
revraz Posted July 28, 2008 Share Posted July 28, 2008 So let's see the URL that calls that page to make sure the $_GET is working right. Link to comment https://forums.phpfreaks.com/topic/117002-solved-updating-an-sql-table/#findComment-601782 Share on other sites More sharing options...
stublackett Posted July 28, 2008 Author Share Posted July 28, 2008 The ID isnt right Its just pulling through ID=" Any ideas how I can get that ID to be correct? Link to comment https://forums.phpfreaks.com/topic/117002-solved-updating-an-sql-table/#findComment-601784 Share on other sites More sharing options...
revraz Posted July 28, 2008 Share Posted July 28, 2008 So let's see the URL that calls that page to make sure the $_GET is working right. Link to comment https://forums.phpfreaks.com/topic/117002-solved-updating-an-sql-table/#findComment-601786 Share on other sites More sharing options...
stublackett Posted July 28, 2008 Author Share Posted July 28, 2008 The URL is : http://www.cornhill-on-tweed.com/test/admin/editdirectoryitem.php?id=7 Link to comment https://forums.phpfreaks.com/topic/117002-solved-updating-an-sql-table/#findComment-601788 Share on other sites More sharing options...
stublackett Posted July 28, 2008 Author Share Posted July 28, 2008 Think I've spotted it The FORM was set to go to editdirectoryitem.php and obviously that form wasnt echoing the ID so I believe it needs a <?php echo $PHP_SELF ?> Link to comment https://forums.phpfreaks.com/topic/117002-solved-updating-an-sql-table/#findComment-601793 Share on other sites More sharing options...
revraz Posted July 28, 2008 Share Posted July 28, 2008 Your problem is in your logic. If the code you posted above is editdirectoryitem.php See if moving $id = $_GET['id']; up to the fist line of code helps. You'll also want to sanitize that, but get it working first. Link to comment https://forums.phpfreaks.com/topic/117002-solved-updating-an-sql-table/#findComment-601798 Share on other sites More sharing options...
stublackett Posted July 28, 2008 Author Share Posted July 28, 2008 Your problem is in your logic. If the code you posted above is editdirectoryitem.php See if moving $id = $_GET['id']; up to the fist line of code helps. You'll also want to sanitize that, but get it working first. Its Solved, Cheers for your help though! Spot on, Helped me find out the issue was with the ID The form being pressed was submitting editdirectoryitem.php but no ID on it, Changing the form to <?php echo $_SELF ?> would then pull through the $id aswell, Jackpot! Thanks Link to comment https://forums.phpfreaks.com/topic/117002-solved-updating-an-sql-table/#findComment-601807 Share on other sites More sharing options...
samshel Posted July 29, 2008 Share Posted July 29, 2008 your id is not passed once the form is submitted...2 options: 1) take a hidden field called as id, in the form, assign value from $_GET to this field and for query use $_POST['id'] instead of $_GET['id']; 2) pass the id as query string to action of your form. change your form tag to something like this. <form method="post" enctype="multipart/form-data" action="?id=<?php echo $_GET['id'];?>" > Link to comment https://forums.phpfreaks.com/topic/117002-solved-updating-an-sql-table/#findComment-602353 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.