| | |
PHP MySQL problem
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Aug 2007
Posts: 9
Reputation:
Solved Threads: 0
Hey guys. Got a quick question. I am having a problem with a message board that I am creating. I have created the log in and registration page and the sessions needed to use the site nad it all works fine, but when the user posts a message I can't get the unique user name to appear for the message that user posts. I can get it to appear as long as the users session is still valid, but once the user logs out, the session is terminated and the users name is gone from the message board. Please help?
If the login name is the id for the user in the database, then that's fine to keep it in the session while they are logged in. You will still need to write that value to the database with the post though. The page that displays posts should retrieve the name of the poster along with the post from the database.
Actually, I would store both the user name and the id in the session. Whatever statement that saves the post just needs to include the id. The query that shows posts just needs to display the name along side the comment, so pull all that info in a single query. If you are confused by that, post your code and we can figure out how to change it.
•
•
Join Date: Aug 2007
Posts: 9
Reputation:
Solved Threads: 0
•
•
•
•
Actually, I would store both the user name and the id in the session. Whatever statement that saves the post just needs to include the id. The query that shows posts just needs to display the name along side the comment, so pull all that info in a single query. If you are confused by that, post your code and we can figure out how to change it.
Aw man....I get it...Thanks...I am about to head out of work for the day...We'll see how it goes tonight and I'll let you know tomorrow...Thanks for all your help!
•
•
Join Date: Aug 2007
Posts: 9
Reputation:
Solved Threads: 0
<?php require_once('Connections/riders.php'); ?>
<?php
if (!isset($_SESSION)) {
session_start();
}
$logoutAction = $_SERVER['PHP_SELF']."?doLogout=true";
if ((isset($_SERVER['QUERY_STRING'])) && ($_SERVER['QUERY_STRING'] != "")){
$logoutAction .="&". htmlentities($_SERVER['QUERY_STRING']);
}
if ((isset($_GET['doLogout'])) &&($_GET['doLogout']=="true")){
$_SESSION['MM_Username'] = NULL;
$_SESSION['MM_UserGroup'] = NULL;
$_SESSION['PrevUrl'] = NULL;
unset($_SESSION['MM_Username']);
unset($_SESSION['MM_UserGroup']);
unset($_SESSION['PrevUrl']);
$logoutGoTo = "index.php";
if ($logoutGoTo) {
header("Location: $logoutGoTo");
exit;
}
}
?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
$insertSQL = sprintf("INSERT INTO messages (message) VALUES (%s)",
GetSQLValueString($_POST['textarea'], "text"));
mysql_select_db($database_riders, $riders);
$Result1 = mysql_query($insertSQL, $riders) or die(mysql_error());
$insertGoTo = "messagepost.php";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Bruces Rider's Social Network. Plan rides, meet new people, or just bike talk.</title>
<style type="text/css">
<!--
body {
background-color: #000000;
}
.style9 {
font-size: 48px;
color: #FFFFFF;
}
.mystyle {
font-family: Arial, Helvetica, sans-serif;
color: #FFFFFF;
font-size: medium;
font-weight: bold;
}
.style11 {color: #FFFFFF}
-->
</style></head>
<body>
<table width="834" height="477" border="0" cellpadding="0" cellspacing="0">
<!--DWLayoutTable-->
<tr>
<td width="241" rowspan="2" valign="top"><table width="100%" border="0" cellpadding="0" cellspacing="0">
<!--DWLayoutTable-->
<tr>
<td width="241" height="192" valign="top"><img src="images/all-stores-bitmap.jpg" width="241" height="192" /></td>
</tr>
<tr>
<td height="92" valign="top"><table width="100%" height="105" border="0" cellpadding="0" cellspacing="0">
<!--DWLayoutTable-->
<tr>
<td width="241" height="22" valign="top"><!--DWLayoutEmptyCell--> </td>
</tr>
<tr>
<td height="49"> </td>
</tr>
<tr>
<td height="34" valign="top"><table width="100%" border="0" cellpadding="0" cellspacing="0">
<!--DWLayoutTable-->
<tr>
<td width="241" height="34" valign="top" class="mystyle"><a href="<?php echo $logoutAction ?>" class="style11">Log out</a></td>
</tr>
</table> </td>
</tr>
</table> </td>
</tr>
</table></td>
<td width="593" height="57" valign="top"><table width="100%" border="0" cellpadding="0" cellspacing="0">
<!--DWLayoutTable-->
<tr>
<td width="589" height="49" valign="top"><div align="center"><span class="style9">Welcome to Bruces Riders!</span></div></td>
</tr>
</table>
<td width="593" height="156"> </td>
</tr>
<tr>
<td width="593" height="113" valign="top" class="mystyle"><?php echo $_SESSION['MM_Username']; ?> ,please feel free to post a message.
<form action="<?php echo $editFormAction; ?>" id="form1" name="form1" method="POST">
<br />
<label>
<textarea name="textarea" id="textarea" cols="45" rows="5">Type message here.</textarea>
</label>
<p>
<label>
<input type="submit" name="button" id="button" value="Submit" />
</label>
</p>
<input type="hidden" name="MM_insert" value="form1" />
</form> </td>
</tr>
</table></td>
</tr>
</table>
</td>
</tr>
<tr>
<td height="19" colspan="2"> </td>
</tr>
</table>
</body>
</html>
This is the page that actually inserts the record into the database.
<?php
if (!isset($_SESSION)) {
session_start();
}
$logoutAction = $_SERVER['PHP_SELF']."?doLogout=true";
if ((isset($_SERVER['QUERY_STRING'])) && ($_SERVER['QUERY_STRING'] != "")){
$logoutAction .="&". htmlentities($_SERVER['QUERY_STRING']);
}
if ((isset($_GET['doLogout'])) &&($_GET['doLogout']=="true")){
$_SESSION['MM_Username'] = NULL;
$_SESSION['MM_UserGroup'] = NULL;
$_SESSION['PrevUrl'] = NULL;
unset($_SESSION['MM_Username']);
unset($_SESSION['MM_UserGroup']);
unset($_SESSION['PrevUrl']);
$logoutGoTo = "index.php";
if ($logoutGoTo) {
header("Location: $logoutGoTo");
exit;
}
}
?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
$insertSQL = sprintf("INSERT INTO messages (message) VALUES (%s)",
GetSQLValueString($_POST['textarea'], "text"));
mysql_select_db($database_riders, $riders);
$Result1 = mysql_query($insertSQL, $riders) or die(mysql_error());
$insertGoTo = "messagepost.php";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Bruces Rider's Social Network. Plan rides, meet new people, or just bike talk.</title>
<style type="text/css">
<!--
body {
background-color: #000000;
}
.style9 {
font-size: 48px;
color: #FFFFFF;
}
.mystyle {
font-family: Arial, Helvetica, sans-serif;
color: #FFFFFF;
font-size: medium;
font-weight: bold;
}
.style11 {color: #FFFFFF}
-->
</style></head>
<body>
<table width="834" height="477" border="0" cellpadding="0" cellspacing="0">
<!--DWLayoutTable-->
<tr>
<td width="241" rowspan="2" valign="top"><table width="100%" border="0" cellpadding="0" cellspacing="0">
<!--DWLayoutTable-->
<tr>
<td width="241" height="192" valign="top"><img src="images/all-stores-bitmap.jpg" width="241" height="192" /></td>
</tr>
<tr>
<td height="92" valign="top"><table width="100%" height="105" border="0" cellpadding="0" cellspacing="0">
<!--DWLayoutTable-->
<tr>
<td width="241" height="22" valign="top"><!--DWLayoutEmptyCell--> </td>
</tr>
<tr>
<td height="49"> </td>
</tr>
<tr>
<td height="34" valign="top"><table width="100%" border="0" cellpadding="0" cellspacing="0">
<!--DWLayoutTable-->
<tr>
<td width="241" height="34" valign="top" class="mystyle"><a href="<?php echo $logoutAction ?>" class="style11">Log out</a></td>
</tr>
</table> </td>
</tr>
</table> </td>
</tr>
</table></td>
<td width="593" height="57" valign="top"><table width="100%" border="0" cellpadding="0" cellspacing="0">
<!--DWLayoutTable-->
<tr>
<td width="589" height="49" valign="top"><div align="center"><span class="style9">Welcome to Bruces Riders!</span></div></td>
</tr>
</table>
<td width="593" height="156"> </td>
</tr>
<tr>
<td width="593" height="113" valign="top" class="mystyle"><?php echo $_SESSION['MM_Username']; ?> ,please feel free to post a message.
<form action="<?php echo $editFormAction; ?>" id="form1" name="form1" method="POST">
<br />
<label>
<textarea name="textarea" id="textarea" cols="45" rows="5">Type message here.</textarea>
</label>
<p>
<label>
<input type="submit" name="button" id="button" value="Submit" />
</label>
</p>
<input type="hidden" name="MM_insert" value="form1" />
</form> </td>
</tr>
</table></td>
</tr>
</table>
</td>
</tr>
<tr>
<td height="19" colspan="2"> </td>
</tr>
</table>
</body>
</html>
This is the page that actually inserts the record into the database.
![]() |
Similar Threads
- Apache (Linux Servers and Apache)
- Double Dropdown Submit ,PHP ,MYSQL (PHP)
- PHP/Mysql Flash problem !!!!1 (PHP)
- php mysql problem (PHP)
- PHP-MySQL Conn Problem in Win XP (PHP)
- PHP + MySQL Problem (PHP)
- installing php and mysql properly (Windows Servers and IIS)
- php mysql help (PHP)
- Using PHP, MySQL and Apache Server (PHP)
Other Threads in the PHP Forum
- Previous Thread: How to print a page from a form details???
- Next Thread: storing multiple email address problem
| Thread Tools | Search this Thread |
advanced ajax apache api array basics beginner binary broken cakephp check checkbox class cms code combobox cookies cron curl database date datepart display dynamic echo email error file files folder form forms function functions google head href htaccess html image include includingmysecondfileinthechain insert integration ip java javascript job joomla js limit link login loop mail menu mlm multiple mysql oop parse password paypal pdf php problem procedure query radio random recursion regex remote script search server sessions smarty smash sms soap source space sql stored syntax system table traffic tutorial unicode update upload url validator variable video web xml youtube






