Forum Moderators: coopster
Any help woould be great.
script below;
<?php
//start the session
session_start();
// db properties
$dbhost = '#*$!#*$!#*$!x';
$dbuser = '#*$!#*$!#*$!xx';
$dbpass = '#*$!#*$!#*$!#*$!';
$dbname = '#*$!#*$!#*$!#*$!';
// make a connection to mysql here
$conn = mysql_connect ($dbhost, $dbuser, $dbpass) or die ("I cannot connect to the database because: " . mysql_error());
mysql_select_db ($dbname) or die ("There is an error with '$dbname' this has been noted, please check back later");
//log user in ---------------------------------------------------
function login($user, $pass){
// escape all data in variables to prevent mysql injection
$user = mysql_real_escape_string($user);
$pass = mysql_real_escape_string($pass);
$pass = md5($pass);
// check if the user id and password combination exist in database
$sql = "SELECT username FROM members WHERE username = '$user' AND password = '$pass'";
$result = mysql_query($sql) or die('<font color="red">You are not authorised to access the database.</font> ' . mysql_error());
if (mysql_num_rows($result) == 1) {
// the username and password match,
// set the session
//get the memberID from database
$getid = mysql_query("SELECT * FROM members WHERE username = '$user'");
while($row = mysql_fetch_object($getid)){
//assign memberID to a variable
$memberID = $row->memberID;
//set the session
$_SESSION['isloggedin'] = $memberID;
}
// reload the page
header("Location: http://www.example.co.uk/test/member-area.php");
exit;
} else {
//make error message avalible outside of function
global $errorMessage;
// define an error message
$errorMessage = '<p><font color="red">The password OR username provided was not recognised</font></p>';
}
}
?>
<?php
if (!isset($_SESSION['isloggedin'])){
?>
<form action="<?php $_SERVER['PHP_SELF'];?>" method="post">
<p>Login: Username
<input name="user" type="text" size="10" />
Password
<input type="password" name="pass" size="10" /><br><br>
<input type="submit" name="slogin" value="Login" />
</p>
</form><br><br>
<?php
}
if (isset($_POST['slogin'])){
login($user, $pass);
}
//if login failed
if (isset($errorMessage)) {
echo "<p><span class=\"warning\">$errorMessage</span></p>\n";
}
?>
[edited by: coopster at 1:15 pm (utc) on June 26, 2009]
[edit reason] please use example.com, thanks! [/edit]
I was wondering how i could add 2 buttons to this script. 1 that would delete the record and the other that would amend the record
<?php
$mysqli = mysqli_connect("#*$!#*$!#*$!#*$!#*$!#*$!");
if (mysqli_connect_errno()) {
printf("Connect Failed: %s\n", mysqli_connect_error());
exit();
} else {
$sql = "SELECT * FROM job_board";
$res = mysqli_query($mysqli, $sql);
if($res) {
while ($newArray = mysqli_fetch_array($res, MYSQLI_ASSOC)) {
$id = $newArray['id'];
$title = $newArray['title'];
$short = $newArray['short'];
$salary = $newArray['salary'];
$end_date = $newArray['end_date'];
echo "<div id='border'>";
echo "<div class='bg'><div class='subject'>".$title. " £" .$salary."</div></div>";
echo "<div class='body'>".$short."<div class='morelink'><a href=\"template/job-info.php?id=".$id."\">I'm Interested</a></div></div>";
echo "<div class='bg'><div class='footer'><a href='mailto:info@example.co.uk'>Click to respond</a></div> </div>";
echo "</div><br>";
}
} else {
printf("Could not retrieve records: %s\n", mysqli_error($mysqli));
}
mysqli_free_result($res);
mysqli_close($mysqli);
}
?>
[edited by: jatar_k at 2:15 pm (utc) on June 26, 2009]
each form would post to a script that would perform the function you want. Or to a single form that has some kind of switch to do the different functions.
for delete you really just run a one liner with error checking to make sure someone doesn't use it to delete more than you intended