Page 1 of 1

dont how to update multiple data into database

Posted: Thu Nov 24, 2011 1:23 pm
by zorokun
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";
?>

Re: dont how to update multiple data into database

Posted: Thu Nov 24, 2011 4:32 pm
by social_experiment

Code: Select all

<?php
 $sql = 'UPDATE table SET name = '" . $_POST['name'] . "', description = '" . $_POST['description'] . "' WHERE id = '" . $_POST['id'] . "' ';
?>
Escape all data you insert into the database : mysql_real_escape_string() & use the PHP Code button, it makes your code easier to read :mrgreen:

Re: dont how to update multiple data into database

Posted: Fri Nov 25, 2011 12:05 am
by namikun
thanks so much social_experiment!it work perfectly! May I ask you.what you mean by "Escape all data you insert into the database : mysql_real_escape_string() & use the PHP Code button"?can u give example?im beginner in php and in learning process.thanks!

Re: dont how to update multiple data into database

Posted: Fri Nov 25, 2011 3:13 am
by Hermit TL
can u give example?

Code: Select all

$sql = 'UPDATE table SET name = '" . mysql_real_escape_string($_POST['name']) . "', description = '" . mysql_real_escape_string($_POST['description']) . "' WHERE id = '" . mysql_real_escape_string($_POST['id']) . "' ';
mysql_real_escape_string() is a function that should be used on variables that are user supplied and being used in a query as a security precaution to strip off 'unsafe' tags that can potentially be used for SQL injection.

The PHP code button is at the top of the editor next to the bold button, italic button, underline, Quote, Text, ect. (If you are using the quick edit it will not show, use the full editor.) See how the code example posted above is in a nice box so it can actually be read, that is what the PHP button does.

Re: dont how to update multiple data into database

Posted: Fri Nov 25, 2011 5:22 am
by namikun
thank you hermit TL for the example! :D

Re: dont how to update multiple data into database

Posted: Fri Nov 25, 2011 5:36 am
by Hermit TL
Anytime! I'm always happy to help when I can. Don't be afraid to ask (what may be considered) stupid questions (I've asked my share) and everyone here is extremely helpful regardless of your skill level.
And welcome to the forums. This is place is great! I hope you like it as much I do and stick around for a while. Good luck with your application.