Undefined variable: update in C:\..\edit.php on line 7

Discussion in 'PHP' started by abosirage, Nov 8, 2010.

  1. abosirage

    abosirage Member

    Joined:
    Jul 29, 2010
    Messages:
    34
    Likes Received:
    0
    Trophy Points:
    6
    Occupation:
    IT
    Location:
    Tripoli-Libya
    hi everybody
    I have the following broplem I ask you kindly to help solve it . it is about showing
    following line in me page in same time the script working perfect here is my php code :
    Code:
    <HTML>
    <div align="center">
    <table border="0" width="63%" id="table1">
    <tr>
    <td>
    <?php
    if ($update) {
     $db = mysql_connect("localhost", "root","");
     mysql_select_db("learndb",$db);
     $sql = "UPDATE personnel SET firstname='$first', 
     lastname='$last', nick='$nickname', email='$email', salary='$salary' WHERE id=$id";
     $result = mysql_query($sql);
     echo "thanks";
    }
    else if($id)
    {
     $db = mysql_connect("localhost", "root", "");
     mysql_select_db("learndb",$db);
     $result = mysql_query("SELECT * FROM personnel WHERE id=$id",$db);
     $myrow = mysql_fetch_array($result);
    ?>
    <form method="post"action="<?php echo $PHP_SELF?>">
    <input type="hidden"name="id"value="<?php echo $myrow["id"]?>">
    <p dir="rtl"><b><span lang="ar-sa">first </span>:</b><span lang="ar-sa">
    </span><input type="Text"name="first"value="<?php echo $myrow["firstname"]?>"><br>
    <b><span lang="ar-sa">last </span>:</b><span lang="ar-sa"> </span><input type="Text" name="last" value="<?php echo $myrow["lastname"]?>"><br>
    <b><span lang="ar-sa">العنوان </span>:</b><span lang="ar-sa"> </span><input type="Text" name="nickname" value="<?php echo $myrow["nick"]?>"><br>
    <b><span lang="ar-sa">e-mail </span>:</b><span lang="ar-sa"> </span><input type="Text" name="email" value="<?php echo $myrow["email"]?>"><br>
    <b><span lang="ar-sa">salary </span>:</b><span lang="ar-sa"> </span><input type="Text" name="salary" value="<?php echo $myrow["salary"]?>"><br>
    &nbsp;</p>
    <p dir="rtl" align="center">
    <input type="Submit" name="update" value="update"></p>
                </form>
                <p dir="rtl"><span lang="ar-sa">&nbsp;</span><?php
    }
    ?>

    note : I am using wampserver 1.6.1.33 with php 5.3.0
     
    Last edited by a moderator: Nov 8, 2010
  2. Max2010

    Max2010 New Member

    Joined:
    Oct 14, 2010
    Messages:
    17
    Likes Received:
    1
    Trophy Points:
    0
    Home Page:
    http://www.logoarena.com
    you are missing to assign the $_POST variables to the variables you are using to insert in db and to test the operation to make

    $update variable is not defined, you should probably do like this before:

    if ($_POST['update']=='update') {
    $update=true;
    }

    again, you should assign all the $_POST variables to the variables you are using to insert in the db, like this:

    $first=$_POST['first'];
    $last=$_POST['last'];
    $id=$_POST['id'];
    ...
    ...

    and also don't forget to sanitize the inputs before inserting in the db, like this:

    $sql = "UPDATE personnel SET firstname='".mysql_real_escape_string($first)."',
    lastname='".mysql_real_escape_string($last)."', nick='".mysql_real_escape_string($nickname)."', email='".mysql_real_escape_string($email)."', salary='".mysql_real_escape_string($salary)."' WHERE id=".(int)$id.";";

    I don't get how your if else condition could work.
    You always have the $id variable if it comes from $_POST['id']... so the second condition will always run if the first run too.
    if you explain better what you need to do I can help you
     
  3. abosirage

    abosirage Member

    Joined:
    Jul 29, 2010
    Messages:
    34
    Likes Received:
    0
    Trophy Points:
    6
    Occupation:
    IT
    Location:
    Tripoli-Libya
    dear Max2010

    thank you very much for your reply here is my complete code please check it & do your best to help me :
    Code:
    <HTML>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1256">
    <div align="center">
        <table border="0" width="63%" id="table1">
            <tr>
                <td>
                <?php
        if($update) {
     $db = mysql_connect("localhost", "root","");
     mysql_select_db("learndb",$db);
     $sql = "UPDATE personnel SET firstname='$first', 
     lastname='$last', nick='$nickname', email='$email', salary='$salary' WHERE id=$id";
     $result = mysql_query($sql);
     echo "شكراً تم تحديث البيانات";
    }
    else if($id)
    {
     $db = mysql_connect("localhost", "root", "");
     mysql_select_db("learndb",$db);
     $result = mysql_query("SELECT * FROM personnel WHERE id=$id",$db);
     $myrow = mysql_fetch_array($result);
    ?>
    <form method="post"action="<?php echo $PHP_SELF?>">
    <input type="hidden"name="id"value="<?php echo $myrow["id"]?>">
    <p dir="rtl"><b><span lang="ar-sa">الاسم&nbsp; </span>:</b><span lang="ar-sa">
    </span><input type="Text"name="first"value="<?php echo $myrow["firstname"]?>"><br>
    <b><span lang="ar-sa">اللقب </span>:</b><span lang="ar-sa"> </span><input type="Text" name="last" value="<?php echo $myrow["lastname"]?>"><br>
    <b><span lang="ar-sa">العنوان </span>:</b><span lang="ar-sa"> </span><input type="Text" name="nickname" value="<?php echo $myrow["nick"]?>"><br>
    <b><span lang="ar-sa">البريد الالكتروني </span>:</b><span lang="ar-sa"> </span><input type="Text" name="email" value="<?php echo $myrow["email"]?>"><br>
    <b><span lang="ar-sa">المرتب </span>:</b><span lang="ar-sa"> </span><input type="Text" name="salary" value="<?php echo $myrow["salary"]?>"><br>
    &nbsp;</p>
    <p dir="rtl" align="center">
    <input type="Submit" name="update" value="حدث البيانات"></p>
                </form>
                <p dir="rtl"><span lang="ar-sa">&nbsp;</span><?php
    }
    ?>
    
                </td>
                <tr>
                <td>
        <p align="center"><b><font size="4"><a href="serch.php">بحـــث</a>&nbsp;&nbsp;
    <a href="datain1.html">إدخال جديد</a>&nbsp;&nbsp; <span lang="ar-sa">
        <a href="viewdb4.php">عودة للقائمة</a></span></font></b></p>    
                <p>
      
            </tr>
        </table>
    </div>
    </HTML>
    
    with my best regards
     
    Last edited by a moderator: Nov 8, 2010
  4. Max2010

    Max2010 New Member

    Joined:
    Oct 14, 2010
    Messages:
    17
    Likes Received:
    1
    Trophy Points:
    0
    Home Page:
    http://www.logoarena.com
    hm actually I need to know what do you need to do. This seems like an edit page, is it? But I wonder from where comes the id value to get the db data... maybe from another script?
     
  5. abosirage

    abosirage Member

    Joined:
    Jul 29, 2010
    Messages:
    34
    Likes Received:
    0
    Trophy Points:
    6
    Occupation:
    IT
    Location:
    Tripoli-Libya
    dear max2010 thank you for communicate


    its come from page view.php exactly line 18 the code below
    Code:
    <HTML> 
    <div align="center">
        <table border="0" width="63%" id="table1">
            <tr>
                <td>
                <?php 
    $db = mysql_connect("localhost", "root", ""); 
    mysql_select_db("learndb",$db); 
    $result = mysql_query("SELECT * FROM personnel",$db); 
    echo "<TABLE BORDER=2>"; 
    echo"<TR><td align=center width=190><B>الاجـــــــراء</B><td align=center width=190><B>العنــــــــوان</B>
    <td align=center width=220><B>الاسم بالكامـــــل</B></TR>"; 
    while($myrow = mysql_fetch_array($result)) 
    { 
    echo "<TR>
    <TD td align=center width=190><a href=\"view.php?id=".$myrow['id']."\">عرض</a> ";
    echo "<a href=\"delete.php?id=".$myrow['id']."\">حذف</a> ";
    echo "<a href=\"addedit.php?id=".$myrow['id']."\">تعديل</ a>";
    
    
    echo "<td align=center width=190>".$myrow["firstname"]." ".$myrow["lastname"]."
    </a><td align=center width=190>".$myrow["nick"];
    } 
    echo "</TABLE>"; 
    ?> 
    
                
                
                
                </td>
            </tr>
        </table>
    </div>
    <p align="center"><b><font size="4"><a href="serch.php">بحـــث</a>&nbsp;&nbsp;
    <a href="datain1.html">إدخال جديد</a></font></b></p>
    <p>&nbsp;</p>
    
    
    </HTML>
    help me pleeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeease
     
    Last edited by a moderator: Nov 8, 2010
  6. Max2010

    Max2010 New Member

    Joined:
    Oct 14, 2010
    Messages:
    17
    Likes Received:
    1
    Trophy Points:
    0
    Home Page:
    http://www.logoarena.com
    something like this:

    <
    PHP:
    HTML>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1256">
    <div align="center">
    <table border="0" width="63%" id="table1">
    <tr>
    <td>
    <?php

    if (isset($_POST['update'])) {

    $id=$_POST['id'];
    $first=$_POST['first'];
    $last=$_POST['last'];
    $nickname=$_POST['nickname'];
    $email=$_POST['email'];
    $salary=$_POST['salary'];

    $db mysql_connect("localhost""root","");
    mysql_select_db("learndb",$db);
    $sql "UPDATE personnel SET firstname='".mysql_real_escape_string($first)."', lastname='".mysql_real_escape_string($last)."', nick='".mysql_real_escape_string($nickname)."', email='".mysql_real_escape_string($email)."', salary='".mysql_real_escape_string($salary)."' WHERE id=".(int)$id.";";
    $result mysql_query($sql);
    echo 
    "شكراً تم تحديث البيانات";

    }

    else {

    $id=(int)$_GET['id'];

    $db mysql_connect("localhost""root""");
    mysql_select_db("learndb",$db);
    $result mysql_query("SELECT * FROM personnel WHERE id=$id",$db);
    $myrow mysql_fetch_array($result);
    ?>
    <form method="post" action="<?php echo $PHP_SELF?>">
    <input type="hidden" name="id" value="<?php echo $myrow["id"]?>" />
    <p dir="rtl"><b><span lang="ar-sa">الاسم&nbsp; </span>:</b><span lang="ar-sa">
    </span><input type="Text" name="first" value="<?php echo $myrow["firstname"]?>" /><br />
    <b><span lang="ar-sa">اللقب </span>:</b><span lang="ar-sa"> </span>
    <input type="Text" name="last" value="<?php echo $myrow["lastname"]?>" /><br />
    <b><span lang="ar-sa">العنوان </span>:</b><span lang="ar-sa"> </span>
    <input type="Text" name="nickname" value="<?php echo $myrow["nick"]?>" /><br />
    <b><span lang="ar-sa">البريد الالكتروني </span>:</b><span lang="ar-sa"> </span>
    <input type="Text" name="email" value="<?php echo $myrow["email"]?>" /><br />
    <b><span lang="ar-sa">المرتب </span>:</b><span lang="ar-sa"> </span>
    <input type="Text" name="salary" value="<?php echo $myrow["salary"]?>" /><br />
    &nbsp;</p>
    <p dir="rtl" align="center">
    <input type="Submit" name="update" value="حدث البيانات" /></p>
    </form>
    <p dir="rtl"><span lang="ar-sa">&nbsp;</span><?php
    }
    ?>

    </td>
    <tr>
    <td>
    <p align="center"><b><font size="4"><a href="serch.php">بحـــث</a>&nbsp;&nbsp;
    <a href="datain1.html">إدخال جديد</a>&nbsp;&nbsp; <span lang="ar-sa">
    <a href="viewdb4.php">عودة للقائمة</a></span></font></b></p>
    <p>

    </tr>
    </table>
    </div>
    </HTML>
     
    shabbir likes this.
  7. abosirage

    abosirage Member

    Joined:
    Jul 29, 2010
    Messages:
    34
    Likes Received:
    0
    Trophy Points:
    6
    Occupation:
    IT
    Location:
    Tripoli-Libya
    dear MAX2010
    thank you very much for your time & help its work & the problem solved
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice