PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
dannyd
Forum Commoner
Posts: 56 Joined: Wed Jan 23, 2008 11:31 am
Post
by dannyd » Tue Mar 25, 2008 12:58 pm
I know its not the best way of coding but my delete and insert work but not update .. can anyone tell me what i could be doing wrong ?
Code: Select all
<?PHP
require_once($_SERVER['DOCUMENT_ROOT'] . 'db.php');
header("Pragma: nocache");// HTTP/1.0
header("Cache-Control: no-cache, must-revalidate");
$clid = "342";
if ($submit) {
// here if no ID then adding else we're editing
echo $attribute_id;
if (!$attribute_id) {
$sql = "INSERT INTO product_attributes (clid,attribute_name,price,url,sort) VALUES ('$clid', '$attribute_name', '$price', '$url','$sort')";
} elseif ($update) {
$sql = "UPDATE product_attributes SET (attribute_id='$attribute_id',clid='$clid',price='$price',url='$url',sort='$sort')WHERE attribute_id=$attribute_id";
}
$result = mysql_query($sql);
} elseif ($delete) {
// delete a record
$sql = "DELETE FROM product_attributes WHERE attribute_id=$attribute_id";
$result = mysql_query($sql);
} else {
// this part happens if we don't press submit
if (!$attribute_id) {
// print the list if there is not editing
$result = mysql_query("SELECT * FROM product_attributes WHERE clid=" . $clid . " ORDER BY sort ASC");
while ($myrow = mysql_fetch_array($result)) {
echo '<input type="text" name="attribute_name" value="' . $myrow["attribute_name"] . '"><input type="text" name="price" value="' . $myrow["price"] . '"><input type="text" name="url" value="' . $myrow["url"] . '"><input type="text" name="sort" size="3" value="' . $myrow["sort"] . '">';
printf("<a href=\"%s?attribute_id=%s&delete=yes\">(DELETE)</a>", $PHP_SELF, $myrow["attribute_id"]);
printf("<a href=\"%s?attribute_id=%s&update=yes\">(UPDATE)</a><BR>", $PHP_SELF, $myrow["attribute_id"]);
}
}
?>
<form method="post" action="<?php echo $PHP_SELF?>">
<?php
if (!$attribute_id) {
// editing so select a record
$sql = "SELECT * FROM product_attributes WHERE clid=$clid ORDER by sort ASC";
$result = mysql_query($sql);
$myrow = mysql_fetch_array($result);
$clid = $myrow["clid"];
$attribute_name = $myrow["attribute_name"];
$price = $myrow["price"];
$url = $myrow["url"];
// print the id for editing
?>
<?php
}
?>
<input type="hidden" name="clid" value="<?php echo $clid ?>"><br>
Attribute:<input type="Text" name="attribute_name"><br>
Price:<input type="Text" name="price"><br>
URL:<input type="Text" name="url"><br>
Order:<input type="Text" name="sort"><br>
<input type="Submit" name="submit" value="Add new row">
</form>
<?php
}
?>
</body>
</html>
Last edited by
dannyd on Thu Mar 27, 2008 9:03 am, edited 1 time in total.
flying_circus
Forum Regular
Posts: 732 Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR
Post
by flying_circus » Tue Mar 25, 2008 5:04 pm
Try:
Code: Select all
<?php
$sql = "UPDATE product_attributes SET attribute_id='$attribute_id', clid='$clid', price='$price', url='$url', sort='$sort' WHERE attribute_id=$attribute_id";
?>
Zoxive
Forum Regular
Posts: 974 Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan
Post
by Zoxive » Tue Mar 25, 2008 5:18 pm
Code: Select all
$result = mysql_query($sql) or die("Mysql Error<br/> \n" . mysql_error());
Always always always show errors when working on code. So you can see what is wrong right away and correct it.
dannyd
Forum Commoner
Posts: 56 Joined: Wed Jan 23, 2008 11:31 am
Post
by dannyd » Wed Mar 26, 2008 1:19 pm
my update query is fine. I think its a logic problem. I can insert and delete just fine. What I'm trying to do is update the same way I'm deleting. When I click update I can see ?attribute_id=37&update=yes .. but nothing happens in the if($update) clause... any ideas. Below is the script again.
Code: Select all
<?PHP
header("Pragma: nocache");// HTTP/1.0
header("Cache-Control: no-cache, must-revalidate");
$clid = "342";
if ($submit) {
// here if no ID then adding else we're editing
if (!$attribute_id) {
$sql = "INSERT INTO product_attributes (clid,attribute_name,price,url,sort) VALUES ('$clid', '$attribute_name', '$price', '$url','$sort')";
$result = mysql_query($sql);
} elseif ($update) {
echo 'updating';
$attribute_id = $_REQUEST['$attribute_id'];
echo $attribute_id;
exit;
$sql = "UPDATE product_attributes SET attribute_id='$attribute_id', clid='$clid', price='$price', url='$url', sort='$sort' WHERE attribute_id=$attribute_id";
$result = mysql_query($sql);
}
} elseif ($delete) {
// delete a record
$sql = "DELETE FROM product_attributes WHERE attribute_id=$attribute_id";
$result = mysql_query($sql);
} else {
// this part happens if we don't press submit
if (!$attribute_id) {
// print the list if there is not editing
$result = mysql_query("SELECT * FROM product_attributes WHERE clid=" . $clid . " ORDER BY sort ASC");
while ($myrow = mysql_fetch_array($result)) {
echo '<input type="text" name="attribute_name" value="' . $myrow["attribute_name"] . '"><input type="text" name="price" value="' . $myrow["price"] . '"><input type="text" name="url" value="' . $myrow["url"] . '"><input type="text" name="sort" size="3" value="' . $myrow["sort"] . '">';
printf("<a href=\"%s?attribute_id=%s&delete=yes\">(DELETE)</a>", $PHP_SELF, $myrow["attribute_id"]);
printf("<a href=\"%s?attribute_id=%s&update=yes\">(UPDATE)</a><BR>", $PHP_SELF, $myrow["attribute_id"]);
}
}
?>
<form method="post" action="<?php echo $PHP_SELF?>">
<?php
if (!$attribute_id) {
// editing so select a record
$sql = "SELECT * FROM product_attributes WHERE clid=$clid ORDER by sort ASC";
$result = mysql_query($sql);
$myrow = mysql_fetch_array($result);
$clid = $myrow["clid"];
$attribute_name = $myrow["attribute_name"];
$price = $myrow["price"];
$url = $myrow["url"];
// print the id for editing
?>
<?php
}
?>
<input type="hidden" name="clid" value="<?php echo $clid ?>"><br>
Attribute:<input type="Text" name="attribute_name"><br>
Price:<input type="Text" name="price"><br>
URL:<input type="Text" name="url"><br>
Order:<input type="Text" name="sort"><br>
<input type="Submit" name="submit" value="Add new row">
</form>
<?php
}
?>
</body>
</html>
flying_circus
Forum Regular
Posts: 732 Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR
Post
by flying_circus » Wed Mar 26, 2008 1:41 pm
kind of hard to tell since we dont know what the rest of your code is, but where are you getting your variables like $submit, $attribute_id, and $update?
If you are trying to pull a variable out of the querystring, you need to access it via $_GET['update'].
kryles
Forum Contributor
Posts: 114 Joined: Fri Feb 01, 2008 7:52 am
Post
by kryles » Wed Mar 26, 2008 2:24 pm
I can't recall if this makes a difference when doing it through PHP, but if any of your fields are numerical then don't surround it with single quotes in the query.
dannyd
Forum Commoner
Posts: 56 Joined: Wed Jan 23, 2008 11:31 am
Post
by dannyd » Wed Mar 26, 2008 3:54 pm
I noticed in my code it just doesnt execute the if ($update) at all. I tried just putting an echo statement inside and it didnt work.. i also tried if ($attribute_id) or if ($attribute_id != '') and got nothing.
Is my logic wrong ?
kryles
Forum Contributor
Posts: 114 Joined: Fri Feb 01, 2008 7:52 am
Post
by kryles » Wed Mar 26, 2008 4:10 pm
what is attribute_id? Try
Code: Select all
if($attribute_id == "") { blah blah good stuff blah }