empty value posting vars to table name in database from php


#1

I have a guest book but when it completes, only the username and message part works (updates to database) however $yourname var dosn’t get put into the table “name” on the database and think its this page because if I manually add a name in the table it shows up on the guest book view page,

any ideas please anyone, i have tried all kinds to no avail.
PS yes I am logged in, and not sure why it dosn’t work as it should as the message gets in there along with the users name ($username) who the guestbook is for.

[php]<?php
session_start();
?>

J~Net Guestbook <?php require 'database.php'; // include '../header.php'; $id = $_SESSION['user_id']; $_SESSION['user_id']= $id; $yourname = $_SESSION['user_name']; $_SESSION['user_name']= $yourname; // $_SESSION['user_name'] = $full_name; $_SESSION['user_level'] = $user_level; $_SESSION['HTTP_USER_AGENT'] = md5($_SERVER['HTTP_USER_AGENT']); $_SESSION['session_name'] = $user_name; $_SESSION['balance']= $balance; // Connects to your Database mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("messages") or die(mysql_error()); $data = mysql_query("SELECT * FROM users where id = $id") or die(mysql_error()); while($info = mysql_fetch_array( $data )) { echo ""; echo " ".$info['user_name'] . " "; } $username = $_SESSION['visited_profile']; $yourname = $info["user_name"]; $message = strip_tags($_POST['message']); if (isset($_POST['submit'])) { if (!$message) { echo "

Please Enter A Message!"; echo "$yourname"; echo " "; exit; } else{} $query = mysql_query("INSERT INTO guestbook (username, message, name) VALUES ('$username','$message','$yourname')") or die(mysql_error()); echo "Your Message Was Posted Sucsessfuly!
Continue"; } ?>[/php]


#2

what are you doing here?

[php] $id = $_SESSION[‘user_id’];
$_SESSION[‘user_id’]= $id; //you dont need this
$yourname = $_SESSION[‘user_name’];
$_SESSION[‘user_name’]= $yourname; //this neither
[/php]

this should be:
[php]$data = mysql_query(“SELECT * FROM users where id = ‘$id’ LIMIT 1”) or die(mysql_error()); [/php]

you cannot get variable out of one while(); or its posible that you get allways the last row :slight_smile:

[php]while($info = mysql_fetch_array( $data ))
{
echo “

”;
echo " ".$info[‘user_name’] . " ";
}
$username = $_SESSION[‘visited_profile’];
$yourname = $info[“user_name”];[/php]

for a specific id or user you dont need a while !
try this

[php]$data = mysql_fetch_row(mysql_query(“SELECT user_name FROM users where id = ‘$id’ LIMIT 1”));
$yourname = $data[0];[/php]

let me know if this helps you any further :slight_smile:


#3

thank you ever so much it wors now all i changed was this line

$data = mysql_fetch_row(mysql_query(“SELECT user_name FROM users where id = ‘$id’ LIMIT 1”));
$yourname = $data[0]
or die(mysql_error());

PS your amazing :smiley:


#4

feel free to visit my site my ip is there near the report button lmao if you want secure connection add certificate to exception list


#5

i`m glad you figured it out :wink:


#6

I cannot see your ip, only you can see it and it would be better when you would keep it that way, belive me you don`t want to publish your ip adress on a public forum :slight_smile:


#7

random I wudnt wana report myself lol anyway

2.216.117.189