Jump to content

[SOLVED] Update or Delete row problem


bschultz

Recommended Posts

I'm having a problem getting the proper syntax to get this working.  I want to display (in an html form) the results currently in the database.  I then want to be able to update the records.  This part of the script is working.  Now, I'm trying to add a "delete row" clause within the update script.

 

Here's what I have so far:

 

<? 
putenv("TZ=US/Central");


$DBhost = "localhost"; 
$DBuser = "username";
$DBpass = "password";
$DBName = "cancellations";

mysql_connect($DBhost,$DBuser,$DBpass) or die("Unable to connect to database"); 

@mysql_select_db("$DBName") or die("Unable to select database $DBName"); 

$date = $_POST[date];
$type = $_POST[type];
$event = $_POST[event];
$action = $_POST[action];
$comments = $_POST[comments];
$row_number = $_POST[row_number];
$cmd = $_POST[cmd];


for ($i=0;$i<count($_POST[event]);$i++){

if('$cmd[$i]'=="delete")
{
    $sqldelete = "DELETE FROM cancellations WHERE row_number='$row_number[$i]'";
    $resultdelete = mysql_query($sqldelete);
    echo $sqldelete;

//    echo "Row deleted!";
}
else 
$usql = "UPDATE cancellations SET 
date='$date[$i]', 
type='$type[$i]', 
event='$event[$i]', 
action='$action[$i]', 
comments='$comments[$i]' 

WHERE row_number='$row_number[$i]'"; 
// above compiles query


$dosql = mysql_query($usql); // executes query
}

if ($dosql){
echo $usql;


} 
else{ 
echo mysql_errno().": ".mysql_error()."<BR>"; 
} 
mysql_close (); 
?>  

 

The delete clause isn't doing anything...just the update clause is working.  Any ideas?  thanks for your help.

 

Brian

Link to comment
Share on other sites

try this and see if there is an error

if('$cmd[$i]'=="delete")
{
    $sqldelete = "DELETE FROM cancellations WHERE row_number='$row_number[$i]'";
    $resultdelete = mysql_query($sqldelete) or die ("Error in Delete Query" . mysql_error());
    echo $sqldelete;

//    echo "Row deleted!";
}

Link to comment
Share on other sites

No error.  This is the result echoed by this line:

 

if ($dosql){
echo $usql;

 

UPDATE cancellations SET date='2007-03-24', type='business', event='bagley library', action='Closed', comments='' WHERE row_number='17'

 

So, it appears that the code isn't doing anything for the delete if statement.

Link to comment
Share on other sites

That's why I said that it doesn't appear to be doing anything on the delete if statement.

 

I'm starting to think I have a logic problem here anyway.  This script will display EVERY record in the database matching today's date.  If the code I posted actually worked, it would stop at the first delete statement. 

 

I want to be able to echo ALL records into the html form, edit ALL records that need to be edited, and delete all the records that need to be deleted ALL AT ONCE. 

 

The way that I wrote this code, that won't happen.  Any ideas on how to lay out this code to accomplish what I want?

Link to comment
Share on other sites

sorry misunderstood, try this

 

$cmd = $_POST[cmd];//because you have this line and its not an array


if( $cmd =="delete") // you just need this line changed to read this without the [$i]
{
    $sqldelete = "DELETE FROM cancellations WHERE row_number='$row_number[$i]'";
    $resultdelete = mysql_query($sqldelete) or die ("Error in Delete Query" . mysql_error());
    echo $sqldelete;

//    echo "Row deleted!";
}

Link to comment
Share on other sites

I tried that, but it still didn't work.

 

By the way, why wouldn't $cmd be an array?  If there are 20 records matching today's date, I'll need $cmd[2] to delete the second row...otherwise, it deletes everything, wouldn't it?

 

 

Link to comment
Share on other sites

ok i made an assumption that it wasnt an array - my fault...

 

echo $cmd[$i]; // just to see what it says

if($cmd[$i] =="delete") //removed the single ticks from around the variable...is it "delete" or "Delete" that will be sent??
{
    $sqldelete = "DELETE FROM cancellations WHERE row_number='$row_number[$i]'";
    $resultdelete = mysql_query($sqldelete) or die ("Error in Delete Query" . mysql_error());
    echo $sqldelete;

//    echo "Row deleted!";
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.