Log in

View Full Version : Save Button



rhodarose
05-04-2011, 07:04 AM
Good day!

I encountered Problem in my code. When i click the save button the data was not save in my database and also it did not appear automatically on my page3.php form.

Here is my page3.php code:


<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
<script type="text/JavaScript">
function confirmDelete(){
var agree = confirm("Are you sure you want to delete this file?");
if(agree){
// direct browser to delete.php
window.location = "./delete.php";
} else {
// do nothing and return false
return false ;
}
}
</script>
</head>
<body>
<form name="machine1" action="page3.php" method="post">
<table border="1">
<tr>
<td>Emp ID</td>
<td>Last Name</td>
<td>First Name</td>
<td>Birthday</td>
<td>Option</td>
</tr>

<?php
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("db_machine1") or die(mysql_error());

$data_p = mysql_query("SELECT * FROM tbl_machine1") or die(mysql_error());
while($info = mysql_fetch_array( $data_p ))
{
$emp_id = $info['Emp_ID'];
$lname = $info['Last_Name'];
$fname = $info['First_Name'];
$bday = $info['Birthday'];
?>
<tr>
<td><?php echo $emp_id;?> </td>
<td><?php echo $lname;?> </td>
<td><?php echo $fname;?> </td>
<td><?php echo $bday;?> </td>
<td><a href = 'edit.php?id=$emp_id'>Edit</a> <a href='delete.php?id=$emp_id' onClick='confirmDelete();'>Delete</a></td>
</tr>
<?php
}
?>


</table>
<A HREF="javascript:void(0)" onclick="window.open('add.php','welcome','width=300,height=200')">
<input type="button" name="add" value="ADD"> </A>
</body>
</html>


and here is my add.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=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form method="post" action="page3.php" name="add">
Last Name:&nbsp;<input type="text" name="Last_Name" id="Last_Name"><br/><br/>
First Name:&nbsp;<input type="text" name="First_Name" id="First_Name"><br/><br/>
Birthday:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="text" name="Birthday" id="Birthday"><br/><br/>
<input type="submit" name="save" value="SAVE">

<?php
error_reporting(0);
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("db_machine1") or die(mysql_error());

$Lname=$_POST['Last_Name'];
$Fname=$_POST['First_Name'];
$bday=$_POST['Birthday'];

$query = "INSERT INTO tbl_machine1 (Last_Name, First_Name, Birthday) VALUES ('$Lname','$Fname','$bday')";
$result=mysql_query($query);

//header ('location:page3.php');

?>
</form>
</body>
</html>


I put error_reporting(0); in my add.php because when theres no error_reporting(0) theres a notice appear that undefined index $Lname, $Fname, $bday

In that code happened is it add a emp_id and the bday was 0000-00-00.

I hope somebody can help me..

Thank you

fastsol1
05-05-2011, 01:59 AM
You really need to start wrapping your code in if() to check if a variable is set, then you can get rid of the error_reporting, the undefined index only happens on a local server not on paid hosting servers cause they suppress that level of reporting.
This is what I mean

if(isset($_POST))
{
$Lname=$_POST['Last_Name'];
$Fname=$_POST['First_Name'];
$bday=$_POST['Birthday'];

$query = "INSERT INTO tbl_machine1 (Last_Name, First_Name, Birthday) VALUES ('$Lname','$Fname','$bday')";
$result=mysql_query($query);

//header ('location:page3.php');
}


This can be anything that you want to see if has been given a value to the server

if(isset($_POST))
Or

if(isset($_POST['submit']))
Or

if(isset($any_var_you_want))

As for the other issue, put

echo mysql_error();
after the failing query and it will tell you why.

rhodarose
05-05-2011, 04:55 AM
I tried this code:



<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

<script language="JavaScript">
function CloseChildWindow()
{
window.opener.location.href="page3.php";
self.close();
}
</script>
</head>

<body>
<form method="post" action="add.php" name="add">
Last Name:&nbsp;<input type="text" name="Last_Name" id="Last_Name"><br/><br/>
First Name:&nbsp;<input type="text" name="First_Name" id="First_Name"><br/><br/>
Birthday:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="text" name="Birthday" id="Birthday"><br/><br/>
<input type="submit" name="save" value="SAVE" onClick="CloseChildWindow();">
<?php
//error_reporting(0);
$Lname = "";
$Fname= "";
$bday = "";


mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("db_machine1") or die(mysql_error());

if(isset($_POST['save'])){
$Lname=$_POST['Last_Name'];
$Fname=$_POST['First_Name'];
$bday=$_POST['Birthday'];
//$bday = date("d m Y", strtotime($info['Birthday']));



$date = date('Y-m-d', strtotime($bday));
//$query = "INSERT INTO tbl_machine1 (Last_Name, First_Name, Birthday) VALUES ('$Lname','$Fname','$bday')";
//$result=mysql_query($query);

$sql = mysql_query("INSERT INTO tbl_machine1 (Last_Name, First_Name, Birthday)
VALUES('".$Lname."', '".$Fname."', '".$date."')");
}

//header ('location:page3.php');

?>
</form>
</body>
</html>


Before it work but now it did not work...I don't know why...

Beverleyh
05-05-2011, 10:12 AM
If it worked before your changes, revert back to the original code and then gradually introduce your changes until the point where it doesnt - I find this helps with troubleshooting.

rhodarose
05-09-2011, 04:05 AM
I resolved my problem by this code:


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

<link rel="stylesheet" href="ui.all.css" type="text/css" media="screen" />

<script language="JavaScript">
function CloseChildWindow()
{
window.opener.location.href="machine1.php";
self.close();
}
</script>
</head>

<body>
<form method="post" action="add.php" name="add">
Last Name:&nbsp;<input type="text" name="Last_Name" id="Last_Name"><br/><br/>
First Name:&nbsp;<input type="text" name="First_Name" id="First_Name"><br/><br/>
Birthday:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="text" name="date" id="date"><br/><br/>
<input type="submit" name="save" value="SAVE" onClick="CloseChildWindow();">
<?php
//error_reporting(0);
$Lname = "";
$Fname= "";
$bday = "";

include 'connection.php';

if(isset($_POST['save'])){
$Lname=$_POST['Last_Name'];
$Fname=$_POST['First_Name'];
$bday=$_POST['date'];



$date = date('Y-m-d', strtotime($bday));

$Lname = mysql_real_escape_string($Lname);
$Fname = mysql_real_escape_string($Fname);
$date = mysql_real_escape_string($date);

$sql = mysql_query("INSERT INTO tbl_machine1 (Last_Name, First_Name, Birthday)
VALUES('".$Lname."', '".$Fname."', '".$date."')");

}

?>
</form>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.5.3/jquery-ui.min.js"></script>

<script type="text/javascript">

$(document).ready(function(){

$("#date").datepicker({ showOn: 'button', buttonText: "Select your Birthday" });

});

</script>
</body>
</html>