dont how to update multiple data into database
Posted: Thu Nov 24, 2011 1:23 pm
hi,guys.currently i'm working on php code for update information but it seem doesnt work.For your information, I want to update data has been already inserted into database. I tried to fix it many times but still unsolved.i dunno what is wrong. Please look my code and help me.thanks.
<?php
session_start();
if((@$_POST["id"]==""))
{
?>
<script type=text/javascript>
function check()
{
if(form1.property.value=="")
{
alert("Fill in all * field(s)");
return false;
}
else
return true;
}
</script>
<?php
$conn=mysql_connect("localhost","root","");
if(!$conn)
die("connection error:".mysql_error());
else
{
$db=mysql_select_db("gymnasium");
if(!$db)
die("DB not found:".mysql_error());
else
{
$query="select id, description, name from package order by id";
$result= mysql_query($query);
if(!$result)
die("Invalid query:".mysql_error());
else
if(mysql_num_rows($result)==0)
echo"No record found in database";
else
{
?>
<form name=form1 action=updatepackage.php method=post onsubmit="return check()">
ID Package*:<select name=id><option></option>
<?php
while($row=mysql_fetch_array($result,MYSQL_BOTH))
echo"<option value=".$row[0].">".$row[0]."</option>";
?>
</select><br>
<input type=hidden name=action value=display>
<input type=submit value=display><input type=reset>
</form>
<?php
}
}
}
}
elseif(($_POST["id"]!="")&&($_POST["action"]=="display"))
{
$conn=mysql_connect("localhost","root","");
if(!$conn)
die("connection error:".mysql_error());
else
$db=mysql_select_db("gymnasium");
if(!$db)
die("DB not found:".mysql_error());
else
{
$query="select * from package where id='".$_POST["id"]."'";
$result=mysql_query($query);
$row=mysql_fetch_array($result,MYSQL_BOTH);
?>
<form name=form1 action=updatepackage.php method=post onsubmit="return check()">
ID Package*:<?php echo $row[0]?><br>
Package Name* :<input type=text name=name value='<?php echo $row[1]?>'><br>
Description* :<textarea name="description" cols="40" rows="5"><?php echo $row[2]?></textarea><br>
Price:<input type=text name=name value='<?php echo $row[3]?>'><br>
<input type=hidden name=action value=edit>
<input type=hidden name=id value='<?php echo $row[0]?>'>
<input type=submit value=Edit><input type=Reset>
</form>
<?php
}
}
else if(($_POST["id"]!="")&&($_POST["action"]=="edit"))
{
$conn=mysql_connect("localhost","root","");
if(!$conn)
{
die("connection error:".mysql_error());
}
else
{
$db=mysql_select_db("gymnasium");
if(!$db)
{
die("DB not found:".mysql_error());
}
else
{
$query="update package set name, description='".$_POST["name"]."','".$_POST["description"]."' where id='".$_POST["id"]."'";
$result=mysql_query($query);
if(!$result)
{
die("Invalid query:".mysql_error());
}
else
{
echo"Record in database has been edited";
}
}
}
}
else
echo"No session or session is expired.Please log in again";
?>
<?php
session_start();
if((@$_POST["id"]==""))
{
?>
<script type=text/javascript>
function check()
{
if(form1.property.value=="")
{
alert("Fill in all * field(s)");
return false;
}
else
return true;
}
</script>
<?php
$conn=mysql_connect("localhost","root","");
if(!$conn)
die("connection error:".mysql_error());
else
{
$db=mysql_select_db("gymnasium");
if(!$db)
die("DB not found:".mysql_error());
else
{
$query="select id, description, name from package order by id";
$result= mysql_query($query);
if(!$result)
die("Invalid query:".mysql_error());
else
if(mysql_num_rows($result)==0)
echo"No record found in database";
else
{
?>
<form name=form1 action=updatepackage.php method=post onsubmit="return check()">
ID Package*:<select name=id><option></option>
<?php
while($row=mysql_fetch_array($result,MYSQL_BOTH))
echo"<option value=".$row[0].">".$row[0]."</option>";
?>
</select><br>
<input type=hidden name=action value=display>
<input type=submit value=display><input type=reset>
</form>
<?php
}
}
}
}
elseif(($_POST["id"]!="")&&($_POST["action"]=="display"))
{
$conn=mysql_connect("localhost","root","");
if(!$conn)
die("connection error:".mysql_error());
else
$db=mysql_select_db("gymnasium");
if(!$db)
die("DB not found:".mysql_error());
else
{
$query="select * from package where id='".$_POST["id"]."'";
$result=mysql_query($query);
$row=mysql_fetch_array($result,MYSQL_BOTH);
?>
<form name=form1 action=updatepackage.php method=post onsubmit="return check()">
ID Package*:<?php echo $row[0]?><br>
Package Name* :<input type=text name=name value='<?php echo $row[1]?>'><br>
Description* :<textarea name="description" cols="40" rows="5"><?php echo $row[2]?></textarea><br>
Price:<input type=text name=name value='<?php echo $row[3]?>'><br>
<input type=hidden name=action value=edit>
<input type=hidden name=id value='<?php echo $row[0]?>'>
<input type=submit value=Edit><input type=Reset>
</form>
<?php
}
}
else if(($_POST["id"]!="")&&($_POST["action"]=="edit"))
{
$conn=mysql_connect("localhost","root","");
if(!$conn)
{
die("connection error:".mysql_error());
}
else
{
$db=mysql_select_db("gymnasium");
if(!$db)
{
die("DB not found:".mysql_error());
}
else
{
$query="update package set name, description='".$_POST["name"]."','".$_POST["description"]."' where id='".$_POST["id"]."'";
$result=mysql_query($query);
if(!$result)
{
die("Invalid query:".mysql_error());
}
else
{
echo"Record in database has been edited";
}
}
}
}
else
echo"No session or session is expired.Please log in again";
?>