PHP MYSQL Update & Delete problems

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

Post Reply
bla5e
Forum Contributor
Posts: 234
Joined: Tue May 25, 2004 4:28 pm

PHP MYSQL Update & Delete problems

Post by bla5e »

General Page Error:
Line 30:

Code: Select all

case 'add':
Error:
Notice: Undefined variable: id in index2.php on line 30
DELETE FORUM

Code: Select all

 
if (!isset($_POST["submit"])) {
                        $query = "SELECT * FROM thecars ORDER BY id";
 
                        $result = mysql_query($query) or die(mysql_error());
 
                        echo ("<div id=\"DivEdit\">");
                        echo ("<form action=\"index2.php?id=delete \" method=\"post\">");
                        echo ("<table align=\"center\">");
 
                        while($row = mysql_fetch_array($result)){
                            echo ("<tr>");
                            echo ("<td align=\"right\">". $row['year'] . "&nbsp;" . $row['make'] . "&nbsp;" . $row['model'] ."</td>");
                            echo ("<td align=\"right\"> <input type=\"checkbox\" name=\"id\" value=\"". $row['id'] ."\" /> </td>");
                            echo ("</tr>");
                        }
                        echo ("<tr>");
                        echo ("<td colspan=\"2\" align=\"center\"><input type=\"submit\" value=\"Delete\" />");
                        echo ("</table>");
                        echo ("</form>");
                        echo ("</div>");
                    } else if (isset($_POST["submit"])) {
 
                        $query = "SELECT * FROM thecars WHERE id='".$_POST['id']."'";
                        $result = mysql_query($query) or die(mysql_error());
                        $row = mysql_fetch_array($result);
 
                        echo "Deleted a ". $year ."&nbsp;". $make ."&nbsp;". $model ." from the database. ";
                        mysql_query("DELETE FROM thecars WHERE id='".$_POST['id']."'") or die(mysql_error());
                    }
ERRORS:None.
Problems: Goes back to default switch... no record deletion or erros.

Edit Forum

Code: Select all

case 'edit':
                        $query = "SELECT * FROM thecars ORDER BY id";
 
                        $result = mysql_query($query) or die(mysql_error());
 
                        echo ("<div id=\"DivEdit\">");
                        echo ("<table align=\"center\">");
                        while($row = mysql_fetch_array($result)){
                        $id=$row['id'];
                            echo ("<tr>");
                            echo ("<td align=\"right\"><a href='execute/edit.php?cmd=edit&id=$id'>". $row['year'] . "&nbsp;" . $row['make'] . "&nbsp;" . $row['model'] ."</a></td>");
                            echo ("</tr>");
 
                        }
                        echo ("</table>");
                        echo ("</div>");
                break;
Errors:None.
Problems:None.

edit.php

Code: Select all

 
<?php
error_reporting(E_ALL);
mysql_connect("xxxx", "xxx", "xxx")or die(mysql_error());
mysql_select_db("xx")or die(mysql_error());
$id = $_GET["id"];
 
if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit")
{
   if (!isset($_POST["submit"]))
   {        
        $result = mysql_query("SELECT * FROM thecars WHERE id=$id") or die(mysql_error());
        $row = mysql_fetch_array($result) or die(mysql_error());
 
?>
 
<div id="DivEdit">
    <form action="edit.php" method="post">
    <input type="hidden" name="cmd" value="edit">
    <input type=hidden name="id" value="<?php print ((isset($row['id']))? $row['id'] : "") ?>">
 
    <table align="center">
        <tr>
            <td align="right">Year</td>
            <td align="left"><input type="text" name="year" id="year" maxlength="4" value="<?php echo $row['year']; ?>" /></td>
        </tr>
        <tr>
            <td align="right">Make</td>
            <td align="left"><input type="text" name="make" id="make" value="<?php echo $row['make']; ?>"/></td>
        </tr>
        <tr>
            <td align="right">Model</td>
            <td align="left"><input type="text" name="model" id="model" value="<?php echo $row['model']; ?>"/></td>
        </tr>
        <tr>
            <td align="right">Short Description of work</td>
            <td align="left"><textarea cols="25" rows="3" name="shortdesc" id="shortdesc"><?php echo $row['shortdesc']; ?></textarea>
        </tr>
        <tr>
            <td align="right">Note worthy items: </td>
            <td align="left">
                <input type="text" id="note1" name="note1" maxlength="35" value="<?php echo $row['note1']; ?>" /><br />
                <input type="text" id="note2" name="note2" maxlength="35" value="<?php echo $row['note2']; ?>"/></td>
        </tr>
        <tr>
            <td colspan="2" align="center"><input type="submit" name="submit" value="Submit" /></td>
        </tr>
    </table>
    </form>
</div>
 
<?php
   }
 
   else if ($_POST["$submit"])
   {
 
      $year = $_POST["year"];
      $make = $_POST["make"];
      $model = $_POST["model"];
      $shortdesc = $_POST["shortdesc"];
      $note1 = $_POST["note1"];
      $note2 = $_POST["note2"];
 
      $sql = "UPDATE thecars SET year=$year,make=$make,model=$model,shortdesc=$shortdesc,note1=$note1,note2=$note2 WHERE id=$id";
      //replace news with your table name above
      $result = mysql_query($sql) or die(mysql_error()) ;
 
      echo "Car updated";
    }
}
?>
Errors before Submission:None
Problems before submission:None

Errors after submission:
Notice: Undefined index: id in execute\edit.php on line 5

Notice: Undefined index: cmd in execute\edit.php on line 7

Notice: Undefined index: Submit in execute\edit.php on line 54
-Line 5: $id = $_GET["id"];
-Line 7: if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit")
-Line 54: else if ($_POST["$submit"])
Problems after submission: Doesn't update the record


I know this is a lot but I have been headaching over this for about 2hours now and cant figure out whats wrong.. I have error reporting for all and each mysql query/connection with no errors except whats posted above. Thanks for the help
bla5e
Forum Contributor
Posts: 234
Joined: Tue May 25, 2004 4:28 pm

Re: PHP MYSQL Update & Delete problems

Post by bla5e »

i still need help with this! grrrrr :banghead:
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: PHP MYSQL Update & Delete problems

Post by flying_circus »

The notices are telling you exactly what is wrong.
bla5e wrote: Notice: Undefined index: id in execute\edit.php on line 5
Line 5: $id = $_GET["id"];
On line 5, you are referencing $_GET['id'], but $_GET['id'] does not exist. This is the same issue for the rest of the notices you are seeing.


You should be checking for variable existence before referencing them. You can do this very easily by using the isset() function.

Code: Select all

<?php
  # Fetch Vars
    $id = (isset($_GET['id'])) ? $_GET['id'] : '';
 
  # Sanity Check
    if(empty($id))
      die('id is empty.');
?>
The above example checks if $_GET['id'] is set. If it is, then $id = $_GET['id']. If it's not set, then $id = '';
Next, if you require $_GET['id'] to contain a non-empty value for your code to function, you should do a sanity check.


Now that we know $_GET['id'] is set, and isn't empty. We are free to use it within our scripts. Understand that this is VERY basic validation, and you should more in-depth validation on all user input. User input is anything that comes from $_GET, $_POST, $_COOKIE, $_REQUEST, and some $_SERVER vars. For example, if you expect $id to be of type integer, then cast it to an int or verify that it actually is an int. Otherwise, if id = a, how will your script handle it?
bla5e
Forum Contributor
Posts: 234
Joined: Tue May 25, 2004 4:28 pm

Re: PHP MYSQL Update & Delete problems

Post by bla5e »

Well why isnt id set? It should grab the ID from the previous page

note: it is grabbing the ID, but not updating the record
bla5e
Forum Contributor
Posts: 234
Joined: Tue May 25, 2004 4:28 pm

Re: PHP MYSQL Update & Delete problems

Post by bla5e »

any ideas??
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: PHP MYSQL Update & Delete problems

Post by social_experiment »

Are you getting 'ID' from the hidden field inside your form? If yes, your form's method is set to 'post', that could be the problem.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
bla5e
Forum Contributor
Posts: 234
Joined: Tue May 25, 2004 4:28 pm

Re: PHP MYSQL Update & Delete problems

Post by bla5e »

i have it setup exactly the same way on another site though, and works flawlessly :(
Alkis
Forum Commoner
Posts: 31
Joined: Fri Mar 26, 2010 8:41 am

Re: PHP MYSQL Update & Delete problems

Post by Alkis »

Do you see a: id={somenumberhere} e.g. : id=5 on the url of the page?

For example: http://www.domain.com/page.php?id=5 ??

or the url is just http://www.domain.com/page.php ?
Post Reply