Jump to content

Creating a simple system.


al3x8730

Recommended Posts

I wanted to create something simple, I already made a base, but I'm not sure it would fit with what I need help with.

 

I was thinking something like this: You can type in a name and it will display information that people wrote about that person. And you can have the option to add a paragraph or whatever about that person. I know php somewhat, but I'm not good with MySql.

 

I did something SUPER basic.

 



<?php

$username = $_POST['username'];



if ($username == "example") {

echo "information about this user";

} else {

echo "No user was found"; 

}

?>

 

That only works if you personally enter everything, and thus people can't add information to it. If anyone wants to help, it's appreciated, thanks.

Link to comment
https://forums.phpfreaks.com/topic/121653-creating-a-simple-system/
Share on other sites

If you have hundreds of people, then having the search facility by allowing people to type the name is fine.  If you have only a few people, then I would query the database and display the results in a drop down menu.  By selecting the person from the list, it displays the details and shows a form for adding info about the person.  As awpti says, it is not that difficult.

 

I was bored so have done a simple example using the select menu:

 

SQL tables (for database dbcomments)

CREATE TABLE `tblcomments` (
  `cID` int(11) NOT NULL auto_increment,
  `uID` int(11) NOT NULL,
  `comment` longtext NOT NULL,
  PRIMARY KEY  (`cID`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;

--
-- Dumping data for table `tblcomments`
--

INSERT INTO `tblcomments` (`cID`, `uID`, `comment`) VALUES
(1, 1, 'test comment'),
(2, 1, 'another comment'),
(3, 1, 'final comment');

-- --------------------------------------------------------

--
-- Table structure for table `tbluser`
--

CREATE TABLE `tbluser` (
  `uID` int(11) NOT NULL auto_increment,
  `uname` varchar(100) NOT NULL,
  PRIMARY KEY  (`uID`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

--
-- Dumping data for table `tbluser`
--

INSERT INTO `tbluser` (`uID`, `uname`) VALUES
(1, 'Bob'),
(2, 'Dave');

 

 

index.php

<?php
$dbHost = "localhost"; // Database Connection Details
$dbUser = "USERNAME"; // Database Connection Details
$dbPass = "PASSWORD"; // Database Connection Details
$dbname = "dbcomments"; // Database Connection Details

$db = mysql_connect($dbHost,$dbUser,$dbPass); // Connection Code
mysql_select_db($dbname,$db); // Connects to database

$uID = $_POST['uID'];

if(isset($_POST['add'])) {
foreach($_POST as $key=>$value) {
	$$key = $value;
}
$sql3 = mysql_query("INSERT INTO tblcomments (cID, uID, comment) VALUES ('', '$uID', '$comment')");
$successmsg = 'Comment Added';
}

if($uID != '') {
$sql = mysql_query("SELECT * FROM tbluser, tblcomments WHERE tbluser.uID = tblcomments.uID && tbluser.uID = $uID");
$row = mysql_fetch_array($sql);
}

echo '<!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-Language" content="en-us" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Comments</title>
</head>

<body>

<form name="chooseuser" action="index.php" method="post">
<div>Choose User: <select class="txtselect" name="uID">';
	$sql2 = mysql_query("SELECT * FROM tbluser");
	$numrows2 = mysql_num_rows($sql2);

	if ($numrows2 == 0) {
echo '<option value="">No Users</option>';
	} else {
		echo '<option value="">Choose User</option>';
		while($row2 = mysql_fetch_array($sql2)) {
			echo '<option ' . ($row['uID']==$row2['uID'] ? 'selected' : '') . ' value="'.$row2['uID'].'">'.$row2['uname'].'</option>'; 
		}
	}
echo '</select>
<input class="subbtn" type="submit" name="submit" value="Submit" /></div>
</form>';

if($uID != '') {
echo $successmsg;
$sql4 = mysql_query("SELECT * FROM tblcomments WHERE uID = $uID ORDER BY cID DESC");
$numrows4 = mysql_num_rows($sql4);
echo '<hr />';
if($numrows4 == 0) {
	echo 'No Comments';
} else {
	while($row4 = mysql_fetch_array($sql4)) {
		echo $row4['comment'].'<br /><hr />';
	}
}
echo '<form name="addcomment" action="index.php" method="post">
<input type="hidden" name="uID" value="'.$uID.'" />
<textarea name="comment"></textarea><br />
<input type="submit" name="add" value="Add Comment" />
</form>';
}

echo '</body>
</html>';
?>

 

Please bear in mind that the above code does not have any validation or error checking.

 

Hope this helps,

 

John

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.