danielandlisa3144 Posted June 15, 2009 Share Posted June 15, 2009 I got a sql update script with two files, select.php that shows the records sorted by id and update.php with the sql update query. Select.php displays the records from the table and it sends you to update.php after clicking on the id link. When i get to update.php the form shows the value from the field in the table but when i hit submit it only shows a blank page and the database doesnt get updated. I only got one field in the table that i want to be updated startsida, the name of the table is also startsida. Im showing the code below for the pages. / Thanks Lisa select.php <? // Connect database include("connectdb.php"); // Get all records in all columns from table and put it in $result. $result=mysql_query("select * from startsida"); /*Split records in $result by table rows and put them in $row. Make it looping by while statement. */ while($row=mysql_fetch_assoc($result)){ // Output echo "id : {$row['id']} <br/>"; echo "startsida : {$row['startsida']} <br/>"; // Add a link with a parameter(id) and it's value. echo '<a href="update.php?id='.$row['id'].'">Update</a>'; } mysql_close(); ?> update.php <? // START PHP CODES. THIS PART MUST ON THE TOP OF THIS PAGE. // Connect database. include("connectdb.php"); // ***** This part will process when you Click on "Submit" button ***** // Check, if you clicked "Submit" button if($_POST['Submit']){ // Get parameters from form. $id=$_POST['id']; $startsida=$_POST['startsida']; // Do update statement. mysql_query("update startsida set startsida='$startsida' where id='$id'"); // Re-direct this page to select.php. header("location:select.php"); exit; } // ************* End update part ************* // *** Select data to show on text fields in form. *** // Get id parameter (GET method) from select.php $id=$_GET['id']; // Get records in all columns from table where column id equal in $id and put it in $result. $result=mysql_query("select * from startsida where id='$id'"); // Split records in $result by table rows and put them in $row. $row=mysql_fetch_assoc($result); // Close database connection. mysql_close(); ?> <!-- END OF PHP CODES AND START HTML TAGS --> <html> <body> <!-- set this form to POST method and target this form to itself ($PHP_SELF;)--> <form id="form1" name="form1" method="post" action="<? echo $PHP_SELF; ?>"> <p>Message : <!-- name of this text field is "name" --> <textarea name="startsida" cols="40" rows="10" id="startsida"><? echo $row['startsida']; ?></textarea> <br /> <p> <input type="submit" name="Submit" value="Submit" /> </p> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/162233-sql-update-script-problem/ Share on other sites More sharing options...
jxrd Posted June 15, 2009 Share Posted June 15, 2009 I didn't really read your code, but I can see instantly that you're not handling query failures at all. Put or die(mysql_error()); after every query/mysql connection. Quote Link to comment https://forums.phpfreaks.com/topic/162233-sql-update-script-problem/#findComment-856227 Share on other sites More sharing options...
danielandlisa3144 Posted June 15, 2009 Author Share Posted June 15, 2009 Ok thanks i will do that wont solve the problem with the script though. Any other suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/162233-sql-update-script-problem/#findComment-856245 Share on other sites More sharing options...
jxrd Posted June 15, 2009 Share Posted June 15, 2009 If you do that you should see why your update isn't working. Also, try echoing out $startsida to see what it is. If you're getting a blank page, you may want to turn on error reporting as well. ini_set('display_errors', E_ALL); Quote Link to comment https://forums.phpfreaks.com/topic/162233-sql-update-script-problem/#findComment-856268 Share on other sites More sharing options...
danielandlisa3144 Posted June 15, 2009 Author Share Posted June 15, 2009 my webhost dont have error error reporting turned on for some reason so i cant get the error report. When something is wrong with the script i only get a blank page. Quote Link to comment https://forums.phpfreaks.com/topic/162233-sql-update-script-problem/#findComment-856281 Share on other sites More sharing options...
jxrd Posted June 15, 2009 Share Posted June 15, 2009 ini_set('display_errors', E_ALL); That should turn it on. I just tested your script and there aren't any parse errors, but obviously i dont know about the sql as I dont have the same db/table structure as you. Without error reporint it's pretty hard to tell. You should set up PHP on your own machine with error reporting turned on. Quote Link to comment https://forums.phpfreaks.com/topic/162233-sql-update-script-problem/#findComment-856287 Share on other sites More sharing options...
danielandlisa3144 Posted June 15, 2009 Author Share Posted June 15, 2009 Im going to try it on another web account my table structure is simple table name startsida fields id int auto increment startsida longtext Quote Link to comment https://forums.phpfreaks.com/topic/162233-sql-update-script-problem/#findComment-856290 Share on other sites More sharing options...
haku Posted June 15, 2009 Share Posted June 15, 2009 If you don't tell us the error, we can't tell you the problem. Quote Link to comment https://forums.phpfreaks.com/topic/162233-sql-update-script-problem/#findComment-856331 Share on other sites More sharing options...
danielandlisa3144 Posted June 15, 2009 Author Share Posted June 15, 2009 thanks guys i tried it on another host and got error on the update page Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource and after hitting submit i got Warning: Cannot modify header information - headers already sent the sql querys didnt seem to give any errors though Quote Link to comment https://forums.phpfreaks.com/topic/162233-sql-update-script-problem/#findComment-856365 Share on other sites More sharing options...
danielandlisa3144 Posted June 15, 2009 Author Share Posted June 15, 2009 i guess one part of the problem is the headers allready sent since the form redirects to select.php after submitting it. Im not sure why i get the error though i got the html code under the php code in the document Quote Link to comment https://forums.phpfreaks.com/topic/162233-sql-update-script-problem/#findComment-856506 Share on other sites More sharing options...
justAnoob Posted June 15, 2009 Share Posted June 15, 2009 I'm still learning, but this is how I do updates <?php $sql = "UPDATE table_name_here SET whatever = '$var', whatever2 = '$var2' WHERE something = '$var3'"; if(mysql_query($sql)) { $url = $_SESSION['url']; header("location:" . $url); exit(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/162233-sql-update-script-problem/#findComment-856563 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.