TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   update table with php form (http://www.talkphp.com/absolute-beginners/4815-update-table-php-form.html)

calexandru 08-04-2009 05:18 PM

update table with php form
 
Hello all.

I try to create a graphical interface, with php and mysql (db). First time the database was in excel.I import from excel in mysql, I created a page to read the table, and now I try to create a page for modify table, but I do not know very well how can i create. Anybody can help me?

this is view.php
PHP Code:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>C.A.S. Angajator 9.5% | Vizualizare</title>

<left>
<?php include("meniu.html"); ?>
</left>



<center>
<?PHP
//Legatura fisier baza de date
include 'conectare.php';

$con mysql_connect("localhost"$user_name$password);

mysql_select_db($database$con);

$result mysql_query("SELECT * FROM casangaj95");
    echo 
"<table border='1'>
        <tr>
            <th>Coloana A</th>
            <th>Coloana B</th>
            <th>Coloana C</th>
        </tr>"
;
        while (
$row mysql_fetch_array($result)) {
            echo 
"<tr>";
            echo 
"<td>" $row['Field1'] . "</td>";
            echo 
"<td>" $row['Field2'] . "</td>";
            echo 
"<td>" $row['Field3'] . "</td>";
            echo 
"</tr>";
        }
    echo 
"</table>";


    
mysql_close($con);

?> 
</center>

and modify.php

PHP Code:

<?
// Connect database.
$host="localhost"// Host name.
$db_user="root"// MySQL username.
$db_password=""// MySQL password.
$database="excel"// Database name.
mysql_connect($host,$db_user,$db_password);
mysql_select_db($database);

// Select all data records in table "name_list" and put them into $result.
$result=mysql_query("select * from casangaj95 order by Field1");
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form id="form1" name="form1" method="post" action="update.php">
<table border="1" cellpadding="3" cellspacing="0">
<tr>
<td align="center" bgcolor="#FFCC00"><strong>Field1</strong></td>
<td align="center" bgcolor="#FFCC00"><strong>Field2</strong></td>
<td align="center" bgcolor="#FFCC00"><strong>Field3</strong></td>
<td align="center" bgcolor="#FFCC00"><strong>Field4</strong></td>
<td align="center" bgcolor="#FFCC00"><strong>Field5</strong></td>
<td align="center" bgcolor="#FFCC00"><strong>Field6</strong></td>
</tr>
<?
// Fetch record rows in $result by while loop and put them into $row.
while($row=mysql_fetch_assoc($result)){
?>
<tr>
<td bgcolor="#FFFFCC"><? echo $row['Field1']; // Show record's ID ?></td>
<td bgcolor="#FFFFCC"><input name="name_<? echo $row['Field1']; ?>" type="text" id="name_<? echo $row['Field1']; ?>" value="<? echo $row['name']; ?>" /></td>
</tr>
<? // End while loop. ?>
</table>
<input type="submit" name="Submit" value="Update" />
</form>
</body>
</html>

and update.php
PHP Code:

<?
if($_POST['Submit']){ // If receive Submit button variable.

// Connect database.
$host="localhost"// Host name.
$db_user="root"// MySQL username.
$db_password=""// MySQL password.
$database="excel"// Database name.
mysql_connect($host,$db_user,$db_password);
mysql_select_db($database);

// Select all data records in table "name_list" and put them into $result.
$result=mysql_query("select id from name_list order by id asc");

// Fetch record rows in $result by while loop and put them into $row.
while($row=mysql_fetch_assoc($result)){
// Get the posted value "name_ID value" from form.php. This variable change it's value by while loop.
$name=$_POST["name_".$row[id]];

// Update field "name", matching with "id" value by while loop.
mysql_query("update name_list set name='$name' where id='$row[id]'");
}
echo 
"--- Update Complete ---";
}
?>

I'm a very beginner :) The table is casangaj95 and the field that i want to modify are Field1 Field2 Field3 etc..Field 21. The update.php ins't configured for my db

Thanks a lot


All times are GMT. The time now is 02:20 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0