Forum Moderators: coopster

Message Too Old, No Replies

can't get UPDATE to update...only insert

update insert

         

dhaworth

9:39 pm on Dec 9, 2004 (gmt 0)

10+ Year Member



Hey people. I've been working with php/mysql for a while now, but keep taking breaks so I am still definitely aon the beginner side of things. I am creating a simple updatable database and have everyting running nice and smooth excpt that i can't seem to make the info "update", it adds a new entry instead. Here is the link:

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]

jatar_k

9:45 pm on Dec 9, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld dhaworth,

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.

dhaworth

9:57 pm on Dec 9, 2004 (gmt 0)

10+ Year Member



Thanks! I did try soemthing similar to this, I found that the condition:

if ($update_ID)

is not being met...but if I test whether $update_ID is set using:

echo $update_ID;

it returns a value...I am stumped! I can think of other ways to make it work, but they're messier with more code.

jatar_k

9:58 pm on Dec 9, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I was watching your page ;)

the thing is your test
if ($update_ID)

is invalid. I think what you meant to do is this

if (isset($update_ID))

which will check if there is a value attributed to $update_ID

dhaworth

10:04 pm on Dec 9, 2004 (gmt 0)

10+ Year Member



>the thing is your test
>if ($update_ID)
>is invalid.

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

jatar_k

10:07 pm on Dec 9, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



the if is definitely wrong, that doesn't mean your query isn't also wrong but let's get the if working first.

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

dhaworth

10:12 pm on Dec 9, 2004 (gmt 0)

10+ Year Member



Cool, I was working from this example, he skips isset, but everyone else says use it:

[webmonkey.wired.com...]

I added isset, page works the same, but whaddaya think about the UPDATE syntax?

jatar_k

10:15 pm on Dec 9, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



that tutorial is ok, just not that great, I've seen it before

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

dhaworth

10:23 pm on Dec 9, 2004 (gmt 0)

10+ Year Member



Naw, it's not seeing it. Added some other testers, too. Can I nest an if inside an if, or does it need to be andif?

dhaworth

10:30 pm on Dec 9, 2004 (gmt 0)

10+ Year Member



I tested just setting

$sql = "UPDATE merch SET name=$name, merchtype=$merchtype WHERE ID=$update_ID";

and nothing happens when I submit?

jatar_k

10:33 pm on Dec 9, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



hmmm

single quotes missing

try this one

$sql = "UPDATE merch SET name='$name', merchtype='$merchtype' WHERE ID=$update_ID";

dhaworth

10:44 pm on Dec 9, 2004 (gmt 0)

10+ Year Member



Yeah I tested whether these had any effect (b/c of tutorial;), they don;'t seem to. Anywasy, tried it with em and it still didn't work. I think teh problem lies in $update_ID not being passed when user clicks update submission button, b/c of

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...

olwen

10:44 pm on Dec 9, 2004 (gmt 0)

10+ Year Member



I came across this recently. I had neglected to define the primary key on the table.

Added: Oops I was using replace not update

[edited by: olwen at 10:47 pm (utc) on Dec. 9, 2004]

jatar_k

10:45 pm on Dec 9, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



alright

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;?>">

dhaworth

10:50 pm on Dec 9, 2004 (gmt 0)

10+ Year Member



I think have it set...this is what my phpMyAdmin says about my indexes:

Keyname PRIMARY
Type PRIMARY
Cardinality 7
Field ID

Keyname ID
Type UNIQUE
Cardinality 7 
Field ID

Keyname ID_2
Type INDEX
Cardilnaity None
Field ID

I don't fully understand what all this stuff is...

dhaworth

11:12 pm on Dec 9, 2004 (gmt 0)

10+ Year Member



Made the change, can't make it work. If I don't pass $ID in the hidden field, none of the other inputs register as being changed. I tried thsi

$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!

dhaworth

11:14 pm on Dec 9, 2004 (gmt 0)

10+ Year Member



whoa...scrathc that...added back single quotes and it seems to be working...let me put add and delete back into effect...

jatar_k

11:18 pm on Dec 9, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



crossing my fingers ;)

dhaworth

11:31 pm on Dec 9, 2004 (gmt 0)

10+ Year Member



Bleep...well, not quite right. I am so confused now. Can you explain what you mean by controlling teh hidden field with the if?

dhaworth

11:54 pm on Dec 9, 2004 (gmt 0)

10+ Year Member



oh yeah, and i was just curious about the earlier isset comment, I trust you, you knwo alot more than I, but check this out:

[edited by: jatar_k at 5:13 pm (utc) on Dec. 10, 2004]
[edit reason] removed personal url [/edit]

jatar_k

9:10 pm on Dec 10, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I didn't forget, I promise

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

dhaworth

6:46 pm on Dec 13, 2004 (gmt 0)

10+ Year Member



Fist of all, thnaks so much for all your help. I really appreciate it. The code you sent works fine, but it's a little slow and clunky...and I'd like to keep the code on one page, if possible. I will pick some stuff form your pages and see if I can't make it work how I want it to, and get back to ya. Thanks again

jatar_k

6:57 pm on Dec 13, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>> I will pick some stuff form your pages and see if I can't make it work how I want it to

that was the plan ;)