Jump to content

[SOLVED] I could use some help, my script doesn't work.


vetman

Recommended Posts

I have a script to change an item in my database. When I process the script it give me an error.

First I get all the info from the database, then I pick the data to change, next I change the data, but when I process the update part it doesn't work. Can you see what wrong?

The first script:

 

<?php include('header.php');?>
<?php include('mainnav.php');?>

<?php
include 'config.php';

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

$result = mysql_query("SELECT * FROM $dbname") or die(mysql_error());
// store the record of the "lakestmill" table into $row
$current = '';

// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
$id = $row['id'];
if (!$current) {
echo "<center><div><table border='1' width='300'>";
$current = $id;
} elseif ($current != $id){
echo "</table></div><br><br><div><table border='1' width='300'>";
$current = $id;
}
?>

<tr><th width='80' height='3'> Unit No.</th><td><?= $row['unit']; ?></td></tr>
<tr><th> Company</th><td><?= $row['company']; ?></td></tr>
<tr><th> FirstName</th><td><?= $row['firstname']; ?></td></tr>
<tr><th> LastName</th><td><?= $row['lastname']; ?></td></tr>
<tr><th> Email</th><td><a href="mailto:<?= $row['email']; ?>"><?= $row['email']; ?></a></td></tr>
<tr><th> Address</th><td><?= $row['address']; ?></td></tr>
<tr><th> City</th><td><?= $row['city']; ?></td></tr>
<tr><th> State</th><td><?= $row['state']; ?></td></tr>
<tr><th> Zip Code</th><td><?= $row['zip']; ?></td></tr>
<tr><th> Phone</th><td><?= $row['phone']; ?></td></tr>
<tr><th>Update</th><td><a href="update_ls.php?id=<? echo $row['id'];?>">update</a></td></tr>
<?php
}
echo "</table></div></center>";
?>
<?php
mysql_close();
?>

 

The second part:

 

<?php include('header.php');?>
<?php include('mainnav.php');?>

<?php
include 'config.php';


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

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


// Retrieve data from database
$sql="SELECT * FROM $dbname 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="1" cellspacing="1" cellpadding="0">
<tr>
<td> </td>
<td colspan="3"><strong>Update data in mysql</strong> </td>
</tr>

<tr><th width='80' height='3'> Unit No.</th><td><input name="unit" type="text" id="unit" value="<? echo $rows['unit']; ?>"></td></tr>
<tr><th> Company</th><td><input name="company" type="text" id="company" value="<? echo $rows['company']; ?>"></td></tr>
<tr><th> FirstName</th><td><input name="firstname" type="text" id="firstname" value="<? echo $rows['firstname']; ?>"></td></tr>
<tr><th> LastName</th><td><input name="lastname" type="text" id="lastname" value="<? echo $rows['lastname']; ?>"></td></tr>
<tr><th> Email</th><td><input name="email" type="text" id="email" value="<? echo $rows['email']; ?>"><a href="mailto:<?= $row['email']; ?>"></a></td></tr>
<tr><th> Address</th><td><input name="address" type="text" id="address" value="<? echo $rows['address']; ?>"></td></tr>
<tr><th> City</th><td><input name="city" type="text" id="city" value="<? echo $rows['city']; ?>"></td></tr>
<tr><th> State</th><td><input name="state" type="text" id="state" value="<? echo $rows['state']; ?>"></td></tr>
<tr><th> Zip Code</th><td><input name="zip" type="text" id="zip" value="<? echo $rows['zip']; ?>"></td></tr>
<tr><th> Phone</th><td><input name="phone" type="text" id="phone" value="<? echo $rows['phone']; ?>"></td></tr>
<tr>
<td> </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>

</tr>
</table>
</td>
</form>
</tr>
</table>

<?php

// close connection
mysql_close();

?>

 

 

This is the part tha doesn't work!

 

<?php
include 'config.php';

$getId = $_POST['id'];
$getUnit = $_POST['unit'];
$getCompany = $_POST['company'];
$getName = $_POST['name'];
$getLastname = $_POST['lastname'];
$getEmail = $_POST['email'];
$getAddress = $_POST['address'];
$getCity = $_POST['city'];
$getState = $_POST['state'];
$getZip = $_POST['zip'];
$getPhone = $_POST['phone'];

// Connect to server and select database.
mysql_connect($dbhost, $dbuser, $dbpass)or die("cannot connect");
mysql_select_db("vetmanpc")or die("cannot select DB");
echo "Connected to Database <br>";
// update data in mysql database
$sql = "UPDATE $dbname SET unit= '$getUnit', company= '$getCompany', name= '$getName', lastname= '$getLastname', email= '$getEmail', address= '$getAddress', city= '$getCity', state= '$getState', zip= '$getZip', phone= '$getPhone', WHERE id='$getId'";

$result=mysql_query($sql);

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

else {
echo "ERROR";
}

?>

 

I only get the "error" message, nothing in the database is changed. I'm kind of lost now!  I thought it would work.

Thanks in advance for looking!

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.