![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
|
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 Last edited by ASHORY; Apr 2nd, 2006 at 6:54 PM. Reason: Wrong Title |
|
|
|
|
|
#2 |
|
Professional Programmer
|
we can only help if there's code samples or something
|
|
|
|
|
|
#3 |
|
Programmer
|
<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 |
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#5 |
|
Programmer
|
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 . |
|
|
|
|
|
#6 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#7 |
|
Programmer
|
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 . |
|
|
|
|
|
#8 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 884
Rep Power: 4
![]() |
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"); |
|
|
|
|
|
#9 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#10 |
|
Programmer
|
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 . |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|