Author Topic: [SOLVED] I have a problem, could use some advice.  (Read 221 times)

0 Members and 1 Guest are viewing this topic.

Offline vetmanTopic starter

  • Enthusiast
  • Posts: 68
    • View Profile
[SOLVED] I have a problem, could use some advice.
« on: October 03, 2008, 08:22:07 AM »
I have ascript to update  my database, first it reads the database, then puts it in a table with an update button. When you click the item you want to update it is supposed to allow you to change anything in the table line. When you are done, it's supposed to  update the database. It says it does but doesn't do anything.
Here is the code.

Code: [Select]
<?php
include 'connect.php';


// Connect to server and select database.
mysql_connect($dbhost$dbuser$dbpass)or die("cannot connect"); 
mysql_select_db("rwts_webmaster")or die("cannot select DB");

// get value of id that sent from address bar
$id=$_GET['id'];


// Retrieve data from database 
$sql="SELECT * FROM example WHERE id='$id'";
$result mysql_query($sql);

$rows mysql_fetch_array($result);
?>

<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<form name="form1" method="post" action="update_ac.php">
<td>
<table width="100%" border="0" cellspacing="1" cellpadding="0">
<tr>
<td>&nbsp;</td>
<td colspan="3"><strong>Update data in mysql</strong> </td>
</tr>
<tr>
<td align="center">&nbsp;</td>
<td align="center">&nbsp;</td>
<td align="center">&nbsp;</td>
<td align="center">&nbsp;</td>
</tr>
<tr>
<td align="center">&nbsp;</td>
<td align="center"><strong>Name</strong></td>
<td align="center"><strong>Lastname</strong></td>
<td align="center"><strong>Email</strong></td>
</tr>
<tr>
<td>&nbsp;</td>
<td align="center"><input name="name" type="text" id="name" value="<? echo $rows['name']; ?>"></td>
<td align="center"><input name="lastname" type="text" id="lastname" value="<? echo $rows['lastname']; ?>" size="15"></td>
<td><input name="email" type="text" id="email" value="<? echo $rows['email']; ?>" size="15"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input name="id" type="hidden" id="id" value="<? echo $rows['id']; ?>"></td>
<td align="center"><input type="submit" name="Submit" value="Submit"></td>
<td>&nbsp;</td>
</tr>
</table>
</td>
</form>
</tr>
</table>

<?php

// close connection 
mysql_close();

?>


Second script to update the database.

Code: [Select]
<?php
include 'connect.php';


// Connect to server and select database.
mysql_connect($dbhost$dbuser$dbpass)or die("cannot connect");
mysql_select_db("rwts_webmaster")or die("cannot select DB");
echo 
"Connected to Database <br>";
// update data in mysql database
$sql "UPDATE example SET name='$name', lastname='$lastname', email='$email' WHERE id ='$id'";
$result=mysql_query($sql);

// if successfully updated.
if($result){
echo 
"Successful";
echo 
"<BR>";
echo 
"<a href='list_records.php'>View result</a>";
}

else {
echo 
"ERROR";
}

?>


Thanks in advance !

Offline ranjuvs

  • Enthusiast
  • Posts: 135
  • Gender: Male
    • View Profile
Re: I have a problem, could use some advice.
« Reply #1 on: October 03, 2008, 08:29:26 AM »
In the update page you need to include the code below

Code: [Select]
<?php
$getId 
$_POST['id'];
$getName $_POST['name'];
$getLastname $_POST['lastname'];

$sql "UPDATE example SET name='$getName', lastname='$getLastname', email='$email' WHERE id ='$getId'";
?>