PDA

View Full Version : Posting of Final code for Part 11 PHP a Beginners Guide


djmiller
03-12-2007, 12:46 AM
These tutorials are great, however I have spent quite a bit of time on Part 11.

I am not getting the desired results. No errors, but before I ask a question I would like to compare the final code with what I have. I believe I have a mistake somewhere in the HTML section.

I only have 1 record in the database at this point and here is what I get when viewing the completed part 11 tutorial.http://djmiller.us/phptut/1.jpg

Here is a copy of my final code.


<?php
require_once("Connections/connection.php"); //database connection
////////////////////
$data2 = $_POST['data2'];
$data3 = $_POST['data3'];
$data4 = $_POST['data4'];
$submit = $_POST['submit'];
$del = $_GET['del'];
$upd = $_GET['upd'];
$update_id = $_POST['update_id'];

///////////////////////////
$query = sprintf("SELECT * FROM table1 where field1_id='$upd'");
$result = @mysql_query($query);
$rowUpdate = mysql_fetch_array($result);
/////////////////////////////

if ($submit && $data2 && $data3 && $data4 && ! $update_id){
/////////////////////////
$query = sprintf("INSERT into table1 (field2, field3, field4) values ('$data2', '$data3', '$data4')");
mysql_query($query)or die(mysql_error());
///////////////////////
}elseif($submit && $update_id){
///////////////////////////
$query = sprintf("UPDATE table1 set field2='$data2', field3='$data3', field4='$data4' where field_id = '$update_id'");
$result = @mysql_query($query);
/////////////////////////////
}
/// delete record
if ($del){
/////////////////////////
$query = sprintf("DELETE FROM table1 where field1_id='$del'");
mysql_query($query)or die(mysql_error());
///////////////////////
}
///////////////////////////
$query = sprintf("SELECT * FROM table1");
$result = @mysql_query($query);
$rowUpdate = mysql_fetch_array($result);
/////////////////////////////

?>
<!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=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<p></p>
<form id="form1" name="form1" method="post" action="<?php $_SERVER["PHP_SELF"]; ?>">
<table height="104" border="1">
<tr>
<td>data2</td>
<td>data3</td>
<td>data4</td>
</tr>
<tr>
<td><input name="data2" type="text" id="data2" value="<?php echo $rowUpdate['field2']; ?>" /></td>
<td><input name="data3" type="text" id="data3" value="<?php echo $rowUpdate['field3']; ?>" /></td>
<td><input name="data4" type="text" id="data4" value="<?php echo $rowUpdate['field4']; ?>" /></td>
</tr>
<tr>
<td colspan="3"><div align="center">
<label>
<input type="reset" name="Submit4" value="Reset" />
<input name="Submit" type="submit" id="Submit" value="Submit" />
</label>
<input name="submit" type="hidden" id="submit" value="1" />
<input name="update_id" type="hidden" id="update_id" value ="<?php echo $rowUpdate['field1_id']; ?>" />
</div></td>
</tr>
</table>
</form>
<table width="458" border="1">
<tr>
<td width="60"></td>
<td width="60">ID1</td>
<td width="82">data2</td>
<td width="82">data3</td>
<td width="82">data4</td>
<td width="52"></td>
</tr>
<?php do { ?>
<tr>
<td>

<input type="button" name="Submit3" value="Update"onClick="document.location.href='dwc.php?upd=<?php echo $row['field1_id']?>'"/>
</td>
<td><?php echo $row['field1_id'];?></td>
<td><?php echo $row['field2'];?></td>
<td><?php echo $row['field3'];?></td>
<td><?php echo $row['field4'];?></td>
<td>

<input type="button" name="Submit2" value="Delete"onClick="document.location.href='dwc.php?del=<?php echo $row['field1_id']?>'"/>
</td>
</tr>
<?php }while ($row = mysql_fetch_array($result)); ?>
</table>
</body>
</html>

Thanks.... great tutorial. Hopefully posting the correct code could help others.

great Job David!

Dan Miller

davidj
03-12-2007, 08:56 AM
you have 2 select statements which have the same naming convension


///////////////////////////
$query = sprintf("SELECT * FROM table1 where field1_id='$upd'");
$result = @mysql_query($query);
$rowUpdate = mysql_fetch_array($result);
/////////////////////////////

///////////////////////////
$query = sprintf("SELECT * FROM table1");
$result = @mysql_query($query);
$rowUpdate = mysql_fetch_array($result);
/////////////////////////////


Please return to the tutorials to see how i did it.

Remember these tuts are not intended to be followed autonomously

djmiller
03-13-2007, 02:18 AM
David,

After another 90 minutes of rewatching the VTM for Part 11 I am sorry to say I am still lost. My code looks to me line for line the same as in your tutorial code. You also have the same two select statments in your tutorial.

Thanks,
Dan

davidj
03-13-2007, 08:34 AM
all i can do is ask someone who has covered the tutorials to post the code as i dont have access to the tutorials any more

if it is working in the video and is not working on your machine and the code looks the same then it means one thing... The code is not the same. You are probably missing something.

Can anyone help here

lux
03-13-2007, 09:05 AM
welcome.php

<?php
session_start();

require_once('connections/conn.php'); //database connection
include('includes/security.php');



$id = $_SESSION['id'];

$query = sprintf("SELECT * FROM users WHERE user_id='$id'");
$result = mysql_query($query);
$rowAccount = mysql_fetch_array($result);

?>
<!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=iso-8859-1" />
<title>Welcome to this Application</title>
</head>

<body>
<p>Welcome to the application <?php echo $rowAccount['user_name']; ?> </p>
<p>&nbsp;</p>
<form name="form1" id="form1" method="post" action="">
<input name="Button" type="button" id="Button" value="Log off" onclick="document.location.href='logoff.php'" />
</form>

</body>
</html>


login.php

<!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=iso-8859-1" />
<title>Login</title>
<?php
require_once("Connections/CONN.php"); // database connection

session_start();

$userId = $_POST['userId'];
$password = $_POST['password'];
$submitted = $_POST['submitted'];

if ($userId && $password){

$query = sprintf("SELECT * FROM users WHERE user_name ='$userId' and user_password ='$password'");
$result = mysql_query($query);
$rowAccount = mysql_fetch_array($result);

}

if ($rowAccount) {

$_SESSION['id'] = $rowAccount['user_id'];

header("location:welcome.php");
exit;

} elseif ($submitted){

echo "You dont exist in the application so your not getting in !";

}

?>
</head>

<body>
<form name="form1" id="form1" method="post" action="<?php $_SERVER['PHP_SELF'] ?>">
<table width="180" border="1" align="center" cellpadding="1" cellspacing="0">
<tr>
<td>User ID</td>
<td><input name="userId" type="text" id="userId" /></td>
</tr>
<tr>
<td>Password</td>
<td><input name="password" type="text" id="password" /></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input name="submitted" type="hidden" id="submitted" value="1" />
<input type="submit" name="Submit" value="Submit" /></td>
</tr>
</table>
</form>
</body>
</html>


logoff.php



<?php

session_start();

session_destroy();

header('location:login.php');
exit;

?>
<!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=iso-8859-1" />
<title>Log off</title>

</head>

<body>

</body>
</html>


security.php

<?php

if(! isset($_SESSION['id'])) {

header('location:Login.php');
exit;

}

?>