Jump to content

Won't Update the information typed in... into the database! ?


colleyboy

Recommended Posts

Cheers mzas87 for the reply.. will keep that in mind.

 

But I have ANOTHER problem!!!

 

When I go to update.php and fill out the form it takes me to updated.php and says its updated the database.

 

It does not seem to update anything though as when i check the db the information is still the same...  can someone help me.

 

Have I missed a line of code out or something?

 

Update.php:

 

<?
$id=$_GET['id'];
include("dbinfo.inc.php");
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM contacts WHERE id='$id'";
$result=mysql_query($query);
$num=mysql_numrows($result); 
mysql_close();

$i=0;
while ($i < $num) {
$first=mysql_result($result,$i,"first");
$last=mysql_result($result,$i,"last");
$phone=mysql_result($result,$i,"phone");
$mobile=mysql_result($result,$i,"mobile");
$fax=mysql_result($result,$i,"fax");
$email=mysql_result($result,$i,"email");
$web=mysql_result($result,$i,"web");

?>

<form action="updated.php">
<input type="hidden" name="ud_id" value="<? echo "$id"; ?>">
First Name: <input type="text" name="ud_first" value="<? echo "$first"?>"><br>
Last Name: <input type="text" name="ud_last" value="<? echo "$last"?>"><br>
Phone Number: <input type="text" name="ud_phone" value="<? echo "$phone"?>"><br>
Mobile Number: <input type="text" name="ud_mobile" value="<? echo "$mobile"?>"><br>
Fax Number: <input type="text" name="ud_fax" value="<? echo "$fax"?>"><br>
E-mail Address: <input type="text" name="ud_email" value="<? echo "$email"?>"><br>
Web Address: <input type="text" name="ud_web" value="<? echo "$web"?>"><br>
<input type="Submit" value="Update">
</form>

<?
++$i;
} 
?>

 

UPDATED.php:

 

<?
include("dbinfo.inc.php");
mysql_connect(localhost,$username,$password);

$ud_id=$_POST['ud_id'];
$ud_first=$_POST['ud_first'];
$ud_last=$_POST['ud_last'];
$ud_phone=$_POST['ud_phone'];
$ud_mobile=$_POST['ud_mobile'];
$ud_fax=$_POST['ud_fax'];
$ud_email=$_POST['ud_email'];
$ud_web=$_POST['ud_web'];


$query="UPDATE contacts SET first='$ud_first', last='$ud_last', phone='$ud_phone', mobile='$ud_mobile', fax='$ud_fax', email='$ud_email', web='$ud_web' WHERE id='$ud_id'";
@mysql_select_db($database) or die( "Unable to select database");
mysql_query($query);
echo "Record Updated";
mysql_close();
?>

 

Obviously the problem I am having is the database is not updated with the typed in information for some reason?

 

Thanks guys.

 

Ian

I'll bet if you look at the address bar in your browser, all your variables are in there, no? Forms default to GET if no method="" is specified.

 

<form action="updated.php">

 

Should be

 

<form action="updated.php" method="POST">

Many thanks !!!! knew it was something silly!!!  ur a legend :D... i owe u one :)

 

silly question tho.  If i wanted to add a search box to search for characters from the table and display results how would I go about it? :S

for example...

 

i have names in my table.

 

The table in index.php lists all the rows in the table.

 

adam smith

bill brush

etc....

 

If i only want to find people with the last name smith in the table how would i make a search bar adequete.  so it searches the table and displays information that matches.

 

I.E: Barcodes were in the table... You type barcode in search box and it displays the row with matching barcode...?

 

pikachu... u might be able to help me.

 

I am trying to mirror the code above that i used to update records in the table so that i could delete records.

 

I copied the two files and renamed then delete.php and deleted.php

 

I changed the:

 

$query="UPDATE contacts SET first='$ud_first', last='$ud_last', phone='$ud_phone', mobile='$ud_mobile', fax='$ud_fax', email='$ud_email', web='$ud_web' WHERE id='$ud_id'";

 

to:

 

$query="DELETE FROM contacts SET first='$ud_first', last='$ud_last', phone='$ud_phone', mobile='$ud_mobile', fax='$ud_fax', email='$ud_email', web='$ud_web' WHERE id='$ud_id'";

 

it is not doing anything!?  I would have thought by changing that one command would have told mysql to delete the record and not update it.  But... nothings happening? What am I doing wrong mate :)

Initially, you can use a form in conjunction with a "SELECT `field` FROM `table` WHERE `field` = '$search_term'"; query, but that will only provide exact matches.

You can use a wildcard LIKE query such as "SELECT `field` FROM `table` WHERE `field` LIKE '%" . $search_term . "%'", in which the '%' will match any number of characters.

The most flexible, though is the FULLTEXT boolean search. Information on that is HERE.

DELETE syntax is different from UPDATE syntax. There's no need to list out the fields to delete a record. If you want to leave the record intact, but delete some of the information in it, you'd still use an UPDATE query. For example, if you wanted to remove the website address from one of the records, you could just clear the field in your update form. It would then update it as an empty field. If you truly want to delete, that's like this:

 

$query = "DELETE FROM `contacts` WHERE `id` = $ud_id LIMIT 1";
// I always use a LIMIT with a DELETE query, so I don't inadvertently wipe out an entire database with one keystroke.

Thanks again picatchu,

 

The delete function works :D.

 

I am a bit of a novice at php.  Would you be able to explain a bit further about the searching.  A wildcard search would be great if it means it picks up "smi" out of the word "smith" and displays anything with smi in it... like smitts and smittfield lol.

 

Many Thanks...

 

Maybe a code example if you could :D!!

 

Kind Regards and many thanks for your time,

Ian

Yup, that's exactly what a wild card search does. MySQL manual page for it is here, and it's a good place to start, but can be a little confusing :). There's another tutorial here.

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.