|
-
Apr 6th, 2009, 10:50 AM
#1
Thread Starter
Lively Member
[RESOLVED] PHP MySQL "Update" Problem
As from the heading you can see that I have a Update problem with my form. The problem is it does not update, it doesn't give me any errors when script executes it says everything has been updated successfully however that is not the case. My add to DB script is working fine but this one is not I dont know why. If someone knows please help.
PHP Code:
<?php
include '../data/data.php';
include 'index.php';
//GET ID FROM THE URL - MAKE SURE WE GET AN INTEGER VALUE
$nid=intval($_GET['id']);
?>
<html>
<head>
<title>Edit</title>
<style type="text/css">
<!--
#table {
border: 1px solid #666666;
width:785px;
border-collapse: collapse;
}
label
{
width: 4em;
float: left;
text-align: right;
margin-right: 0.5em;
display: block
}
.submit input
{
margin-left: 4.5em;
}
input, textarea
{
color: #781351;
background: #fee3ad;
border: 1px solid #781351
}
.submit input
{
color: #000;
background: #ffa20f;
border: 2px outset #d7b9c9
}
fieldset
{
border: 1px solid #781351;
width: 20em
}
legend
{
color: #fff;
background: #ffa20c;
border: 1px solid #781351;
padding: 2px 6px
}
-->
</style>
<style type="text/css">
textarea{
width:250px;
height:100px
width:144px;
height:80px;
overflow:auto;
}
</style>
</head>
<body><br /><br />
<center>
<div>
<?php
if ($nid>0) {
$query=mysql_query("SELECT * FROM `nek` WHERE `nid`='".$nid."' LIMIT 1");
while ($fetch=mysql_fetch_assoc($query)) {
print '<form enctype="multipart/form-data" action="edit.php" method="POST">';
print '<table width="628" border="0" cellpadding="0" cellspacing="0" id="table">';
print " <tr>";
print ' <td width="113">Address:</td>';
print ' <td><textarea id="textarea" type="text" name = "address" />'.$fetch['address'].'';
print ' </textarea></td>';
print ' <td width="133"> </td>';
print ' <td width="219"> </td>';
print " </tr>";
print " <tr>";
print ' <td>Area:</td>';
print ' <td><input type="text" name = "size" value="'.$fetch['size'].'"></td>';
print " <td> </td>";
print " <td> </td>";
print " </tr>";
print " <tr>";
print " <td>Price:</td>";
print ' <td><input type="text" name = "price" value="'.$fetch['price'].'" /></td>';
print " <td> </td>";
print " <td> </td>";
print " </tr>";
print " <tr>";
print " <td>Date:</td>";
print ' <td><input type="text" name = "date" value="'.$fetch['date'].'" /></td>';
print " <td> </td>";
print " <td> </td>";
print " </tr>";
print " <tr>";
print " <td>City:</td>";
print ' <td><input type="text" name = "city" value="'.$fetch['city'].'" /></td>';
print " <td> </td>";
print " <td> </td>";
print " </tr>";
print " <tr>";
print ' <td width="160">Type:</td>';
print ' <td><input type="text" name = "type" value="'.$fetch['type'].'" /></td>';
print " <td> </td>";
print " <td> </td>";
print " </tr>";
print ' <tr valign="top">';
print " <td>Details:</td>";
print ' <td><textarea id="textarea" type="text" name = "description" />'.$fetch['description'].'';
print ' </textarea></td>';
print " <td> </td>";
print " <td> </td>";
print " </tr>";
print " <tr>";
print " <td> </td>";
print " <td> </td>";
print " <td> </td>";
print " <td> </td>";
print " </tr>";
print " <tr>";
print " <td>Main Photo:</td>";
print ' <td><input type="file" name="photo0" value="'.$fetch['photo0'].'"></td>';
print " <td> </td>";
print " <td> </td>";
print " </tr>";
print " <tr>";
print " <td>Photo 1:</td>";
print ' <td><input type="file" name="photo1" value="'.$fetch['photo1'].'"></td>';
print " <td>Photo 4:</td>";
print ' <td width="160"><input type="file" name="photo4" value="'.$fetch['photo4'].'"></td>';
print " </tr>";
print " <tr>";
print " <td>Photo 2:</td>";
print ' <td><input type="file" name="photo2" value="'.$fetch['photo2'].'"></td>';
print " <td>Photo 5:</td>";
print ' <td><input type="file" name="photo5" value="'.$fetch['photo5'].'"></td>';
print " </tr>";
print " <tr>";
print " <td>Photo 3:</td>";
print ' <td><input type="file" name="photo3" value="'.$fetch['photo3'].'"></td>';
print " <td>Photo 6:</td>";
print ' <td><input type="file" name="photo6" value="'.$fetch['photo6'].'"></td>';
print "</tr>";
print "</table><br />";
print '<input type="submit" value="Add">';
print "</form>";
}
} else {
print '<tr><td colspan="4">Error: wrong ID number.</td></tr>';
}
?>
</div>
</center>
</body>
</html>
PHP Code:
<?php
include '../data/data.php';
//GET ID FROM THE URL - MAKE SURE WE GET AN INTEGER VALUE
$nid=intval($_GET['id']);
?>
<html>
<head>
<title>Edit</title>
</head>
<link href="../css/style.css" rel="stylesheet" type="text/css" media="screen">
<body>
<div align="center">
<table cellspacing="0" cellpadding="0" width="599">
<tr>
<td id="topone"><br /><br />
</td>
</tr>
<tr>
<td id="contentone">
<div id="text">
<table border="1" cellspacing="0" cellpadding="0" width="590">
<?php
$query=mysql_query("UPDATE `nek` SET address = '$address', size = '$size', price = '$price', date = '$date', city = '$city', type = '$type', description = '$description', photo0 = '$photo0', photo1 = '$photo1', photo2 = '$photo2', photo3 = '$photo3', photo4 = '$photo4', photo5 = '$photo5', photo6 = '$photo6' WHERE `nid`='".$nid."' LIMIT 1");
print '<tr><td colspan="4">Successfully Deited</td></tr>';
?>
</table>
</div>
</td>
</tr>
<tr>
<td height="27" id="bottomone">
</td>
</tr>
</table>
</div><br /><br /><br />
<center><a href="admin.php">Back</a></center>
</body>
</html>
Thank you for taking interest.
-
Apr 6th, 2009, 10:55 AM
#2
Hyperactive Member
Re: PHP MySQL "Update" Problem
First thing is don't echo/print out soooo much HTML code, there's no point, just close the PHP tags.
Secondly, do this:
PHP Code:
<?php
$query = mysql_query("UPDATE `nek` SET address = '$address', size = '$size', price = '$price', date = '$date', city = '$city', type = '$type', description = '$description', photo0 = '$photo0', photo1 = '$photo1', photo2 = '$photo2', photo3 = '$photo3', photo4 = '$photo4', photo5 = '$photo5', photo6 = '$photo6' WHERE `nid`='".$nid."' LIMIT 1");
if(!$query) { ?>
<tr><td colspan="4">Failed - <?php echo mysql_error(); ?></td></tr>
<?php } else { ?>
<tr><td colspan="4">Successfully Deited</td></tr>
<?php { ?>
?>
» Twitter: @rudi_visser : Website: www.rudiv.se «
If Apple fixes security flaws, they are heralded as proactive. If Microsoft fixes a security flaw, they finally got around to fixing their buggy OS.
-
Apr 6th, 2009, 11:10 AM
#3
Thread Starter
Lively Member
Re: PHP MySQL "Update" Problem
 Originally Posted by RudiVisser
First thing is don't echo/print out soooo much HTML code, there's no point, just close the PHP tags.
Secondly, do this:
PHP Code:
<?php
$query = mysql_query("UPDATE `nek` SET address = '$address', size = '$size', price = '$price', date = '$date', city = '$city', type = '$type', description = '$description', photo0 = '$photo0', photo1 = '$photo1', photo2 = '$photo2', photo3 = '$photo3', photo4 = '$photo4', photo5 = '$photo5', photo6 = '$photo6' WHERE `nid`='".$nid."' LIMIT 1");
if(!$query) { ?>
<tr><td colspan="4">Failed - <?php echo mysql_error(); ?></td></tr>
<?php } else { ?>
<tr><td colspan="4">Successfully Deited</td></tr>
<?php { ?>
?>
Thanks for replying so quickly but the same thing happens "Successfully Edited" message comes up but Noting happens in the DB.
-
Apr 6th, 2009, 11:40 AM
#4
Hyperactive Member
Re: PHP MySQL "Update" Problem
put echo mysql_error(); right under the line $query = ...
» Twitter: @rudi_visser : Website: www.rudiv.se «
If Apple fixes security flaws, they are heralded as proactive. If Microsoft fixes a security flaw, they finally got around to fixing their buggy OS.
-
Apr 6th, 2009, 11:50 AM
#5
Thread Starter
Lively Member
Re: PHP MySQL "Update" Problem
 Originally Posted by RudiVisser
put echo mysql_error(); right under the line $query = ...
I did this:
PHP Code:
<?php
$query = mysql_query("UPDATE `nek` SET address = '$address', size = '$size', price = '$price', date = '$date', city = '$city', type = '$type', description = '$description', photo0 = '$photo0', photo1 = '$photo1', photo2 = '$photo2', photo3 = '$photo3', photo4 = '$photo4', photo5 = '$photo5', photo6 = '$photo6' WHERE `nid`='".$nid."' LIMIT 1");
echo mysql_error();
if(!$query) { ?>
<tr><td colspan="4">Failed - <?php echo mysql_error(); ?></td></tr>
<?php } else { ?>
<tr><td colspan="4">Successfully Edited</td></tr>
<?php } ?>
Same thing happens successful updated but nothing in the DB.
-
Apr 6th, 2009, 11:58 AM
#6
Hyperactive Member
Re: PHP MySQL "Update" Problem
Well if no error's being outputted either then it has to be working...
» Twitter: @rudi_visser : Website: www.rudiv.se «
If Apple fixes security flaws, they are heralded as proactive. If Microsoft fixes a security flaw, they finally got around to fixing their buggy OS.
-
Apr 6th, 2009, 12:05 PM
#7
Thread Starter
Lively Member
Re: PHP MySQL "Update" Problem
 Originally Posted by RudiVisser
Well if no error's being outputted either then it has to be working...
That is what is making me pull my hair out, it doesnt give any errors, but doesn't update.
-
Apr 6th, 2009, 01:27 PM
#8
Re: PHP MySQL "Update" Problem
try setting your sql to a string var and echoing it out to make sure it's right./
-tg
-
Apr 6th, 2009, 04:20 PM
#9
Thread Starter
Lively Member
Re: PHP MySQL "Update" Problem
 Originally Posted by techgnome
try setting your sql to a string var and echoing it out to make sure it's right./
-tg
Did that as well still "update successful" but nothing in the DB
-
Apr 6th, 2009, 04:51 PM
#10
Re: PHP MySQL "Update" Problem
did the sql look right when echoed? also I don't see where the ID gets passed from the form to the processing page...
-tg
-
Apr 6th, 2009, 08:16 PM
#11
Re: PHP MySQL "Update" Problem
the ID is being taken from $_GET['id'].
looking at your query, the syntax is fine, though you should be escaping the user input if you haven't been. the only problem is that MySQL has reserved words that need to be escaped sometimes, which is not always evident if you're using those reserved words as field names. off the top of my head: size, date, and type are all words that might be reserved. just use the tick-quotes for them:
Code:
UPDATE `nek` SET
address = '$address',
`size` = '$size',
price = '$price',
`date` = '$date',
city = '$city',
`type` = '$type',
description = '$description',
photo0 = '$photo0',
photo1 = '$photo1',
photo2 = '$photo2',
photo3 = '$photo3',
photo4 = '$photo4',
photo5 = '$photo5',
photo6 = '$photo6'
WHERE `nid`='".$nid."'
LIMIT 1
other than this, the only thing that could be messing things up is the value of the variables you're inputting. you can make them all safe by calling mysql_real_escape_string() when defining them to be put into a query.
-
Apr 6th, 2009, 09:29 PM
#12
Re: PHP MySQL "Update" Problem
I can see where the OP is GETting the ID from... what I CAN'T see (and it could be that I'm jsut blind at the moment) ... is how that ID is SENT to the form... he's POSTing the data... but I don't see the ID in the url portion at all (which is where it would need to be for a GET)
-tg
-
Apr 7th, 2009, 12:17 AM
#13
Re: PHP MySQL "Update" Problem
ah yes, I stand corrected, I didn't bother looking at the form! the ID is also not a hidden field being sent to the edit script, either.
so, if that's the issue, you can either make the form post to "edit.php?id=$_GET['id']", or add a hidden input that holds the ID, and then access it with $_POST['id'].
Code:
<input type="hidden" name="id" value="<?php echo $_GET['id']; ?>" />
-
Apr 7th, 2009, 05:09 PM
#14
Thread Starter
Lively Member
Re: PHP MySQL "Update" Problem
 Originally Posted by kows
the ID is being taken from $_GET['id'].
looking at your query, the syntax is fine, though you should be escaping the user input if you haven't been. the only problem is that MySQL has reserved words that need to be escaped sometimes, which is not always evident if you're using those reserved words as field names. off the top of my head: size, date, and type are all words that might be reserved. just use the tick-quotes for them:
Code:
UPDATE `nek` SET
address = '$address',
`size` = '$size',
price = '$price',
`date` = '$date',
city = '$city',
`type` = '$type',
description = '$description',
photo0 = '$photo0',
photo1 = '$photo1',
photo2 = '$photo2',
photo3 = '$photo3',
photo4 = '$photo4',
photo5 = '$photo5',
photo6 = '$photo6'
WHERE `nid`='".$nid."'
LIMIT 1
other than this, the only thing that could be messing things up is the value of the variables you're inputting. you can make them all safe by calling mysql_real_escape_string() when defining them to be put into a query.
Did what you have suggested and I still get "Successfully Updated" but nothing happens.
Does anybody have a working example of this that I might try.
-
Apr 7th, 2009, 05:26 PM
#15
Re: PHP MySQL "Update" Problem
read the previous two posts about your ID not being set :)
your form has no "ID" value being submitted and is being submitted via POST, but you're assigning $nid to a variable from the GET array in edit.php. you need to either change your form action to something like this:
PHP Code:
<form action="edit.php?id=<?php echo $nid; ?>" method="post">
or add a hidden input (shown in my last post) to the form and assign $nid in edit.php to $_POST['id'].
hope you followed that!
Last edited by kows; Apr 7th, 2009 at 05:30 PM.
-
Apr 8th, 2009, 07:00 AM
#16
Thread Starter
Lively Member
Re: PHP MySQL "Update" Problem
 Originally Posted by kows
read the previous two posts about your ID not being set
your form has no "ID" value being submitted and is being submitted via POST, but you're assigning $nid to a variable from the GET array in edit.php. you need to either change your form action to something like this:
PHP Code:
<form action="edit.php?id=<?php echo $nid; ?>" method="post">
or add a hidden input (shown in my last post) to the form and assign $nid in edit.php to $_POST['id'].
hope you followed that!
Did that as well except this time when i have executed the script all text fields got deleted.
-
Apr 8th, 2009, 07:11 AM
#17
Re: PHP MySQL "Update" Problem
you'll have to post your code, I don't know what you've changed.
-
Apr 8th, 2009, 07:25 AM
#18
Thread Starter
Lively Member
Re: PHP MySQL "Update" Problem
 Originally Posted by kows
you'll have to post your code, I don't know what you've changed.
PHP Code:
<?php
include '../data/data.php';
include 'index.php';
//GET EMPLOYEE ID FROM THE URL - MAKE SURE WE GET AN INTEGER VALUE
$nid=intval($_GET['id']);
?>
<html>
<head>
<title>Editiraj nekretnine</title>
<style type="text/css">
<!--
#table {
border: 1px solid #666666;
width:785px;
border-collapse: collapse;
}
label
{
width: 4em;
float: left;
text-align: right;
margin-right: 0.5em;
display: block
}
.submit input
{
margin-left: 4.5em;
}
input, textarea
{
color: #781351;
background: #fee3ad;
border: 1px solid #781351
}
.submit input
{
color: #000;
background: #ffa20f;
border: 2px outset #d7b9c9
}
fieldset
{
border: 1px solid #781351;
width: 20em
}
legend
{
color: #fff;
background: #ffa20c;
border: 1px solid #781351;
padding: 2px 6px
}
-->
</style>
<style type="text/css">
textarea{
width:250px;
height:100px
width:144px;
height:80px;
overflow:auto;
}
</style>
</head>
<body><br /><br />
<center>
<div>
<?php
if ($nid>0) {
$query=mysql_query("SELECT * FROM `nek` WHERE `nid`='".$nid."' LIMIT 1");
while ($fetch=mysql_fetch_assoc($query)) {
?>
<form action="edit.php?id=<?php echo $nid; ?>" method="post">
<?php
print '<table width="628" border="0" cellpadding="0" cellspacing="0" id="table">';
print " <tr>";
print ' <td width="113">ADDRESS:</td>';
print ' <td><textarea id="textarea" type="text" name = "address" />'.$fetch['address'].'';
print ' </textarea></td>';
print ' <td width="133"><input type="text" name = "nid" value="'.$fetch['nid'].'"> </td>';
print ' <td width="219"> </td>';
print " </tr>";
print " <tr>";
print ' <td>AREA:</td>';
print ' <td><input type="text" name = "size" value="'.$fetch['size'].'"></td>';
print " <td> </td>";
print " <td> </td>";
print " </tr>";
print " <tr>";
print " <td>PRICE:</td>";
print ' <td><input type="text" name = "price" value="'.$fetch['price'].'" /></td>';
print " <td> </td>";
print " <td> </td>";
print " </tr>";
print " <tr>";
print " <td>DATE:</td>";
print ' <td><input type="text" name = "date" value="'.$fetch['date'].'" /></td>';
print " <td> </td>";
print " <td> </td>";
print " </tr>";
print " <tr>";
print " <td>CITY:</td>";
print ' <td><input type="text" name = "city" value="'.$fetch['city'].'" /></td>';
print " <td> </td>";
print " <td> </td>";
print " </tr>";
print " <tr>";
print ' <td width="160">TYPE:</td>';
print ' <td><input type="text" name = "type" value="'.$fetch['type'].'" /></td>';
print " <td> </td>";
print " <td> </td>";
print " </tr>";
print ' <tr valign="top">';
print " <td>DESCRIPTION:</td>";
print ' <td><textarea id="textarea" type="text" name = "description" />'.$fetch['description'].'';
print ' </textarea></td>';
print " <td> </td>";
print " <td> </td>";
print " </tr>";
print " <tr>";
print " <td> </td>";
print " <td> </td>";
print " <td> </td>";
print " <td> </td>";
print " </tr>";
print "</table><br />";
print '<input type="submit" value="Add">';
print "</form>";
}
} else {
print '<tr><td colspan="4">Error wrong ID.</td></tr>';
}
?>
</div>
</center>
</body>
</html>
PHP Code:
<?php
include '../data/data.php';
//GET EMPLOYEE ID FROM THE URL - MAKE SURE WE GET AN INTEGER VALUE
$nid=intval($_GET['id']);
?>
<html>
<head>
<title>Editram Informacije</title>
</head>
<link href="../css/style.css" rel="stylesheet" type="text/css" media="screen">
<body>
<div align="center">
<table cellspacing="0" cellpadding="0" width="599">
<tr>
<td id="topone"><br /><br />
</td>
</tr>
<tr>
<td id="contentone">
<div id="text">
<table border="1" cellspacing="0" cellpadding="0" width="590">
<?php
include '../data/data.php';
mysql_query("UPDATE `nek` SET address = '$address', `size` = '$size', price = '$price', `date` = '$date', city = '$city', `type` = '$type', description = '$description' WHERE `nid` = '".$nid."' LIMIT 1");
echo mysql_error();
?>
<tr><td colspan="4">Failed - <?php echo mysql_error(); ?></td></tr>
<tr><td colspan="4">Successfully Edited</td></tr>
</table>
</div>
</td>
</tr>
<tr>
<td height="27" id="bottomone">
</td>
</tr>
</table>
</div><br /><br /><br />
<center><a href="admin.php">BACK</a></center>
</body>
</html>
-
Apr 8th, 2009, 08:41 AM
#19
Thread Starter
Lively Member
Re: PHP MySQL "Update" Problem
Just to let everyone know I've fixed it I mean you fixed it. I seems I have forgot to put in
Code:
$address=$_POST['address'];
$size=$_POST['size'];
$price=$_POST['price'];
$date=$_POST['date'];
$city=$_POST['city'];
$type=$_POST['type'];
$description=$_POST['description'];
once I did that everything worked fine!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|