Forum Moderators: coopster
Here is the relevant php (I left out the connect script;) :
----
<html>
<head>
<title>merch for mofos</title>
<link href="/styles/baddudes-styles.css" rel="stylesheet" type="text/css">
</head>
<body>
<?
// Connect to the database server
MYSTUFF
?>
<div class="text">
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
Type your merch order here, mofo <b><?php echo $_SESSION['username'];?></b>:<br><br>
<?
if ($update_ID) {
// updating so select a record and set button value
$sql = "SELECT * FROM merch WHERE ID=$update_ID";
$result = mysql_query($sql);
$myrow = mysql_fetch_array($result);
$ID = $myrow["ID"];
$name = $myrow["name"];
$merchtype = $myrow["merchtype"];
$button = "update";
}
else {
$button = "add";
}
// add/update/delete data
if ($submit) {
if ($update_ID) {
$sql = "UPDATE merch SET name=$name, merchtype=$merchtype WHERE ID=$update_ID";
} else {
$sql = "INSERT INTO merch (name, merchtype) VALUES ('$name', '$merchtype')";
}
} elseif ($delete_ID) {
$sql = "DELETE FROM merch WHERE ID=$delete_ID";
}
$result = mysql_query($sql);
?>
<input type=hidden name="id" value="<? echo $id?>">
<input type="text" name="name" value="<? echo $name?>"> name<br>
<input type="text" name="merchtype" value="<? echo $merchtype?>"> merchtype<br>
<input type="submit" name="submit" value="<? echo $button?>">
</form>
<?
// Print table of all entries with delete/update buttons
$result = mysql_query("SELECT ID, name, merchtype FROM merch ORDER BY ID DESC",$db);
echo('
<table border="1" cellpadding="2" cellspacing="0" class="text">
<tr class="greenbold"><td>ID</td><td>name</td><td>email</td><td>mail</td><td>merchtype</td><td>date ordered</td><td>date filled</td></tr>
');
while ($row = mysql_fetch_array($result) ) {
$ID = $row['ID'];
$name = $row['name'];
$merchtype = $row['merchtype'];
echo('
<tr><td>' . $ID . '</td><td>' . $name . '</td><td><a href="mailto:' . $email . '">' . $email . '</a></td><td>' . $mail . '</td><td>' . $merchtype . '</td><td>' . $dateordered . '</td><td>' . $datefilled . '</td><td><a href="' . $_SERVER['PHP_SELF'] . '?delete_ID=' . $ID . '">' . 'delete</a></td><td><a href="' . $_SERVER['PHP_SELF'] . '?update_ID=' . $ID . '">' . 'update</a></td></tr>
');
}
echo('</table>');
?>
</div>
</body>
</html>
----
If anyone has time to take a look at this, I'd would really appreciate it. Thanks!
[edited by: jatar_k at 9:40 pm (utc) on Dec. 9, 2004]
[edit reason] no personal urls thanks [/edit]
first things first let's see what it is doing, let's add a few echo's
if ($submit) {
if ($update_ID) {
$sql = "UPDATE merch SET name=$name, merchtype=$merchtype WHERE ID=$update_ID";
echo '<p>look, I am in the right spot';
} else {
$sql = "INSERT INTO merch (name, merchtype) VALUES ('$name', '$merchtype')";
}
} elseif ($delete_ID) {
$sql = "DELETE FROM merch WHERE ID=$delete_ID";
}
echo '<p>',$sql;
die;
$result = mysql_query($sql);
tht should help to see if it is hitting the right if statement and show what the query it is grabbing is then kill the script.
Really? But it seems to work with
if($submit)
I think there is something wrong with this
$sql = "UPDATE merch SET name=$name, merchtype=$merchtype WHERE ID=$update_ID";
because when I test by executing this it doesn't update the info...am I missing something? Thansk again
example
[example.com...]
now $update_ID == 60
so given the if ($update_ID)
if (60) will be false so it will drop to the else statement
the if statement is true or false only so it only responds to values of 1 or 0. Any other number passed will cause it to be false
[webmonkey.wired.com...]
I added isset, page works the same, but whaddaya think about the UPDATE syntax?
the update [dev.mysql.com] looks fine in concept but if the vars aren't being properly populated then that could be your error.
did you confirm that the query is now the update instead of the insert? do that first
echo $sql;
test results. It gets passed when user clicks "update in table, but then not when user clicks update submission button. Thanks so much...
I was playing with your form at the same time as you were ;)
I know what it is
when someone clicks update you pass the $update_ID or delete or what have you, then you edit the textboxes and hit submit but there is no update_ID anymore as it was not posted with the form.
<added>all posting at the same time ;)
add the id as a hidden field in the form, controlled by the if statements
<input type="hidden" name="update_ID" value="<? echo update_ID;?>">
$sql = "UPDATE merch SET name=$name, merchtype=$merchtype WHERE ID=$ID";
because $ID seems to be getting passed too, so why not take a variable out of teh equation, and then everyting appears tp update EXCEPT the actual database!
ok, I ran a bunch of testing this morning since I couldn't actually figure out what your problem was. I built and fixed the script on my servers.
The main thing I did was add an extra control structure and put the actual update/insert/delete code into a seperate file.
There is a ton of testing code in this but it works. I had to switch everything to use $_GET and $_POST because I do not have register_globals on. I switched <? echo to the short form of <?= so you may need to change that.
I called the first file something different since I couldnt remember what you called it
testinsert.php
<?
// put your connect and db select code here
?>
<html>
<head><title>testing</title></head>
<body>
<p><form action="addupmerch.php" method="post">
<?
if (isset($_GET['ac']) && isset($_GET['id'])) {
$sql = "SELECT * FROM merch WHERE ID=" . $_GET['id'];
$result = mysql_query($sql);
$myrow = mysql_fetch_array($result) or die('<p>line 20: ' . mysql_error());
$ID = $myrow["ID"];
$name = $myrow["name"];
$merchtype = $myrow["merchtype"];
}
switch ($_GET['ac']) {
case 'update':
$button = 'update';
$ac = $_GET['ac'];
$id = $_GET['id'];
$mess = "Update the merch ";
break;
case 'delete':
$button = 'delete';
$ac = $_GET['ac'];
$id = $_GET['id'];
$mess = "";
break;
default:
$button = 'add';
$ac = '';
$id = '';
$mess = "Type your merch order here";
break;
}
?>
<p><?= $mess?>:
<input type="hidden" name="ac" value="<?= $ac?>">
<input type="hidden" name="id" value="<?= $id?>">
<p><input type="text" name="name" value="<?= $name?>"> name
<br><input type="text" name="merchtype" value="<?= $merchtype?>"> merchtype
<p><input type="submit" name="submit" value="<?= $button?>">
</form>
<?
$result = mysql_query("SELECT ID, name, merchtype FROM merch ORDER BY ID DESC") or die('<p>line 53: ' . mysql_error());
echo '<p><table border="1" cellpadding="2" cellspacing="0"><tr><td>ID</td><td>name</td><td>email</td><td>mail</td><td>merchtype</td><td>date ordered</td><td>date filled</td></tr>' . "\n";
while ($row = mysql_fetch_array($result)) {
$ID = $row['ID'];
$name = $row['name'];
$merchtype = $row['merchtype'];
echo '<tr><td>' . $ID . '</td><td>' . $name . '</td><td><a href="mailto:' . $email . '">' . $email . '</a></td><td>' . $mail . '</td><td>' . $merchtype . '</td><td>' . $dateordered . '</td><td>' . $datefilled . '</td><td><a href="' . $_SERVER['PHP_SELF'] . '?id=' . $ID . '&ac=delete">' . 'delete</a></td><td><a href="' . $_SERVER['PHP_SELF'] . '?id=' . $ID . '&ac=update">' . 'update</a></td></tr>' . "\n";
}
echo '</table>';
?>
</div>
</body>
</html>
addupmerch.php
<?
// put your connect and db select code here
// add/update/delete data
$sql = "";
switch ($_POST['ac']) {
case 'update':
$sql = "UPDATE merch SET name='" . $_POST['name'] . "', merchtype='" . $_POST['merchtype'] . "' WHERE ID=" . $_POST['id'];
break;
case 'delete':
$sql = "DELETE FROM merch WHERE ID=" . $_POST['id'];
break;
default:
$sql = "INSERT INTO merch (name, merchtype) VALUES ('" . $_POST['name'] . "', '" . $_POST['merchtype'] . "')";
break;
}
$result = mysql_query($sql) or die('<p>line 41: ' . mysql_error());
header("Location: testinsert.php");
?>
if you have questions just ask away