Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 2nd, 2006, 6:53 PM   #1
ASHORY
Programmer
 
ASHORY's Avatar
 
Join Date: Dec 2004
Location: Egypt
Posts: 30
Rep Power: 0 ASHORY is on a distinguished road
Send a message via ICQ to ASHORY Send a message via MSN to ASHORY Send a message via Yahoo to ASHORY
Question Data Table !?

Hey ..
iam new in php but not too much i know a little in php and mysql

but i always face problems . and one of my problems that i want to make a table qoute from a database having a "name" and "phone number" where i can edit the phone number and click save to save it in the database but it is not working with me in my example ,,

so can any body help ..

thanx
__________________
"^Ahmed.SHOukRY^"
http://www.ashory.741.com

Last edited by ASHORY; Apr 2nd, 2006 at 6:54 PM. Reason: Wrong Title
ASHORY is offline   Reply With Quote
Old Apr 3rd, 2006, 12:42 AM   #2
Lich
Professional Programmer
 
Lich's Avatar
 
Join Date: May 2005
Location: Detroit
Posts: 254
Rep Power: 4 Lich is on a distinguished road
Send a message via AIM to Lich Send a message via MSN to Lich
we can only help if there's code samples or something
__________________
--John Cruz
Web Developer
www.cruzweb.net
Lich is offline   Reply With Quote
Old Apr 4th, 2006, 3:45 PM   #3
ASHORY
Programmer
 
ASHORY's Avatar
 
Join Date: Dec 2004
Location: Egypt
Posts: 30
Rep Power: 0 ASHORY is on a distinguished road
Send a message via ICQ to ASHORY Send a message via MSN to ASHORY Send a message via Yahoo to ASHORY
<html>
<head><title></title>
<body>
<?php
if (!isset($_POST['sav_data'])) {
$SQL_Host="localhost";
$SQL_Username="username";
$SQL_Database="database";
$SQL_Password="password";
//Connecting to SQL Server & Opening Database//
$SQL_Connect=mysql_connect($SQL_Host, $SQL_Username,$SQL_Password) or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ($SQL_Database);
$Open_Database=mysql_select_db ($SQL_Database,$SQL_Connect) or die('Cant Choose Database Becouse' . mysql_error());
//Table 1 the whole page//
echo '<table border="0" width="100%" cellspacing="0" cellpadding="0">';
    echo '<tr>';
echo "Codes" . "<br />";
    echo '</tr>';
    echo '<tr>';
//Bringing Data From Database//
$Brg_Data="SELECT * FROM user";
$Brg_Query=mysql_query($Brg_Data);
$Brg_Rows=mysql_num_rows($Brg_Query);
//Table 2 //
        echo '<table border="0" width="100%" cellspacing="0" cellpadding="0">';
           echo '<tr>';
            echo '<td>';
//Table 3 the data table//
            echo '<table border="2" width="100%" cellspacing="0" cellpadding="0" bordercolor="#000000">';
                echo '<tr>';
                 echo '<td>';
                        echo "name";
                 echo '</td>';
                 echo '<td>';
                        echo "phone"  ;
                  echo '</td>';
                 echo '</tr>';

$i=0;
?>
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
<?php
While ($i < $Brg_Rows){
          echo '<tr>';
                echo '<td>';
                      $Import_name=mysql_result($Brg_Query,$i,"name");
                       echo $Import_name ;
                   echo '</td>';
                 echo '<td>';
                     $Import_phone=mysql_result($Brg_Query,$i,"phone");
?>
                              <input type=text name="phone<?php echo $i; ?>"  value="<?php echo $Import_Code;?>">
<?php
               echo '</td>';
          echo '</tr>';
$i++;
}
          echo '</table>';
//End of Table 3//
            echo'</td>';
            echo '<td>';
?>
           <input type="submit" value="Save" name="sav_data">
           </form>
<?php
        echo '</td>';
        echo '</tr>';
      echo '</table>';
//End of table 2//
    echo '</tr>';
    echo '<tr>';
    echo '</tr>';
}
else {
$i2=0;
while ( $i2 < $Brg_Rows ){
$Save_Code=$_POST['phone'.$i2] ;
$DB_Code=mysql_result($Brg_Query,$i2,"phone");
if ($Save_Code=$DB_Code){
$i2++;
echo "No Changes";
}
else {
echo '<h1 align=center>'."Saved Codes".'</h1>'.'<br>';
echo '<table border="2" width="100%" cellspacing="0" cellpadding="0" bordercolor="#000000">';
     echo '<tr>';
        echo '<td>';
             echo "name";
        echo '</td>';
        echo '<td>';
             echo "phone"  ;
        echo '</td>';
     echo '</tr>';
            $Up_Code="UPDATE Codes SET phone='".$Save_Code."' WHERE ID='".$i2."'";
            $Up_Query=mysql_query($Up_Code);
            $DB_phone2=mysql_result($Con_Num,$i,"phone");
    echo '<tr>';
         echo '<td>';
            echo $Import_name;
         echo '</td>';
         echo '<td>';
            echo $DB_phone2;
         echo '</td>';
    echo '</tr>';
echo '</table>';
$i2++;
}
}
}
?>
</body>
</html>

Thats my code guys but my problem here is how to save
__________________
"^Ahmed.SHOukRY^"
http://www.ashory.741.com
ASHORY is offline   Reply With Quote
Old Apr 4th, 2006, 5:39 PM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
That's so trashily formatted I can't read it. Essentially, you read the post variables at the top of the program (assuming you are using the same program as to display and process the post). If they aren't there (first access), nothing happens. Then you query the DB and display the results, often right in the form. The user dinks around and then submits the form. You're back at the beginning, but this time there are posted variables to save in the DB. Once you have the cycle working, you can introduce other variations, such a displaying outside the form, using hidden fields or drop-down menus to convey group or sort orders, or whatever.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Apr 4th, 2006, 6:15 PM   #5
ASHORY
Programmer
 
ASHORY's Avatar
 
Join Date: Dec 2004
Location: Egypt
Posts: 30
Rep Power: 0 ASHORY is on a distinguished road
Send a message via ICQ to ASHORY Send a message via MSN to ASHORY Send a message via Yahoo to ASHORY
this script is full of errors , i know . but i only showed it i need a more accurate example as a phone book with an edit and save feature .

can any one help me .
__________________
"^Ahmed.SHOukRY^"
http://www.ashory.741.com
ASHORY is offline   Reply With Quote
Old Apr 4th, 2006, 6:45 PM   #6
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
I gave you the scenario. Try something simple and post back. Put a name and address in the DB. Query the DB. Put the name and address in the form (text boxes). Exhort the user (presumably you) to change a value. Submit the page. Update the DB. Refresh the form. Rinse and repeat. Post with the problems. Be explicit. Show readably formatted code.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Apr 4th, 2006, 6:57 PM   #7
ASHORY
Programmer
 
ASHORY's Avatar
 
Join Date: Dec 2004
Location: Egypt
Posts: 30
Rep Power: 0 ASHORY is on a distinguished road
Send a message via ICQ to ASHORY Send a message via MSN to ASHORY Send a message via Yahoo to ASHORY
ok i did the example but when i click save , it gives me "No Data Saved" so i tried to echo the field in the database and the form field beside it i found that the two are the same even though i changed the form field value or not knowing that i made the field value equals to the database filed value so i can change it and click save so it will be updated in the database, i thought that the problem is the script take the default value of the field so i changed the value of the filed -<input type=text value=123456789> - as an example and changed it to "111111111" i found that it echo's the the database field value and the form field value the same "DB>5555&FV>5555" and its default value is not 5555 or the changed value is 5555 so whats that problem here .
i checked the script well i didnt find any errors .
__________________
"^Ahmed.SHOukRY^"
http://www.ashory.741.com
ASHORY is offline   Reply With Quote
Old Apr 4th, 2006, 8:32 PM   #8
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 884
Rep Power: 4 The Dark is on a distinguished road
In the script that you posted, the "else" part (i.e. when there is posted data) uses variables that haven't been set (they are only set in the non-posted data part).
Each time someone accesses your page, your script starts from the beginning, so this code:
$i2=0;
while ( $i2 < $Brg_Rows ){
$Save_Code=$_POST['phone'.$i2] ;
$DB_Code=mysql_result($Brg_Query,$i2,"phone");
Is using $Brg_Rows and $Brg_Query which haven't been set. Also mysql_result retrieves the value from a result set, it does not do any updates. You need to perform some INSERT, UPDATE or REPLACE sql statements.
The Dark is offline   Reply With Quote
Old Apr 4th, 2006, 8:57 PM   #9
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Here is some example code, beginning simply, but with PHP, style, and HTML stuff. You'll have to arrange for your own DB and connection, of course. You can see this work here.
[php]
<?php
/*
The database manipulation area. You'll have to arrange your own DB
and connection functions
*/
require_once ("connect.php");
demoConnect ();
$advisory = "Advisory goes here";
if ($_POST ['Name'])
{
/*
Path of execution if information has been posted. More complex
session control may be required under some circumstances.
Note that there is nothing to detect, restrict, or prevent
duplicate information. That's as it should be. In Hogwallow,
there might be four brothers with the same name. The PersonID
field ensures that each is unique. When I want to guard against
this, I make the totality of information more complex (even an
address and email address are not enough) and make an extraction
code which I put in the DB.
*/
$insert = "INSERT INTO AddressBook
SET PersonID=NULL,
Name='".ucwords (strtolower ($_POST ['Name']))."',
City='".ucwords (strtolower ($_POST ['City']))."'";
$iResult = mysql_query ($insert) or die (mysql_error () . " insertion failure<br/>");
$PersonID = mysql_insert_id ();
$advisory = "You inserted {$_POST ['Name']} with ID $PersonID";
}
// Query the DB
$query = "SELECT * FROM AddressBook ORDER BY PersonID";
$addresses = mysql_query ($query) or die (mysql_error () . " query failure<br/>");

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Address Book</title>
<!-- Control things with CSS. Obvioulsy, widely used styles on a multi-page
site should be aggregated in a style sheet and pulled in -->
<style type="text/css">
body
{
margin: 5em;
}
td
{
border: 1px solid #000000;
}
th
{
border: 1px solid #000000;
width: 15em;
font-weight: bold;
}
.shorty
{
width: 5em;
}
.entry
{
font-weight: bold;
}
.pid
{
text-align: center;
font-weight: bold;
}
.theForm
{
padding: 10px;
border: 3px outset #000000;
width: 60%;
margin: auto;
}
.advisory
{
padding: 20px;
width: 50%;
margin: auto;
text-align: center;
}
</style>
</head>

<body>
<!-- Simple HTML. Presentation and content are mixed here. Ya gotta
start somewhere -->
<table align="center" cellspacing="0" cellpadding="0" class="book">
<tr>
<th class="shorty">ID</th><th>Name</th><th>City</th>
</tr>
<?php
while ($address = mysql_fetch_array ($addresses))
{
echo '<tr>';
echo '<td class="pid">'.$address ["PersonID"].'</td>';
echo '<td class="entry">'.$address ["Name"].'</td>';
echo '<td class="entry">'.$address ["City"].'</td>';
echo '</tr>';
}
?>
</table>
<p align="center"><strong>Add Entries Here</strong></p>
<div align="center" class="theForm" id="theForm">
<form name="addEntry" id="addEntry" method="post"
action="http://www.daweidesigns.com/AddressBook.php">
Name: <input type="text" size="20" name="Name" id="Name"/>
City: <input type="text" size="20" name="City" id="City"/>
<input type="submit" value="Add" name="Submit" />
</form>
</div>
<div class="advisory" id="advisory">
<?php echo $advisory;?>
</div>
</body>
</html>
[/php]
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Apr 5th, 2006, 7:11 AM   #10
ASHORY
Programmer
 
ASHORY's Avatar
 
Join Date: Dec 2004
Location: Egypt
Posts: 30
Rep Power: 0 ASHORY is on a distinguished road
Send a message via ICQ to ASHORY Send a message via MSN to ASHORY Send a message via Yahoo to ASHORY
thanks very much dawei , but what about if i needed to edit the city as an example
but like that :
while ($address = mysql_fetch_array ($addresses)) 
    { 
        echo '<tr>'; 
        echo '<td class="pid">'.$address ["PersonID"].'</td>'; 
        echo '<td class="entry">'.$address ["Name"].'</td>'; 
        echo '<td class="entry">'.'<input type=text value='".$address ["City"]."'>'.'</td>'; 
        echo '</tr>'; 
    }

so the city will be written in the text value so i can edit it , then i click save to update the only changed field .
__________________
"^Ahmed.SHOukRY^"
http://www.ashory.741.com
ASHORY is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:48 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC