Go4Expert

Go4Expert (http://www.go4expert.com/)
-   PHP (http://www.go4expert.com/forums/php/)
-   -   Undefined variable: update in C:\..\edit.php on line 7 (http://www.go4expert.com/forums/undefined-variable-update-ceditphp-line-t23795/)

abosirage 8Nov2010 15:22

Undefined variable: update in C:\..\edit.php on line 7
 
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

Max2010 8Nov2010 16:51

Re: Undefined variable: update in C:\..\edit.php on line 7
 
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

abosirage 8Nov2010 17:21

Re: Undefined variable: update in C:\..\edit.php on line 7
 
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

Max2010 8Nov2010 17:59

Re: Undefined variable: update in C:\..\edit.php on line 7
 
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?

abosirage 8Nov2010 18:29

Re: Undefined variable: update in C:\..\edit.php on line 7
 
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

Max2010 8Nov2010 18:40

Re: Undefined variable: update in C:\..\edit.php on line 7
 
something like this:

<
PHP 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 (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>


abosirage 9Nov2010 11:24

Re: Undefined variable: update in C:\..\edit.php on line 7
 
dear MAX2010
thank you very much for your time & help its work & the problem solved


All times are GMT +5.5. The time now is 05:44.