Page 1 of 1

Displaying Content From a Table

Posted: Sun Jun 29, 2003 12:22 am
by MeltedPixel
Hello everyone!
Its great to be here.
Anyways, im tying out my first php/mysql script.
So far, so good.
It submits the info you have sent to the MYSQL table, but i cant figure out how to display it.
I have this code to display it so far:

Code: Select all

<?
$host = "localhost";
$user = "meltedpi_defualt";
$password = "---------";
$database = "meltedpi_php";

$dbh = mysql_connect ($host, $user, $password) or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ($database);

$getaim = mysql_query("SELECT  *  FROM  `aim` ORDER BY id ")  or die (mysql_error());
WHILE($aim = mysql_fetch_array($getaim)){
$uname = $aim[uname];
}

?>
Just to let you know, i do have the password filled in =P
I have a table called "uname", and i want it to display that.
I also have a ID table, so it can list them in order.
When it does display that, i want it to put a brake next to it so it can list all of the usernames in a row.
So far, all i get is a blank screen.
If any one could help me here, i would greatly appreciate it!
Thanks,
MeltedPixel

Posted: Sun Jun 29, 2003 12:57 am
by qartis
Is that the full code you're executing? If so, the lines

Code: Select all

WHILE($aim = mysql_fetch_array($getaim)){
$uname = $aim[uname];
}
Are probably working, but they aren't supposed to put anything to the screen, they just fill the variable $uname with the last value of $aim[uname]. Try

Code: Select all

WHILE($aim = mysql_fetch_array($getaim)){
$uname[] = $aim[uname];
echo $aim[uname]."<br>\r\n";
}

Posted: Sun Jun 29, 2003 5:21 pm
by MeltedPixel
Thank you!
Here is the link:
http://meltedpixel.com/php/aim_view.php
See, i put it in my profile and it shows who has visited it.
Now, i need to figure out how to make it so when you visit my proflie, it will rederect you to:
http://meltedpixel.com/php/aim_view.php
In my profile, i have this link:
http://www.meltedpixel.com/php/aim.php? ... mented=set
the %n will put the name of the user in there, and once they click it it will add them to a mysql database i have, which also gives the hit a ID.
Anywho, about the rederect, i have this code for when you hit the page from my profile:

Code: Select all

<? 
$ip = getenv("REMOTE_ADDR");
$time = date("D M j"); 

if(isset($_GET['commented']))
{ 

// Tell the user it has been submitted (optional) 
echo("Your comment has been posted.");

// Set Mysql Variables
$host = "localhost"; 
$user = "meltedpi_defualt";
$pass = "-----";
$db = "meltedpi_php";
$table = "aim"; 

// Set global variables to easier names
$uname = $_GET['uname']; 

// Connect to Mysql, select the correct database, and run teh query which adds the data gathered from the form into the database
mysql_connect($host,$user,$pass) or die(mysql_error());
mysql_select_db($db) or die(mysql_error());
$add_all = "INSERT INTO $table values('$uname','')";
mysql_query($add_all) or die(mysql_error());
}
else
{

?>
<form method="get" action="<? echo"$PHP_SELF"; ?>">
AIM: <input type="text" name="uname"><br><br>
<input type="hidden" name="commented" value="set">
<input type="submit" value="Post your comment">
</form> 
<?
}
?>
I know its complicated, doing it with a form, but i cant figure out how to do it without the form in there.
Anyways, i know i have to put something that will rederect it to the page in there:

Code: Select all

// Tell the user it has been submitted (optional) 
echo("Your comment has been posted.");
I just cant figure out what.
If anyone call help me with this, and how to get rid of duplicates in the Database, please let me know.
Thanks,

Posted: Mon Jun 30, 2003 2:09 am
by MeltedPixel
Ok, i am now making a news script.
I want to make it so i can add news from a page.
I have this code:

Code: Select all

<? 
if(isset($_GET['commented'])) 
{ 

// Tell the user it has been submitted (optional) 
echo("Your comment has been posted.<br>Please <a href="news_view.php">click here</a> to view your comment!"); 

// Set Mysql Variables 
$host = "localhost"; 
$user = "meltedpi_defualt"; 
$pass = "-----"; 
$db = "meltedpi_php"; 
$table = "news"; 

// Set global variables to easier names 
$author = $_GET['author']; 
$title = $_GET['title']; 
$email = $_GET['email']; 
$content = $_GET['content']; 

// Connect to Mysql, select the correct database, and run teh query which adds the data gathered from the form into the database 
mysql_connect($host,$user,$pass) or die(mysql_error()); 
mysql_select_db($db) or die(mysql_error()); 
$add_all = "INSERT INTO $table values('$author,'$title','$email','$content','')"; 
mysql_query($add_all) or die(mysql_error()); 
} 
else 
{ 

// If the form has not been submitted, display it! 
?> 
<form method="get" action="<? echo"$PHP_SELF"; ?>"> 
Name: <input type="text" name="author"><br> 
Email: <input type="text" name="email"><br> 
Title: <input type="text" name="title"><br> 
Content: <textarea input type="text" name="content"></textarea><br> 
<input type="hidden" name="commented" value="set"> 
<input type="submit" value="Post your comment"> 
</form> 
<? 
} 
?>
It comes up with this error however when i press submit:
You have an error in your SQL syntax near 'test','test','test','')' at line 1
The weird thing is that is pulls the content from the database fine, but it does not seem to work adding content...
If any one could please help me with this.

Posted: Mon Jun 30, 2003 2:42 am
by qartis
Can the last column of the table `news` be a null value?