Jump to content

[SOLVED] Updating an SQL Table?


stublackett

Recommended Posts

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

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

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

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.

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

 

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

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

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'];?>" >

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.