1

I'm inserting a certain string value with a form in html(it's actually php, i'm echo-ing html) into mysql. Few days ago I inserted the value let's say "Ahhh". Now, i'm trying to insert something else, but it still brings up that old value. It's not the browsers cache nor is the value in my code wrong.

Form looks like this:

# form for adding a notice
echo "<form name='formAdd' action='controller.php' method='post'>";
echo "<input type='text' name='data' size='50'/>";
echo "<input type='hidden' name='user' value='$user' />";
echo "<input type='submit' name='submit' value='Add notice'/>";
echo "</form>";

I have 3 php files. functions.php, controller.php and the main file that shows content and creates the form that you can see above. The $user that i'm passing to controller.php is the right value that should be inserted (I print it and it's ok).

In controller.php I get the user by

if($_POST["user"]!=NULL){
# HERE he actually comes in, but gets another value from before. 
    $user = $_POST["user"];
}

After a call of a insert function I redirect back to the main file that should show me the updated value, but it shows the wrong one instead.

Any ideas?

additional info:

This gets the correct user in the main script.

# get username
$sql = "select username from user_auth where id=" . $_SESSION["sess_user_id"];
$result = mysql_query($sql) or die (mysql_error());
$name = mysql_fetch_array($result);
$user = $name['username'];
echo $user;

The functions.php looks like:

#cacti DB specifications
$database = "cacti";
$hostname = "localhost";
$username = "xxxxxx";
$password = "xxxxxx";
$port = "xxxx";

mysql_connect($hostname,$username,$password);
mysql_select_db($database);

function addNotice($data,$user,$date){
    $sql = "INSERT INTO lalalal (data,user,date) VALUES ('$data','$user','$date')";
    $result = mysql_query($sql) or die (mysql_error());
}

function deleteNotice($id){
    $sql = "DELETE FROM lalalala WHERE id='$id'";
    $result = mysql_query($sql) or die (mysql_error());
}

The controller.php looks like:

    $user = NULL;
if($_POST["user"]!=NULL){
    $user = $_POST["user"];
}

ini_set("display_errors", 1);

include_once("/var/www/html/cacti/plugins/lalala/functions.php");

$id = NULL;
$data = NULL;
$date = date('d.m.Y');

if($_POST["data"]!=NULL && $_POST["id"]==NULL){
    $data = $_POST["data"]; 
    addNotice($data,$user,$date);
}
8
  • 3
    care to show us the relevant code then? Commented Jul 13, 2012 at 18:35
  • 1
    My best guess would be to check the ID of the row being created. We'd need more code to see both the select and the insert statements. Commented Jul 13, 2012 at 18:38
  • Yes, if he just inserts the new value into the database, we should take a look at how the values are searched for in the db.. Commented Jul 13, 2012 at 18:40
  • Let us see the insert and select Commented Jul 13, 2012 at 18:42
  • You can figure out this issue by printing the sql you are running. I think data is not updating properly. Commented Jul 13, 2012 at 18:48

2 Answers 2

2

It sounds like maybe you are setting the default value to something in the form and that's what's getting thru instead of the new value.

echo "<input type='hidden' name='user' value='$user' />";

Perhaps, try not setting a default value in the form and then debug to see why your new value isn't coming thru.

If you post more of the code, we would probably be able to help more.

Sign up to request clarification or add additional context in comments.

1 Comment

Ahhh, it works now! This wasn't the problem, something else was. I was showing the results in a while loop, and had the $user variable used twice, once in a while loop for showing the data inserted and once before for the sql query. I didn't notice that the names were the same. tnx for help anyway
0

solved:

# get username
$sql = "select username from user_auth where id=" . $_SESSION["sess_user_id"];
$result = mysql_query($sql) or die (mysql_error());
$name = mysql_fetch_array($result);
$user = $name['username'];
echo $user;

# get data
$sql = "SELECT * FROM lalala";
$result = mysql_query($sql) or die (mysql_error());

echo "<table CELLPADDING=2 border=1 align=center>";
echo "<tr>";
echo "<th>User</th>";
echo "<th>Notice</th>";
echo "<th>Date</th>";
echo "</tr>";

while($row = mysql_fetch_array($result)){
    $id = $row['id'];
    $user = $row['user'];
    $data = $row['data'];
    $date = $row['date'];

    echo "<tr>";
    echo "<td>".$user."</td>";
    echo "<td>".$data."</td>";
    echo "<td>".$date."</td>";
    echo "<td><form name='formDelete' action='controller.php' method='post'>";
    echo "<input type='hidden' name='id' value='$id' />";
    echo "<input type='submit' name='submit' value='Delete'/>";
    echo "</form></td>";
    echo "</tr>";
}

echo "</table><br>";

# form for adding a notice
echo "<form name='formAdd' action='controller.php' method='post'>";
echo "<input type='text' name='data' size='50'/>";
echo "<input type='hidden' name='user' value='$user' />";
echo "<input type='submit' name='submit' value='Add notice'/>";
echo "</form>";

The problem was that there were 2 $user variables. And the one that got into a form was the last one displayed in a html table.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.