Jump to content

What im doing wrong? Help plzzzz - Simple Update


princeofpersia

Recommended Posts

Hi guys,

 

I have an update page where users can update their information, i have a different update page which works fine with almost the same code, but this one wont update anything.

 

it does show the data but it wont update it,

can u help please?

 

<?php
session_start();

include ("../../global.php");
//welcome messaage
$username=$_SESSION['username'];
echo "$username";

$query=mysql_query("SELECT id FROM users WHERE username='$username'");
while($row = mysql_fetch_assoc($query))
{
$user_id = $row['id'];
}



$ref=$_GET['reference'];


if (isset($_POST['register']) && $_POST['register']){

$title = addslashes(strip_tags($_POST['title']));

$update=mysql_query("UPDATE msg SET title ='$title' WHERE reference='$ref'");

}
?>
<html>
<head>
    </head>
<body>
<form action='edit-msgs.php' method='POST' enctype='multipart/form-data'>
  Title:<br />
    <input type='text' name='title'  id='title' value='<?php 
$getdata = "SELECT title FROM msg WHERE reference='$ref' AND referal_id='$user_id'";
$result = mysql_query($getdata);
$row = mysql_fetch_assoc($result);
echo $row['title'];?>'><p />

    <input type='submit' name='register' value='Update'>
</form>
</body>
</html>





1. there is no form field with name='reference' in your form. You need to add a field called reference to the form.

2. the form is POST'ed, so you won't GET any variables from it. You need to change it to

 

$ref = $_POST['reference'];

 

if you simply echo out your SQL, you'll see the missing value.

 

$sql = "UPDATE msg SET title ='$title' WHERE reference='$ref'";
echo "sql: $sql <br />";

from previous page, as i mentioned, i have listed list of msgs belongs to a user, they can choose and edit each one in reference to their reference number for each msg

 

I have used the same techniq to delete and it works

 

<td><a href='edit-msgs.php?reference=".$row['reference']."'>Edit</a></td>

yes. you use GET when the reference is passed in the URL. But when you check to see if the form is POST'ed, $_GET['reference'] is no longer set. anyway, this updated code should work.

 

<?php
session_start();

include ("../../global.php");
//welcome messaage
$username=$_SESSION['username'];
echo "$username";

$query=mysql_query("SELECT id FROM users WHERE username='$username'");
$row = mysql_fetch_assoc($query);
$user_id = $row['id'];

if ($_SERVER['REQUEST_METHOD'] == "POST"){
$ref = $_POST['ref'];
$title = addslashes(strip_tags($_POST['title']));
$update=mysql_query("UPDATE msg SET title ='$title' WHERE reference='$ref'");
} else {
$ref=$_GET['reference'];
}
?>
<html>
<head>
    </head>
<body>
<form action='edit-msgs.php' method='POST' enctype='multipart/form-data'>
  Title:<br />
    <input type='text' name='title'  id='title' value='<?php 
$getdata = "SELECT title FROM msg WHERE reference='$ref' AND referal_id='$user_id'";
$result = mysql_query($getdata);
$row = mysql_fetch_assoc($result);
echo $row['title'];?>'><p />
<input type='hidden' name='reference' value='<?php echo $ref; ?>'>
    <input type='submit' name='register' value='Update'>
</form>
</body>
</html>

when you on the page that has the href tag with the reference if you hover over the edit link do you see a value for example ?reference=foo in the browser? I dont know if you echo out the content for the link tag or if its just html output but here are some things to try.

 

if content is been echoded

<?php

$row['reference'] = 2;

echo "<td><a href='edit-msgs.php?reference=".$row['reference']."'>Edit</a></td>";

?>

 

else if just html in php page

 

<td><a href='edit-msgs.php?reference=<?=$row['reference']?>'>Edit</a></td>

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.