Hi,
I was just wondering how the program flow is like for the script below. I've added some comments and was wondering if i got it right.
How I think it works-->
When the form is first called, if user chooses to add new info, then it is $submit (meaning boolean variable $submit for true ). This corresponds with the input button name "submit". if it is updating current info the it is $update (meaning boolean variable $update for true ). This corresponds with the input button name "update ".I was wondering what the 'if statement' for $id is for?How does it go together?
<?php
include("include/header.inc.php"); //include header file
?>
<h1>Add /Edit new Entry<br>
</h1>
<?php
include("include/dbconnect.php"); //include connection to mysql server
/if user chooses the option corresponding to
<input type="Submit" name="submit" value="Enter information">
then it will do the following.
does it mean when option above chosen, $submit becomes true, like
a boolean variable??
the same goes to <input type="Submit" name="update" value="Update information">
/
if($submit)
{
$sql= "INSERT INTO $table (firstname,lastname,address,home,office,mobile)VALUES('$firstname','$lastname','$address','$home','$office','$mobile')";
$result = mysql_query($sql);
echo"<br><br>Information entered into addressbook";
}
else if($update)
{
$sql="UPDATE $table SET firstname ='$firstname', lastname = '$lastname' , address ='$address', home ='$home', office ='$office', mobile = '$mobile' WHERE id ='$id";
$result = mysql_query($sql);
echo"<br><br>Information updated in addressbook";
}
/
Fetch all ids available in database.
/
else if($id)
{
$result = mysql_query("SELECT FROM $table WHERE id ='$id'", $db);
$rowshow = mysql_fetch_array($result);
?>
<form method = "post" action="edit.php">
<table width="380" border="0">
<tr>
<td width="163">
<!--
using an associative array, first field id of result is displayed
-->
<input type="hidden" name="id" value="<?php echo $rowshow["id"]?>">
<strong>First Name</strong>:
</td>
<td width="207"><input name="firstname" type="text" value="<?php echo $rowshow["firstname"]?>"></td>
</tr>
<tr>
<td><strong>Last Name</strong>:</td>
<td><input name="lastname" type="text" value="<?php echo $rowshow["lastname"]?>"></td>
</tr>
<tr>
<td><strong>Address</strong>:</td>
<td><textarea name="address" rows="5" cols="35" value="<?php echo $rowshow["address"]?>"></textarea></td>
</tr>
<tr>
<td colspan="2"><strong>Telephone</strong></td>
</tr>
<tr>
<td>Home:</td>
<td><input name="home" type="text" value="<?php echo $rowshow["home"]?>"></td>
</tr>
<tr>
<td>Office:</td>
<td><input name="office" type="text" value="<?php echo $rowshow["office"]?>"></td>
</tr>
<tr>
<td>Mobile:</td>
<td><input name="mobile" type="text" value="<?php echo $rowshow["mobile"]?>"></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="update" value="Update Info"></td>
</tr>
</table>
</form>
<?
}
else
{
?>
<form method = "post" action="<?php echo $PHP_SELF ?>">
<table width="380" border="0">
<tr>
<td width="163">
<!--
using an associative array, first field id of result is displayed
-->
<input type="hidden" name="id" value="<?php echo $rowshow["id"]?>">
<strong>First Name</strong>:
</td>
<td width="207"><input name="firstname" type="text" size="35"></td>
</tr>
<tr>
<td><strong>Last Name</strong>:</td>
<td><input name="lastname" type="text" size="35"></td>
</tr>
<tr>
<td><strong>Address</strong>:</td>
<td><textarea name="address" rows="5" cols="35"></textarea></td>
</tr>
<tr>
<td colspan="2"><strong>Telephone</strong></td>
</tr>
<tr>
<td>Home:</td>
<td><input name="home" type="text"></td>
</tr>
<tr>
<td>Office:</td>
<td><input name="office" type="text"></td>
</tr>
<tr>
<td>Mobile:</td>
<td><input name="mobile" type="text"></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="update" value="Update Info"></td>
</tr>
</table>
</form>
<?
}
include ("include/footer.inc.php");
?>
p.s. When I ran this script, I had three warnings:
Warning: Undefined variable: submit in c:\program files\apache group\apache\htdocs\phpbaby\blackbook\edit.php on line 17
Warning: Undefined variable: update in c:\program files\apache group\apache\htdocs\phpbaby\blackbook\edit.php on line 23
Warning: Undefined variable: id in c:\program files\apache group\apache\htdocs\phpbaby\blackbook\edit.php on line 29
However I was able to enter data and save the data in the mysql server table.
Many thanks to the person who'll help me out.