Jump to content

sql update script problem


Recommended Posts

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>

 

 

 

 

 

 

 

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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();
  }
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.