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>





Link to comment
Share on other sites

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 />";

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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>

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.